传引用而不是传参数 pass by reference
function f(x)
x[1]=0
return x
end
x=[1,2,3]
f(x) # [0,2,3]
x # [0,2,3]
return type
function(x,y)::Int8 #返回值是Int8类型
只在julia无法判断返回类型时,才需要指定类型。
return nothing 或者只写 return
[1 2 3] #hcat
[1, 2, 3] #vcat
[1; 2; 3] #vcat
g(x,y)=x+y,x*y
匿名函数
(x,y) -> x+y
do block
function mysum(f,x)
out = zero(fx[1]))
end
return out
end
#do
mysum(x) do x
if x>0.5
return x
else
return 0.5
end
end
Julia字符串不能广播是最小单元,不是char的vector,也能用for in 迭代。与其他语言不同:”abc” * “def” 用乘法拼接字符串,join(R:paste)函数也可以用于拼接,支持分隔符。
str_list=[“hello”,”world”,”julians!”]
string(str_list…)
字符串拼接
"abc" * "def"
join()
字符串插值
name = "3C"
"hello world $name"
x=[1,2,3]
"the sum of $x is $(sum(x))"
raw ```julia str = raw””” Hello \n world “””
“Hello \n\nworld\n”
- 查找
strartswith(x,"xy")<br />endswith(x,"apple")<br />findfisrt(isequal('p'),"apple")<br />occusin("a","apple")<br />x=[1,2,3]
- reduce(+,x) == 1+2+3
- map(abs2,x) == 1+2+3
- mapreduce(abs2,+,x)
- foreach 无返回值 ~ map 有返回值
Function<br />传引用而不是传参数 pass by reference
```julia
function f(x)
x[1]=0
return x
end
x=[1,2,3]
f(x) # [0,2,3]
x # [0,2,3]
return type
function(x,y)::Int8 #返回值是Int8类型
只在julia无法判断返回类型时,才需要指定类型。
return nothing 或者只写 return
[1 2 3] #hcat
[1, 2, 3] #vcat
[1; 2; 3] #vcat
g(x,y)=x+y,x*y
匿名函数
(x,y) -> x+y
do block
function mysum(f,x)
out = zero(fx[1]))
end
return out
end
#do
mysum(x) do x
if x>0.5
return x
else
return 0.5
end
end