Wiki: Julia

Check CGSE Julia golfing tips

imperative/functional/array

Julia syntax is flexible enough to allow different programming styles. A simple task as printing the numbers from 1 to 10 each on a new line can be written in several ways.

The classic imperative style reads

# 29 bytes
for i=1:10
    println(i)
end

This can be easily golfed by removing all the indentation

# 24 bytes
for i=1:10;println(i)end

Note that no space is required between the closing bracket and end.

Julia supports list comprehension as well, therefore we may write also

# 23 bytes
[println(i) for i=1:10]

A more functional approach would use map as

# 23 bytes
map(i->println(i),1:10)

where -> is used to define an anonymous function. The previous can be even shorter:

# 20 bytes
map(println,1:10)

However, julia really shines when we start broadcasting functions over arrays. Every function can be applied to all the elements of an array by postpending a ., hence

# 14 bytes
println.(1:10)

Broadcasting

You can use the broadcast operator + pipe operator to save parentheses. (x->x^2+1).([1,2,3]) => [1,2,3].|>x->x^2+1