A call for community supplied Julia Code Examples

Hey all, The Julia Language website currently has a “Code Examples” section that is linked on the main page which is designed to show how easy it is to use Julia syntactically. The existing example is fine in my opinion but I would like to extend this further with more examples.

Please see here: Code examples and feel free to open a PR to the website repo or link code examples below and I can add them. Thanks!

3 Likes

Are the examples limited to the core of Julia? Can we add examples using specific packages?

1 Like

I am open to it!

See e.g. https://rosettacode.org/wiki/Category:Julia

4 Likes

The default example (Code examples) Mandelbrot set can be written cheaper:

f(z,a) = z^2 + a
mandelbrot(a, n=50) = (n == 0) ? 0 : f(mandelbrot(a, n-1) ,a)

or even

mandelbrot(a, n=50) = (n == 0) ? 0 : mandelbrot(a, n-1)^2 + a

Instead of

function mandelbrot(a)
    z = 0
    for i=1:50
        z = z^2 + a
    end
    return z
end

But that presumably introduces function call overhead. Did you benchmark?

1 Like

I didn’t benchmark.
Since this is a teaching example, I only wrote it to shrink the number of code lines from 7 to 2 or 1. Plus it gives new users practice w recursive functions.

1 Like

I think the point of the existing example is that it highlights the syntax of Julia will be familiar to those who know how to program already.

2 Likes

Wouldn’t a fibonnaci or binomial coefficients example show the recursion better? They are way more familiar.

2 Likes

I’m not sure, this example was suggested by folks. Feel free to add a PR with other relevant examples!

Hi @logankilpatrick, I looked up some random “learn python/java/html” website for inspiration and here are a few ideas:

Collect prime numbers up to 100

primes = Int[]

for i in 2:100
    divisible = false
    for p in primes
        if rem(i/p, 1) == 0.0
            divisible = true
            break
        end
    end
    if !divisible
        push!(primes, i)
    end
end

primes

“countmap” a string

d = Dict()
str = "a string is an array of characters"

for char in str
    if !haskey(d, char)
        d[char] = 0
    end
    d[char] += 1
end

# alternatively, the loop above can be a one liner:
# foreach(char->d[char] = get(d, char, 0) + 1, str)

d

Magic eight ball

responses = ["As I see it, yes."
             "Ask again later."
             "Better not tell you now."
             "Cannot predict now."
             "Concentrate and ask again."
             "Don’t count on it."
             "It is certain."
             "It is decidedly so."
             "Most likely."
             "My reply is no."
             "My sources say no."
             "Outlook not so good."
             "Outlook good."
             "Reply hazy, try again."
             "Signs point to yes."
             "Very doubtful."
             "Without a doubt."
             "Yes."
             "Yes – definitely."
             "You may rely on it."]

while true
    question = readline(stdin)
    println(question)
    println("\t", rand(responses))
end

3 Likes