/usr/share/julia/base/iterator.jl is in julia-common 0.4.7-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | # This file is a part of Julia. License is MIT: http://julialang.org/license
isempty(itr) = done(itr, start(itr))
# enumerate
immutable Enumerate{I}
itr::I
end
enumerate(itr) = Enumerate(itr)
length(e::Enumerate) = length(e.itr)
start(e::Enumerate) = (1, start(e.itr))
function next(e::Enumerate, state)
n = next(e.itr,state[2])
(state[1],n[1]), (state[1]+1,n[2])
end
done(e::Enumerate, state) = done(e.itr, state[2])
eltype{I}(::Type{Enumerate{I}}) = Tuple{Int, eltype(I)}
# zip
abstract AbstractZipIterator
immutable Zip1{I} <: AbstractZipIterator
a::I
end
zip(a) = Zip1(a)
length(z::Zip1) = length(z.a)
eltype{I}(::Type{Zip1{I}}) = Tuple{eltype(I)}
start(z::Zip1) = (start(z.a),)
function next(z::Zip1, st)
n = next(z.a,st[1])
return ((n[1],), (n[2],))
end
done(z::Zip1, st) = done(z.a,st[1])
immutable Zip2{I1, I2} <: AbstractZipIterator
a::I1
b::I2
end
zip(a, b) = Zip2(a, b)
length(z::Zip2) = min(length(z.a), length(z.b))
eltype{I1,I2}(::Type{Zip2{I1,I2}}) = Tuple{eltype(I1), eltype(I2)}
start(z::Zip2) = (start(z.a), start(z.b))
function next(z::Zip2, st)
n1 = next(z.a,st[1])
n2 = next(z.b,st[2])
return ((n1[1], n2[1]), (n1[2], n2[2]))
end
done(z::Zip2, st) = done(z.a,st[1]) | done(z.b,st[2])
immutable Zip{I, Z<:AbstractZipIterator} <: AbstractZipIterator
a::I
z::Z
end
zip(a, b, c...) = Zip(a, zip(b, c...))
length(z::Zip) = min(length(z.a), length(z.z))
@generated function tuple_type_cons{S,T<:Tuple}(::Type{S}, ::Type{T})
Tuple{S, T.parameters...}
end
eltype{I,Z}(::Type{Zip{I,Z}}) = tuple_type_cons(eltype(I), eltype(Z))
start(z::Zip) = tuple(start(z.a), start(z.z))
function next(z::Zip, st)
n1 = next(z.a, st[1])
n2 = next(z.z, st[2])
(tuple(n1[1], n2[1]...), (n1[2], n2[2]))
end
done(z::Zip, st) = done(z.a,st[1]) | done(z.z,st[2])
# filter
immutable Filter{F,I}
flt::F
itr::I
end
filter(flt, itr) = Filter(flt, itr)
start(f::Filter) = start_filter(f.flt, f.itr)
function start_filter(pred, itr)
s = start(itr)
while !done(itr,s)
v,t = next(itr,s)
if pred(v)
return (false, v, t)
end
s=t
end
(true,)
end
next(f::Filter, s) = advance_filter(f.flt, f.itr, s)
function advance_filter(pred, itr, st)
_, v, s = st
while !done(itr,s)
w,t = next(itr,s)
if pred(w)
return v, (false, w, t)
end
s=t
end
v, (true,)
end
done(f::Filter, s) = s[1]
eltype{I}(::Type{Filter{I}}) = eltype(I)
# Rest -- iterate starting at the given state
immutable Rest{I,S}
itr::I
st::S
end
rest(itr,state) = Rest(itr,state)
start(i::Rest) = i.st
next(i::Rest, st) = next(i.itr, st)
done(i::Rest, st) = done(i.itr, st)
eltype{I}(::Type{Rest{I}}) = eltype(I)
# Count -- infinite counting
immutable Count{S<:Number}
start::S
step::S
end
countfrom(start::Number, step::Number) = Count(promote(start, step)...)
countfrom(start::Number) = Count(start, one(start))
countfrom() = Count(1, 1)
eltype{S}(::Type{Count{S}}) = S
start(it::Count) = it.start
next(it::Count, state) = (state, state + it.step)
done(it::Count, state) = false
# Take -- iterate through the first n elements
immutable Take{I}
xs::I
n::Int
end
take(xs, n::Int) = Take(xs, n)
eltype{I}(::Type{Take{I}}) = eltype(I)
start(it::Take) = (it.n, start(it.xs))
function next(it::Take, state)
n, xs_state = state
v, xs_state = next(it.xs, xs_state)
return v, (n - 1, xs_state)
end
function done(it::Take, state)
n, xs_state = state
return n <= 0 || done(it.xs, xs_state)
end
# Drop -- iterator through all but the first n elements
immutable Drop{I}
xs::I
n::Int
end
drop(xs, n::Int) = Drop(xs, n)
eltype{I}(::Type{Drop{I}}) = eltype(I)
function start(it::Drop)
xs_state = start(it.xs)
for i in 1:it.n
if done(it.xs, xs_state)
break
end
_, xs_state = next(it.xs, xs_state)
end
xs_state
end
next(it::Drop, state) = next(it.xs, state)
done(it::Drop, state) = done(it.xs, state)
# Cycle an iterator forever
immutable Cycle{I}
xs::I
end
cycle(xs) = Cycle(xs)
eltype{I}(::Type{Cycle{I}}) = eltype(I)
function start(it::Cycle)
s = start(it.xs)
return s, done(it.xs, s)
end
function next(it::Cycle, state)
s, d = state
if done(it.xs, s)
s = start(it.xs)
end
v, s = next(it.xs, s)
return v, (s, false)
end
done(it::Cycle, state) = state[2]
# Repeat an object infinitely many times
immutable Repeated{O}
x::O
end
repeated(x) = Repeated(x)
eltype{O}(::Type{Repeated{O}}) = O
start(it::Repeated) = nothing
next(it::Repeated, state) = (it.x, nothing)
done(it::Repeated, state) = false
repeated(x, n::Int) = take(repeated(x), n)
|