bat/tests/syntax-tests/highlighted/Julia/test.jl

72 lines
6.5 KiB
Julia
Raw Permalink Normal View History

2021-07-13 09:07:29 +02:00
x = 3
2020-10-05 12:53:01 +02:00
2021-07-13 09:07:29 +02:00
y = 2x
2020-10-05 12:53:01 +02:00
typeof(y)
2021-07-13 09:07:29 +02:00
f(x) = 2 + x
2020-10-05 12:53:01 +02:00
f
f(10)
2021-07-13 09:07:29 +02:00
function g(x, y)
 z = x + y
 return z^2
2020-10-05 12:53:01 +02:00
end
g(1, 2)
2021-07-13 09:07:29 +02:00
let s = 0
 for i in 1:10
 s += i # Equivalent to s = s + i
2020-10-05 12:53:01 +02:00
 end
2021-07-13 09:07:29 +02:00
 s
2020-10-05 12:53:01 +02:00
end
typeof(1:10)
function mysum(n)
2021-07-13 09:07:29 +02:00
 s = 0
 for i in 1:n
 s += i
2020-10-05 12:53:01 +02:00
 end
2021-07-13 09:07:29 +02:00
 return s
2020-10-05 12:53:01 +02:00
end
mysum(100)
2021-07-13 09:07:29 +02:00
a = 3
2020-10-05 12:53:01 +02:00
2021-07-13 09:07:29 +02:00
a < 5
2020-10-05 12:53:01 +02:00
2021-07-13 09:07:29 +02:00
if a < 5
2020-10-05 12:53:01 +02:00
 "small"
else
 "big"
end
2021-07-13 09:07:29 +02:00
v = [1, 2, 3]
2020-10-05 12:53:01 +02:00
typeof(v)
v[2]
v[2] = 10
2021-07-13 09:07:29 +02:00
v2 = [i^2 for i in 1:10]
2020-10-05 12:53:01 +02:00
2021-07-13 09:07:29 +02:00
M = [1 2
2020-10-05 12:53:01 +02:00
 3 4]
typeof(M)
zeros(5, 5)
2021-07-13 09:07:29 +02:00
zeros(Int, 4, 5)
2020-10-05 12:53:01 +02:00
2021-07-13 09:07:29 +02:00
[i + j for i in 1:5, j in 1:6]