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: https://julialang.org/learning/code-examples/ and feel free to open a PR to the website repo or link code examples below and I can add them. Thanks!
Are the examples limited to the core of Julia? Can we add examples using specific packages?
See e.g. https://rosettacode.org/wiki/Category:Julia
The default example (https://julialang.org/learning/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)
mandelbrot(a, n=50) = (n == 0) ? 0 : mandelbrot(a, n-1)^2 + a
z = 0
z = z^2 + a
But that presumably introduces function call overhead. Did you benchmark?
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.
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.
Wouldn’t a fibonnaci or binomial coefficients example show the recursion better? They are way more familiar.
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
"countmap" a string
d = Dict()
str = "a string is an array of characters"
for char in str
if !haskey(d, char)
d[char] = 0
d[char] += 1
# alternatively, the loop above can be a one liner:
# foreach(char->d[char] = get(d, char, 0) + 1, str)
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."
"My reply is no."
"My sources say no."
"Outlook not so good."
"Reply hazy, try again."
"Signs point to yes."
"Without a doubt."
"Yes – definitely."
"You may rely on it."]
question = readline(stdin)