Julia: Difference between revisions
No edit summary |
No edit summary |
||
Line 33: | Line 33: | ||
<syntaxhighlight lang="julia"> | <syntaxhighlight lang="julia"> | ||
"Variable x is $x, y is $y, and x+y is $(x+y)" | "Variable x is $x, y is $y, and x+y is $(x+y)" | ||
</syntaxhighlight> | |||
===Arrays=== | |||
{{main|https://en.wikibooks.org/wiki/Introducing_Julia/Arrays_and_tuples}} | |||
<syntaxhighlight lang="julia"> | |||
// Make an 1d array of Float64. | |||
// Equivalent to [0.0, 0.0, 0.0] | |||
myArr = zeros(4); | |||
// Make a 1d array of Float64 left to uninitialized values | |||
myArr = Array{Float64,1}(undef, 3) | |||
// Convert an iterable to an array with collect | |||
myArr = collect(1:3) | |||
// Reference copy | |||
a = [1, 2, 3]; | |||
b = [4, 5, 6]; | |||
b = a | |||
// Shallow copy | |||
a = [1, 2, 3]; | |||
b = [4, 5, 6]; | |||
// elementwise equals operator | |||
b .= a | |||
// or b[:] = a | |||
// or b = copy(a) (this will discard the original b) | |||
// Basic functionality | |||
// Call functions elementwise by adding . after the function name | |||
a = [1, 5, 99]; | |||
a = clamp.(a, 1, 11); | |||
// a is now [1, 5, 11] | |||
// Equivalent to clamp!(a, 1, 11) | |||
// Julia also has map, foldl, foldr, reduce | |||
</syntaxhighlight> | </syntaxhighlight> | ||