Julia

第一节:REPL的使用

help模式:按?进入help模式

shell模式:按;进入shell模式

package模式:按 ] 进入package模式

按backspace键返回正常Julia模式

第二节:变量 整型 浮点型

变量

像其他动态语言一样,可以无需声明直接赋值

x=10
x="Hello world"
x=1.1
x="这是julia笔记"

变量名还可以是中文,当然不推荐这么做

测试=10
测试+1

还可以输入\ +符号名称的方式输入更多的Unicode数学字符,如\alpha后按tab键就会出现α的字符。命名规范跟其他编程语言的命名规范基本相同,如:

  • 变量名尽量小写

  • 类型和模块名首字母大写,单词间使用驼峰式分隔

  • 在几个单词不易区分时才以_分隔,一般不鼓励使用下划线_

  • 函数名和宏名使用小写字母,不使用下划线

  • 修改参数的函数结尾使用!,这样的函数被称为mutating functions或in-place functions

整型和浮点

整数类型

Type

Signed

Number of bits

Smallest value

Largest value

lnt8

8

-2^7

2^7-1

Ulnt8

8

0

2^8-1

lnt16

16

-2^15

2^15-1

Ulnt16

16

0

2^16-1

lnt32

32

-2^31

2^32-1

Ulnt32

32

0

2^32-1

lnt64

64

-2^63

2^63-1

Ulnt64

64

0

2^64-1

lnt128

128

-2^128

2^127-1

Ulnt128

128

0

2^128-1

Bool

N/A

8

false(0)

true(1)

浮点数

Type

Precision

Number of bits

Float16

half

16

Float32

single

32

Float64

double

64

在32位系统中,整数默认时lnt32类型,浮点数默认是Float32类型;在64位系统中,整数默认时lnt64类型,浮点数默认是Float64类型.

x=1
typeof(x)
>>lnt64
a=typeof(1.1)
>>Float64
typeof(a)
>>DataType
sizeof(Float64)
>>8
sizeof(lnt128)
>>16
sizeof(Bool)
>>1
sizeof(Signed)
>>ERROR: Abstract type Signed does not have a definite size.
Stacktrace:
 [1] sizeof(x::Type)
   @ Base .\essentials.jl:473
 [2] top-level scope
   @ REPL[24]:1

可以使用Sys.WORD_SIZE命令命令查看系统是32位还是64位,也可以直接输入lntUlnt看系统位数 Julia中的很多语法和REPL的用法都跟matlab很像,比如上一次的结果用ans表示

julia>x=1
a
julia>ans+1
2

十六进制 由于Julia的整数中定义了lnt和Uint两种大类型,其中lnt用十进制表示,Uint用十六进制表示,当然我们也可以以0b开头表示二进制,以0o开头表示八进制

x=10   
UInt8(x) %%用八位表示的十六进制的数
>>0x0a %%结果
x=0b1010 %%二进制变量
Int64(x)  %%将其转成十进制
>>10

二进制和十六进制也支持有符号数,直接在前面家-即可

-0xa
>>0xf6
bitstring(2)
>>"0000000000000000000000000000000000000000000000000000000000000010"
 bitstring(-2)
 >>"1111111111111111111111111111111111111111111111111111111111111110"

查看某种进制类型的最大最小值:

(typemin(Int32),typemax(Int32))
>>(-2147483648, 2147483647)
typemax(Int64)+1
>>-9223372036854775808

浮点数表示方法

1e-3   %%表示1*10^(-3)
>>0.001
 typeof(ans)
 >>Float64
1f-3
>>0.001f0

浮点的0.0和-0.0在表示方式中也是有点分别对

bitstring(0.0)
>>"0000000000000000000000000000000000000000000000000000000000000000"
 bitstring(-0.0)
 >>"1000000000000000000000000000000000000000000000000000000000000000"

精度

eps(Float32)
>>1.1920929f-7
eps(Float64)
>>2.220446049250313e-16

舍入模式

1.1+0.1
>>1.2000000000000002

任意精度的运算

 BigInt(typemax(Int64))+1
 >>9223372036854775808
 x=BigFloat(1.1+0.1)
 >>1.20000000000000017763568394002504646778106689453125

BigFloat也是有默认的长度的,不过可以调节,但每层调节只能do模块里面的精度

setrounding(BigFloat,RoundUp) do
    BigFloat(1) + parse(BigFloat,"0.1")
    end
    >>1.100000000000000000000000000000000000000000000000000000000000000000000000000003
    setrounding(BigFloat,RoundDown) do
    BigFloat(1) + parse(BigFloat,"0.1")
    end
    >>1.099999999999999999999999999999999999999999999999999999999999999999999999999986
    setprecision(40) do
    BigFloat(1) + parse(BigFloat,"0.1")
    end
    >>1.1000000000004

复数

julia> (1+2im)*(2-3im) %% im 表示复数里面的i
8 + 1im
julia> (1+2im)^2
-3 + 4im

运算优先级

2/5im %%表示2/(5*im)
julia> 2/5im==2/(5*im)
true

复数的其他运算

julia> sqrt(2+3im)
1.6741492280355401 + 0.8959774761298381im
julia> cos(1+2im)
2.0327230070196656 - 3.0518977991517997im
julia> exp(1+2im)
-1.1312043837568135 + 2.4717266720048188im
julia> x=1+2im
1 + 2im
julia> abs(x)
2.23606797749979
julia> x=1
1
julia> y=2
2
julia> z=complex(x,y)
1 + 2im
julia> real(z) %%取出复数z的实部
1
julia> imag(z) %%取出复数z的虚部
2
julia> angle(z) %%取出复数z的相位角
1.1071487177940904
julia> sqrt(-1+0im) %%不能直接对-1开根号
0.0 + 1.0im

分数

julia> 4//8 %%会自动约分
1//2
julia> numerator(2//3) %%取出分数的分子
2
julia> denominator(2//3)  %%取出分数的分母
3
julia> float(1//2)  %%分数转换成小数
0.5
julia> isequal(float(1//2),1/2) %%判断float(1//2)和1/2是否相等
true

字符串

julia> x='a'  %%用单引号定义字符
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
julia> Int(x) 
97
julia> Char(97) %%ASCII码对应
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
julia> '\u21' %%直接输入ASCII码值
'!': ASCII/Unicode U+0021 (category Po: Punctuation, other)
julia> Int('\t') %%算出\t对应的ASCII码值
9
julia> str="Hello Word" %%定义字符串
"Hello Word"
julia> str[1] %%对字符串进行索引(从1开始的)
'H': ASCII/Unicode U+0048 (category Lu: Letter, uppercase)
julia> str[end-3:end]
"Word"

Julia完整支持Unicode字符和字符串,Unicode嘛位可以使用\u\U来转义,在Julia中,非ASCII字符使用UTF-8编码,但UTF-8编码是一种变长的编码方式,也就是说不同字符的编码长度可能不同,这就导致在使用一些非常见字符时可能会碰到很麻烦的问题。

julia> str="\u2200 x \u2203 y"
"∀ x ∃ y"
julia> str[1]
'∀': Unicode U+2200 (category Sm: Symbol, math)
julia> str[2]
ERROR %%报错
julia> str[3]
ERROR %%报错
julia> str[4]
' ': ASCII/Unicode U+0020 (category Zs: Separator, space)

在UTF-8的编码中,'\u2200'即'∀使用了三个字符,因此str[2]和str[3]都是无效的。str[4]为空格。但可以这么使用:

julia> str[1:4]
"∀ "

也可以使用firstindexlastindex来索引

julia> for i=firstindex(str):lastindex(str)
       try
       println(str[i])
       catch
       end
       end
∀

X

∃

Y

当然,也可以像Python一样,直接将字符串当作遍历对象,而且不需要异常处理:

julia> for i in str
       print(i)
       end
∀ X ∃ Y

字符串的其他操作

julia> x="Hello"
"Hello"
julia> y="Word"
"Word"
julia> string(x,y)
"HelloWord"
julia> x* ',' *y
"Hello,Word"
julia> string("$x $y !") %%取出x的值
"Hello Word !"
julia> print("\$100") %% $是一个特殊的字符,当做文字使用时需转义
$100
julia> "abc"^3
"abcabcabc"
julia> lowercase("HELLO") %%将大写改小写
"hello"
julia> uppercase("hello") %%将小写改大写
"HELLO"
julia> replace("I want to learn Python","Python"=>"Julia")
"I want to learn Julia"
julia> replace("ABCD","A"=>"S")
"SBCD"
julia> startswith("julia is interesting","julia")
true
julia> startswith(" julia is interesting","julia") #有空格
false
julia> startswith(strip(" julia is interestring"),"julia") #strip去掉字符串中给空格
true
julia> s=split("one two three") #分割字符串
3-element Vector{SubString{String}}:
 "one"
 "two"
 "three"
julia> join(s) #拼接字符串
"onetwothree"
julia> join(s," ")
"one two three"

跨越多行的字符串

julia> """
       hello
       word
       !"""
"hello\nword\n!"

字符串的数学运算

julia> "abcd"<"xyz" #对应位置按字母表顺序大小比较
true
julia> "abcd"!="abcde"
true
julia> findfirst(isequal('x'),"wexilinx") #找字符串"wexilinx"中第一次出现'x'的位置
3
julia> findnext(isequal('w'),"wechartwithaperson",1)
1
julia> findnext(isequal('w'),"wechartwithperson",2)
8
julia> occursin("world","hello,world") #判断字符串"world"是否出现在字符串"hello,world"中
true
julia> repeat('a',10)
"aaaaaaaaaa"
julia> x="abc"
"abc"
julia> repeat(x,5)
"abcabcabcabcabc"
julia> join(["I","fall","in","love"],",","her") #矩阵表示要连接的字符串,第一个参数","表示前面几个字符串用","连接,最后两个字符串用第二个参数"her"连接
"I,fall,inherlove"
julia> x="abcdefgh"
"abcdefgh"
julia> length(x,2,4)
3

字符串的地方当然少不了正则表达式

julia> r"^\s*(?:#|$)"
r"^\s*(?:#|$)"
julia> typeof(ans)
Regex

学累了看看我女神哈哈哈哈哈哈哈

(https://cdn.limfx.pro/-img-bf3c9d43-66a1-4af0-b7b5-a93ec30e1a89-articles-66966852-3d59-49ac-af59-5b96efd6e4b7-articelcontent-02b679e0-694b-46a7-8924-417a80255446/朴信惠9.0.jpg)

Figure.1 Tired of learning to Look at my goddess

元祖 字典 集合

Tuple()表示,内容不可更改

julia> x1=(1,2,3,4)
(1, 2, 3, 4)
julia> x1[1]
1
julia> length(x1)
4
julia> x1[end]
4
julia> x1[2:end]
(2, 3, 4)
julia> x2=(1,1.2,'a',"hello")
(1, 1.2, 'a', "hello")
julia> x3=(a=1,b=2)
(a = 1, b = 2)
julia> x3.a
1

字典 Julia中也支持字典类型

julia> dic=Dict("aa"=>1,"bb"=>2,"cc"=>3)
Dict{String, Int64} with 3 entries:
  "bb" => 2
  "aa" => 1
  "cc" => 3
julia> typeof(dic)
Dict{String, Int64}
julia> dic.count #查看dic中几个数值
3
julia> dic.keys
16-element Vector{String}:
 #undef
 #undef
   ⋮
 #undef
 #unde
julia> dic.vals
16-element Vector{Int64}:
 0
 0
 0
 0
 2
 0
 0
 0
 0
 0
 0
 0
 1
 3
 0
 0
 julia> dic["aa"] %%取值
1

也可以把Array转成字典

julia> ary=["one"=>1,"two"=>2]
2-element Vector{Pair{String, Int64}}:
 "one" => 1
 "two" => 2
 julia> Dict(ary)
Dict{String, Int64} with 2 entries:
  "two" => 2
  "one" => 1

把Array中的Tuple转成字典

julia> tpl=[("one",1),("two",2)]
2-element Vector{Tuple{String, Int64}}:
 ("one", 1)
 ("two", 2)
julia> Dict(tpl)
Dict{String, Int64} with 2 entries:
  "two" => 2
  "one" => 1

把两个Array组合成Dict

julia> vas=[1,2,3]
3-element Vector{Int64}:
 1
 2
 3
julia> kes=["one","two","three"]
3-element Vector{String}:
 "one"
 "two"
 "three"
julia> Dict(zip(kes,vas))
Dict{String, Int64} with 3 entries:
  "two"   => 2
  "one"   => 1
  "three" => 3

新建一个新字典

julia> d=Dict()
Dict{Any, Any}()
julia> d["one"]=1
1

在新建字典时指定类型

julia> d=Dict{String,Int64}()
Dict{String, Int64}()

字典遍历

julia> d=Dict("one"=>1,"two"=>2,"three"=>3)
Dict{String, Int64} with 3 entries:
  "two"   => 2
  "one"   => 1
  "three" => 3
julia> for (k,v) in d
       println("key is $k for value $v")
       end
key is two for value 2
key is one for value 1
key is three for value 3
julia> for v in d
       println(v)
       end
"two" => 2
"one" => 1
"three" => 3

字典的其他用法

julia> d=Dict("one"=>1,"Two"=>2)
Dict{String, Int64} with 2 entries:
  "Two" => 2
  "one" => 1
julia> get(d,"three",-1) #查找d中是否有three无责显示-1
-1
julia> get(d,"one",-1)
1
julia> delete!(d,"one") #删除字典中的key
Dict{String, Int64} with 1 entry:
  "Two" => 2
julia> fieldnames(typeof(d))
(:slots, :keys, :vals, :ndel, :count, :age, :idxfloor, :maxprobe) 

Set

julia> S=Set([1,3 ,2,4,3,2])
Set{Int64} with 4 elements:
  4
  2
  3
  1
julia> S=Set(1:3:10)
Set{Int64} with 4 elements:
  4
  7
  10
  1
julia> 2 in S
false 
julia> issubset([1,4],S)  #包含关系
true
julia> A=Set([1,2,5,7])
Set{Int64} with 4 elements:
  5
  7
  2
  1
julia> B=([2,5,8,9,4])
5-element Vector{Int64}:
 2
 5
 8
 9
julia> intersect(A,B) #求A∩B
Set{Int64} with 2 elements:
  5
  2
julia> union(A,B)  #求A或B
Set{Int64} with 7 elements:
  5
  4
  7
  2
  9
  ⋮ 
julia> intersect([1,3,5],[2,5,6])
1-element Vector{Int64}:
 5

第三节:数学运算

比matlab更直观的数学表达式

julia> x=10
10
julia> 22x
220

但这就导致了可能会出现语法的冲突

  • 十六进制整数文本表达式0xff可以解析为数值文本0乘以变量xff

  • 浮点数文本表达式1e10可以解析为数值文本1乘以变量e10

因此,Julia中

  • 以0x开头的表达式,都被解析为十六进制文本

  • 以数字文本开头,后面跟着e或E,都被解析为浮点数文本

##运算方法 常用的四则运算跟其他语言基本完全一样 向量运算,跟matlab的操作完全相同

julia> [1,2,3].*3
3-element Vector{Int64}:
 3
 6
 9

比较运算,支持链式比较

julia>  1 <= 2 <= 3 == 3 <=5 > 4 >=2
true

常用数学函数

#进位函数
round(x)  #四舍五入
floor(x)  #向下取整
ceil(x)   #向上取整
trunc(x)  #trunc时直接砍掉小数,在整数的时候trunc跟floor一样,负数时跟ceil一样
#除法运算
div(x,y)   #取模
fld(x,y)   #取小于结果的最大整数
cld(x,y)   #取大于结果的最大整数
rem(x,y)   #取余
mod(x,y)   #取余
mod1(x,y)  #如果x是y的整数倍,则返回y,不是则返回余数
mod2pi(x)  #对2pi取余
divrem(x,y) #返回取模的值和取余的值
fldmod(x,y) #返回取小于x的最大整数和取余的值
gcd(x,y...) #最大公约数
lcm(x,y...) #最小公约数
#符号函数
abs(x)  #取模(取绝对值)
abs2(x) #取平方
sign(x) #取符号(以-1/+1的形式返回给定值的符号)
signbit(x) #正数返回false,负数返回true
copysign(x,y) #返回x*sign(y)
flipsign(x,y) #返回x*sign(x*y)
#开根号 log
sqrt(x) #开根号
cbrt(x)
hypot(x,y)
exp(x)
expml(x)
ldexp(x,n) #x^n
log(x)  #loge(x)
log(b,x)  #以b为底数的对数
log2(x)   #以2为底数的对数log2(x)
log10(x)
loglp(x)  #loge(1+x)
#三角函数
sin cos tan cot sec csc
sinh cosh tanh coth sech csch
asin acos atan acot asec acsc
asinh acosh atanh acoth asech acsch
sinc cosc

(https://cdn.limfx.pro/-img-bf3c9d43-66a1-4af0-b7b5-a93ec30e1a89-articles-66966852-3d59-49ac-af59-5b96efd6e4b7-articelcontent-f1357dc3-3419-4633-b005-9a5f22a8b2f6/朴信惠15.jpg)

figure.2 My goddess give me power!!!

##矩阵操作 定义一个简单的矩阵(向量),在REPL中看返回的类型

julia> a=[1,2,3,4]
4-element Vector{Int64}:
 1
 2
 3
 4

定义步进向量

julia> aa=(1:3:9)
1:3:7
julia> aa.step #step(aa)
3
julia> aa.start #first(aa)
1
julia> aa.stop #last(aa)
7
julia> Int8[3,5,7]
3-element Vector{Int8}:
 3
 5
 7
julia> ["one","two","three"]
3-element Vector{String}:
 "one"
 "two"
 "three"
julia> [true, "two",1.2,5]
4-element Vector{Any}:
 true
     "two"
    1.2
    5
julia> [] #定义一个空矩阵
Any[]
julia> Int8[] #定义一个空矩阵的类型
Int8[]
julia> a=(1,2,3)
(1, 2, 3)
julia> b=collect(a)
3-element Vector{Int64}:
 1
 2
 3
julia> c=collect(1:4)
4-element Vector{Int64}:
 1
 2
 3
 4
julia> c[2:end]
3-element Vector{Int64}:
 2
 3
 4
 julia> c1=c   #c1与c的内存地址相同,两者相关联
4-element Vector{Int64}:
 1
 2
 3
 4
julia> c2=c[:]  #c2只是c的一个拷贝,无关联关系
4-element Vector{Int64}:
 50
  2
  3
  4
julia> c=[1 2 3 4]
1×4 Matrix{Int64}:
 1  2  3  4
julia> b=[1,2,3,4]
4-element Vector{Int64}:
 1
 2
 3
 4
 julia> d=[1 2 3 4;5 6 7 8]
2×4 Matrix{Int64}:
 1  2  3  4
 5  6  7  8

矩阵拼接中空格 , ;的区别

julia> x=ones(2,3)
2×3 Matrix{Float64}:
 1.0  1.0  1.0
 1.0  1.0  1.0
julia> y=zeros(2,3)
2×3 Matrix{Float64}:
 0.0  0.0  0.0
 0.0  0.0  0.0
 julia> z=[x y]  #与函数hcat(x,y)功能相同
2×6 Matrix{Float64}:
 1.0  1.0  1.0  0.0  0.0  0.0
 1.0  1.0  1.0  0.0  0.0  0.0
 julia> s=[x,y]
2-element Vector{Matrix{Float64}}:
 [1.0 1.0 1.0; 1.0 1.0 1.0]
 [0.0 0.0 0.0; 0.0 0.0 0.0]
julia> hcat([x,y]...) #将矩阵内部的Array作拼接
2×6 Matrix{Float64}:
 1.0  1.0  1.0  0.0  0.0  0.0
 1.0  1.0  1.0  0.0  0.0  0.0
 julia> z=[x;y] #与函数vcat(x,y)功能相同
4×3 Matrix{Float64}:
 1.0  1.0  1.0
 1.0  1.0  1.0
 0.0  0.0  0.0
 0.0  0.0  0.0
julia> size(z) #查看矩阵维度
(4, 3)
julia> length(z) #查看矩阵元素数量
12
julia> sum(z) #求矩阵所有元素合
6.0

###矩阵运算

julia> a=reshape(1:6,2,3)
2×3 reshape(::UnitRange{Int64}, 2, 3) with eltype Int64:
 1  3  5
 2  4  6
julia> a=collect(a)
2×3 Matrix{Int64}:
 1  3  5
 2  4  6
julia> b=ones(2,3)
2×3 Matrix{Float64}:
 1.0  1.0  1.0
 1.0  1.0  1.0
 julia> a.+b
2×3 Matrix{Float64}:
 2.0  4.0  6.0
 3.0  5.0  7.0
 julia> a.-b
2×3 Matrix{Float64}:
 0.0  2.0  4.0
 1.0  3.0  5.0
julia> a.*b
2×3 Matrix{Float64}:
 1.0  3.0  5.0
 2.0  4.0  6.0
julia> a*b'
2×2 Matrix{Float64}:
  9.0   9.0
 12.0  12.0
julia> a/b    ??????????????????????????????
2×2 Matrix{Float64}:
 1.5  1.5
 2.0  2.0
julia> a./b
2×3 Matrix{Float64}:
 1.0  3.0  5.0
 2.0  4.0  6.0

函数对矩阵操作时需要加.

julia> A=[1,2,3]
3-element Vector{Int64}:
 1
 2
 3
julia> sin.(A)
3-element Vector{Float64}:
 0.8414709848078965
 0.9092974268256817
 0.1411200080598672

添加/删除/移动

julia> push!(A,4)  #在矩阵中添加元素
4-element Vector{Int64}:
 1
 2
 3
 4
julia> pop!(A)
4
julia> push!(A, 4,5,6)
6-element Vector{Int64}:
 1
 2
 3
 4
 5
 6
julia> append!(A, [7,8,9])
9-element Vector{Int64}:
 1
 2
 3
 ⋮
 8
 9
julia> prepend!(A, [10,11,12])
12-element Vector{Int64}:
 10
 11
 12
  ⋮
  8
  9
julia> arr=reshape(1:6,2,3)
2×3 reshape(::UnitRange{Int64}, 2, 3) with eltype Int64:
 1  3  5
 2  4  6
julia> circshift(arr,(0,1))
2×3 Matrix{Int64}:
 5  1  3
 6  2  4
 julia> circshift(arr,(1,-2))
2×3 Matrix{Int64}:
 6  2  4
 5  1  3
julia> rand(10)
10-element Vector{Float64}:
 0.06253825972833438
 0.5919606832956706
 0.3364134068565564
 ⋮
 0.7073378435854666
 0.09707256408853804
julia> rand(2,3)
2×3 Matrix{Float64}:
 0.108681  0.202068  0.761769
 0.444472  0.319045  0.845761
julia> rand(Int8,2,3)
2×3 Matrix{Int8}:
 -110   125  -65
   68  -112  -91

函数

函数定义

function f1(x,y,z)
    z^2+x^2+y^2
    end
julia> f1(1,2,3)
14

函数的最后一行是不需要加return的,return一般用于在函数中间返回时使用。因为Julia的代码都是表达式,表达式是有返回值的,要么是nothing,要么是别的,因此函数最后一行默认就是返回值,无需再加return;如果一个函数不想有返回值,那在最后一行写个nothing即可。 指定函数输出的类型

function g(x,y)::Int8
  return x*y
  end

符号也可以当做函数使用

julia> +(1,2,3)
6

给函数添加说明

"sample add function"
function funcadd(x,y) %同行输入
  x+y
  end

使用@doc 函数名可以查看函数前的注释,如果是在REPL定义的函数,则可以在help模式下查看函数使用说明 匿名函数

map(x->x*2-1,[1,2,3,4])
>>4-element Vector{Int64}:
 1
 3
 5
 7

多返回值

function f3(x,y)
x+y,x-y
end
julia> f3(7,2)
(9, 5)  #返回的是一个tuple
julia> function minval(x,y)
              if x>=y
                 x=y
            else
               x=x
               end
               end

可变参数

function f4(x...)
r1=length(x)
r2=x[r1]
return r1,r2
end
julia> f4(3,5,9,64,3,(12,13))
(6, (12, 13))

默认参数 关键字参数

function f5(x,y=1) #默认参数只能放在后面
return x+y
end
julia> f5(30)
31
julia> f5(4,5)
9
julia> function f6(x;y,z=3)
       x+y+z
       end
f6 (generic function with 1 method)

julia> f6(1,2)
ERROR
julia> f6(1,y=3)
7
julia> f6(1,y=4,z=5)
10
julia> f6(1,2,3)
ERROR

函数的其他用法 map

julia> function funcAbs(x)
       x >=0 ? x : -x
       end
funcAbs (generic function with 1 method)

julia> map(funcAbs,-3:3)
7-element Vector{Int64}:
 3
 2
 1
 ⋮
 2
 3

reduce

julia> function add(x,y)
       x+y
       end
add (generic function with 1 method)
julia> reduce(add,1:10)
55
julia> reduce(+,1:10)
55

|>操作符,对于运算顺序非常直观

julia> "abcde" |> uppercase
"ABCDE"
julia> [i for i in 1:5] |> join
"12345"

灵活定义函数

julia> f8(x::Int64,y::Int64)=2*x+y
f8 (generic function with 2 methods)
julia> f8(2,4)
8

(https://cdn.limfx.pro/-img-bf3c9d43-66a1-4af0-b7b5-a93ec30e1a89-articles-66966852-3d59-49ac-af59-5b96efd6e4b7-articelcontent-bf89b548-0f17-4b4c-b4fc-3e26a3f31c66/朴信惠14.jpg)

figure.3 End of class!!!


本文章使用limfx的vscode插件快速发布