一、简介函数
julia提供的各类接口为自定义类型扩展功能。
本文讨论一下Iteration接口。
自定义的类型实现这里接口就能够实现迭代功能,也就是能够直接在for循环这样使用:
假如iter是你的自定义类型:this
for i in iter # or "for i = iter" # body end
等同于:code
next = iterate(iter) while next !== nothing (i, state) = next # body next = iterate(iter, state) end
二、如何实现索引
要实现Iteration接口须要实现iterate(iter, state)方法,这个方法在Base包里面,它的含义是返回iter的下一个元素和其状态state,若是没有就返回nothing, state能够是索引。
咱们定义一个类型Squares,它有一个count域:接口
struct Squares count::Int end
而后定义他的iterate方法,要显性的指定报名:element
Base.iterate(S::Squares, state=1) = state > S.count ? nothing : (state*state, state+1)
state小于count是计算它的平方值,而后+1,不然返回nothing。it
定义好以后用for循环迭代以下:io
julia> for i in Squares(5) println(i) end 1 4 9 16 25
三、其余与迭代相关方法for循环
实现了iterable后还可使用其余相关的函数, 如 in (检查元素是否在集合中) 、 mean(求平均数) 和 std (求方差),后两个函数在Statistics包里:扩展
julia> 25 in Squares(10) true julia> using Statistics julia> mean(Squares(3)) 4.666666666666667 julia> std(Squares(100)) 3024.355854282583
四、实现其余迭代集合方法
咱们还能够为Squares实现其余与迭代集合有关的方法:
eltype(返回集合元素类型), length(返回集合元素个数),
julia> Base.eltype(::Type{Squares}) = Int # Note that this is defined for the type julia> Base.length(S::Squares) = S.count julia> sum(Squares(100)) 338350
默认的sum方法是经过循环迭代计算全部元素的和:
咱们能够从新实现sum方法使其更高效,使用公式直接求和
julia> Base.sum(S::Squares) = (n = S.count; return n*(n+1)*(2n+1)÷6) julia> sum(Squares(1803)) 1955361914
5.反向迭代
要实现反向迭代,须要实现Iterators.Reverse{T}的iterate方法,T指类型,这里咱们实现Iterators.Reverse{Squares},
collect收集集合元素,也能够用for迭代:
julia> Base.iterate(rS::Iterators.Reverse{Squares}, state=rS.itr.count) = state < 1 ? nothing : (state*state, state-1) julia> collect(Iterators.reverse(Squares(4))) 4-element Array{Int64,1}: 16 9 4 1 julia> for i in Iterators.reverse(Squares(5)) println(i) end 25 16 9 4 1