Tutorial by Examples

A common use case for a for loop is to iterate over a predefined range or collection, and do the same task for all its elements. For instance, here we combine a for loop with a conditional if-elseif-else statement: for i in 1:100 if i % 15 == 0 println("FizzBuzz") elsei...
In some situations, one might want to return from a function before finishing an entire loop. The return statement can be used for this. function primefactor(n) for i in 2:n if n % i == 0 return i end end @assert false # unreachable end Usage: jul...
In Julia, a for loop can contain a comma (,) to specify iterating over multiple dimensions. This acts similarly to nesting a loop within another, but can be more compact. For instance, the below function generates elements of the Cartesian product of two iterables: function cartesian(xs, ys) f...
Julia provides macros to simplify distributing computation across multiple machines or workers. For instance, the following computes the sum of some number of squares, possibly in parallel. function sumofsquares(A) @parallel (+) for i in A i ^ 2 end end Usage: julia> sumo...

Page 1 of 1