Skip to content

Commit 211d14d

Browse files
committed
add IterTools module around extra iterator functionality
1 parent 8450182 commit 211d14d

22 files changed

+489
-471
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ CORE_SRCS := $(addprefix $(JULIAHOME)/, \
198198
base/inference.jl \
199199
base/int.jl \
200200
base/intset.jl \
201-
base/iterator.jl \
202201
base/nofloat_hashing.jl \
203202
base/number.jl \
204203
base/operators.jl \

base/REPLCompletions.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,10 @@ function bslash_completions(string, pos)
376376
# return possible matches; these cannot be mixed with regular
377377
# Julian completions as only latex / emoji symbols contain the leading \
378378
if startswith(s, "\\:") # emoji
379-
emoji_names = filter(k -> startswith(k, s), keys(emoji_symbols))
379+
emoji_names = IterTools.filter(k -> startswith(k, s), keys(emoji_symbols))
380380
return (true, (sort!(collect(emoji_names)), slashpos:pos, true))
381381
else # latex
382-
latex_names = filter(k -> startswith(k, s), keys(latex_symbols))
382+
latex_names = IterTools.filter(k -> startswith(k, s), keys(latex_symbols))
383383
return (true, (sort!(collect(latex_names)), slashpos:pos, true))
384384
end
385385
end

base/asyncmap.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# This file is a part of Julia. License is MIT: http://julialang.org/license
22

3+
using Base.IterTools.Enumerate
34

45
"""
56
AsyncCollector(f, results, c...; ntasks=0) -> iterator

base/broadcast.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ end
226226
else
227227
R = typejoin(eltype(B), S)
228228
new = similar(B, R)
229-
for II in take(iter, count)
229+
for II in IterTools.take(iter, count)
230230
new[II] = B[II]
231231
end
232232
new[I] = V

base/client.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ end
2323
# Create a docstring with an automatically generated list
2424
# of colors.
2525
const possible_formatting_symbols = [:normal, :bold]
26-
available_text_colors = collect(filter(x -> !isa(x, Integer), keys(text_colors)))
26+
available_text_colors = collect(IterTools.filter(x -> !isa(x, Integer), keys(text_colors)))
2727
available_text_colors = cat(1,
2828
sort(intersect(available_text_colors, possible_formatting_symbols), rev=true),
2929
sort(setdiff( available_text_colors, possible_formatting_symbols)))

base/coreimg.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ include("reduce.jl")
6161
## core structures
6262
include("intset.jl")
6363
include("dict.jl")
64-
include("iterator.jl")
6564

6665
# core docsystem
6766
include("docs/core.jl")

base/dates/Dates.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module Dates
44

55
importall ..Base.Operators
66

7+
using Base.IterTools
8+
79
include("types.jl")
810
include("periods.jl")
911
include("accessors.jl")

base/deprecated.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,4 +1019,14 @@ eval(Multimedia, :(macro textmime(mime)
10191019
end
10201020
end))
10211021

1022+
@deprecate_binding Filter IterTools.Filter
1023+
@deprecate_binding Zip IterTools.Zip
1024+
@deprecate filter(flt, itr) IterTools.filter(flt, itr)
1025+
@deprecate_binding rest IterTools.rest
1026+
@deprecate_binding countfrom IterTools.countfrom
1027+
@deprecate_binding take IterTools.take
1028+
@deprecate_binding drop IterTools.drop
1029+
@deprecate_binding cycle IterTools.cycle
1030+
@deprecate_binding repeated IterTools.repeated
1031+
10221032
# End deprecations scheduled for 0.6

base/essentials.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ function tuple_type_tail(T::DataType)
6363
return Tuple{argtail(T.parameters...)...}
6464
end
6565

66+
tuple_type_cons{S}(::Type{S}, ::Type{Union{}}) = Union{}
67+
function tuple_type_cons{S,T<:Tuple}(::Type{S}, ::Type{T})
68+
@_pure_meta
69+
Tuple{S, T.parameters...}
70+
end
71+
6672
isvarargtype(t::ANY) = isa(t, DataType) && is((t::DataType).name, Vararg.name)
6773
isvatuple(t::DataType) = (n = length(t.parameters); n > 0 && isvarargtype(t.parameters[n]))
6874
unwrapva(t::ANY) = isvarargtype(t) ? t.parameters[1] : t
@@ -231,3 +237,5 @@ function vector_any(xs::ANY...)
231237
end
232238
a
233239
end
240+
241+
isempty(itr) = done(itr, start(itr))

base/exports.jl

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export
2323
Docs,
2424
Markdown,
2525
Threads,
26+
IterTools,
2627

2728
# Types
2829
AbstractChannel,
@@ -60,7 +61,6 @@ export
6061
Enumerate,
6162
Factorization,
6263
FileMonitor,
63-
Filter,
6464
FloatRange,
6565
Future,
6666
Hermitian,
@@ -123,7 +123,6 @@ export
123123
VersionNumber,
124124
WeakKeyDict,
125125
WorkerConfig,
126-
Zip,
127126

128127
# Ccall types
129128
Cchar,
@@ -958,16 +957,11 @@ export
958957

959958
# iteration
960959
done,
961-
enumerate,
962960
next,
963961
start,
962+
963+
enumerate, # re-exported from IterTools
964964
zip,
965-
rest,
966-
countfrom,
967-
take,
968-
drop,
969-
cycle,
970-
repeated,
971965

972966
# object identity and equality
973967
copy,

base/generator.jl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ result, and algorithms that resize their result incrementally.
5555
iteratorsize(x) = iteratorsize(typeof(x))
5656
iteratorsize(::Type) = HasLength() # HasLength is the default
5757

58-
and_iteratorsize{T}(isz::T, ::T) = isz
59-
and_iteratorsize(::HasLength, ::HasShape) = HasLength()
60-
and_iteratorsize(::HasShape, ::HasLength) = HasLength()
61-
and_iteratorsize(a, b) = SizeUnknown()
62-
6358
abstract IteratorEltype
6459
immutable EltypeUnknown <: IteratorEltype end
6560
immutable HasEltype <: IteratorEltype end
@@ -81,9 +76,6 @@ values.
8176
iteratoreltype(x) = iteratoreltype(typeof(x))
8277
iteratoreltype(::Type) = HasEltype() # HasEltype is the default
8378

84-
and_iteratoreltype{T}(iel::T, ::T) = iel
85-
and_iteratoreltype(a, b) = EltypeUnknown()
86-
8779
iteratorsize{T<:AbstractArray}(::Type{T}) = HasShape()
8880
iteratorsize{I,F}(::Type{Generator{I,F}}) = iteratorsize(I)
8981
length(g::Generator) = length(g.iter)

base/inference.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,8 @@ function abstract_apply(af::ANY, fargs, aargtypes::Vector{Any}, vtypes::VarTable
10171017
end
10181018

10191019
function pure_eval_call(f::ANY, argtypes::ANY, atype::ANY, vtypes::VarTable, sv::InferenceState)
1020-
for a in drop(argtypes,1)
1020+
for i = 2:length(argtypes)
1021+
a = argtypes[i]
10211022
if !(isa(a,Const) || isconstType(a,false))
10221023
return false
10231024
end
@@ -1053,7 +1054,7 @@ function pure_eval_call(f::ANY, argtypes::ANY, atype::ANY, vtypes::VarTable, sv:
10531054
return false
10541055
end
10551056

1056-
args = Any[ isa(a,Const) ? a.val : a.parameters[1] for a in drop(argtypes,1) ]
1057+
args = Any[ (a=argtypes[i]; isa(a,Const) ? a.val : a.parameters[1]) for i in 2:length(argtypes) ]
10571058
try
10581059
return abstract_eval_constant(f(args...))
10591060
catch

base/iterator.jl renamed to base/itertools.jl

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# This file is a part of Julia. License is MIT: http://julialang.org/license
22

3-
isempty(itr) = done(itr, start(itr))
3+
module IterTools
4+
5+
global Filter
6+
7+
import Base: start, done, next, isempty, length, size, eltype, iteratorsize, iteratoreltype, indices, ndims
8+
9+
using Base: tuple_type_cons, SizeUnknown, HasLength, HasShape, IsInfinite, EltypeUnknown, HasEltype, OneTo
10+
11+
export enumerate, zip, rest, countfrom, take, drop, cycle, repeated, product, flatten, partition
412

513
_min_length(a, b, ::IsInfinite, ::IsInfinite) = min(length(a),length(b)) # inherit behaviour, error
614
_min_length(a, b, A, ::IsInfinite) = length(a)
@@ -12,6 +20,14 @@ _diff_length(a, b, ::IsInfinite, ::IsInfinite) = 0
1220
_diff_length(a, b, ::IsInfinite, B) = length(a) # inherit behaviour, error
1321
_diff_length(a, b, A, B) = max(length(a)-length(b), 0)
1422

23+
and_iteratorsize{T}(isz::T, ::T) = isz
24+
and_iteratorsize(::HasLength, ::HasShape) = HasLength()
25+
and_iteratorsize(::HasShape, ::HasLength) = HasLength()
26+
and_iteratorsize(a, b) = SizeUnknown()
27+
28+
and_iteratoreltype{T}(iel::T, ::T) = iel
29+
and_iteratoreltype(a, b) = EltypeUnknown()
30+
1531
# enumerate
1632

1733
immutable Enumerate{I}
@@ -143,11 +159,6 @@ zip(a, b, c...) = Zip(a, zip(b, c...))
143159
length(z::Zip) = _min_length(z.a, z.z, iteratorsize(z.a), iteratorsize(z.z))
144160
size(z::Zip) = promote_shape(size(z.a), size(z.z))
145161
indices(z::Zip) = promote_shape(indices(z.a), indices(z.z))
146-
tuple_type_cons{S}(::Type{S}, ::Type{Union{}}) = Union{}
147-
function tuple_type_cons{S,T<:Tuple}(::Type{S}, ::Type{T})
148-
@_pure_meta
149-
Tuple{S, T.parameters...}
150-
end
151162
eltype{I,Z}(::Type{Zip{I,Z}}) = tuple_type_cons(eltype(I), eltype(Z))
152163
@inline start(z::Zip) = tuple(start(z.a), start(z.z))
153164
@inline function next(z::Zip, st)
@@ -245,42 +256,6 @@ rest_iteratorsize(::IsInfinite) = IsInfinite()
245256
iteratorsize{I,S}(::Type{Rest{I,S}}) = rest_iteratorsize(iteratorsize(I))
246257

247258

248-
"""
249-
head_and_tail(c, n) -> head, tail
250-
251-
Returns `head`: the first `n` elements of `c`;
252-
and `tail`: an iterator over the remaining elements.
253-
254-
```jldoctest
255-
julia> a = 1:10
256-
1:10
257-
258-
julia> b, c = Base.head_and_tail(a, 3)
259-
([1,2,3],Base.Rest{UnitRange{Int64},Int64}(1:10,4))
260-
261-
julia> collect(c)
262-
7-element Array{Any,1}:
263-
4
264-
5
265-
6
266-
7
267-
8
268-
9
269-
10
270-
```
271-
"""
272-
function head_and_tail(c, n)
273-
head = Vector{eltype(c)}(n)
274-
s = start(c)
275-
i = 0
276-
while i < n && !done(c, s)
277-
i += 1
278-
head[i], s = next(c, s)
279-
end
280-
return resize!(head, i), rest(c, s)
281-
end
282-
283-
284259
# Count -- infinite counting
285260

286261
immutable Count{S<:Number}
@@ -730,3 +705,5 @@ function next(itr::PartitionIterator, state)
730705
end
731706
return resize!(v, i), state
732707
end
708+
709+
end

base/multidimensional.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ end
381381
# and ensure the value to set is either an AbstractArray or a Repeated scalar
382382
# before redispatching to the _unsafe_batchsetindex!
383383
_iterable(v::AbstractArray) = v
384-
_iterable(v) = repeated(v)
384+
_iterable(v) = IterTools.repeated(v)
385385
@inline function _setindex!{T,N}(l::LinearIndexing, A::AbstractArray{T,N}, x, J::Vararg{Union{Real,AbstractArray,Colon},N})
386386
@boundscheck checkbounds(A, J...)
387387
_unsafe_setindex!(l, A, x, J...)

base/pkg/resolve.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function sanity_check(deps::Dict{String,Dict{VersionNumber,Available}},
6666

6767
vers = Array{Tuple{String,VersionNumber,VersionNumber}}(0)
6868
for (p,d) in deps, vn in keys(d)
69-
lvns = VersionNumber[filter(vn2->(vn2>vn), keys(d))...]
69+
lvns = VersionNumber[IterTools.filter(vn2->(vn2>vn), keys(d))...]
7070
nvn = isempty(lvns) ? typemax(VersionNumber) : minimum(lvns)
7171
push!(vers, (p,vn,nvn))
7272
end

base/pmap.jl

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,40 @@ function process_batch_errors!(p, f, results, on_error, retry_on, retry_n, retry
213213
nothing
214214
end
215215

216+
"""
217+
head_and_tail(c, n) -> head, tail
218+
219+
Returns `head`: the first `n` elements of `c`;
220+
and `tail`: an iterator over the remaining elements.
221+
222+
```jldoctest
223+
julia> a = 1:10
224+
1:10
225+
226+
julia> b, c = Base.head_and_tail(a, 3)
227+
([1,2,3],Base.IterTools.Rest{UnitRange{Int64},Int64}(1:10,4))
228+
229+
julia> collect(c)
230+
7-element Array{Any,1}:
231+
4
232+
5
233+
6
234+
7
235+
8
236+
9
237+
10
238+
```
239+
"""
240+
function head_and_tail(c, n)
241+
head = Vector{eltype(c)}(n)
242+
s = start(c)
243+
i = 0
244+
while i < n && !done(c, s)
245+
i += 1
246+
head[i], s = next(c, s)
247+
end
248+
return resize!(head, i), IterTools.rest(c, s)
249+
end
216250

217251
"""
218252
batchsplit(c; min_batch_count=1, max_batch_size=100) -> iterator
@@ -231,13 +265,13 @@ function batchsplit(c; min_batch_count=1, max_batch_size=100)
231265
end
232266

233267
# Split collection into batches, then peek at the first few batches
234-
batches = partition(c, max_batch_size)
268+
batches = IterTools.partition(c, max_batch_size)
235269
head, tail = head_and_tail(batches, min_batch_count)
236270

237271
# If there are not enough batches, use a smaller batch size
238272
if length(head) < min_batch_count
239273
batch_size = max(1, div(sum(length, head), min_batch_count))
240-
return partition(collect(flatten(head)), batch_size)
274+
return IterTools.partition(collect(flatten(head)), batch_size)
241275
end
242276

243277
return flatten((head, tail))

base/promotion.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ if isdefined(Core, :Inference)
226226
end
227227
function _promote_op(op, R::ANY, S::ANY)
228228
F = typeof(a -> op(a...))
229-
G = Tuple{Generator{Zip2{Tuple{R},Tuple{S}},F}}
229+
G = Tuple{Generator{IterTools.Zip2{Tuple{R},Tuple{S}},F}}
230230
return Core.Inference.return_type(first, G)
231231
end
232232
else

base/sysimg.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ include("bitarray.jl")
127127
include("intset.jl")
128128
include("dict.jl")
129129
include("set.jl")
130-
include("iterator.jl")
130+
include("itertools.jl")
131+
using .IterTools: zip, enumerate
132+
using .IterTools: Flatten, product # for generators
131133

132134
# Definition of StridedArray
133135
typealias StridedReshapedArray{T,N,A<:DenseArray} ReshapedArray{T,N,A}

src/julia-syntax.scm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2152,7 +2152,7 @@
21522152
(car ranges)
21532153
`(call (top product) ,@ranges)))
21542154
(iter (if filt?
2155-
`(call (top Filter)
2155+
`(call (|.| (top IterTools) 'Filter)
21562156
,(func-for-generator-ranges (cadr (caddr e)) range-exprs)
21572157
,iter)
21582158
iter)))

test/choosetests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function choosetests(choices = [])
2020
"dates", "dict", "hashing", "iobuffer", "staged", "offsetarray",
2121
"arrayops", "tuple", "reduce", "reducedim", "random", "abstractarray",
2222
"intfuncs", "simdloop", "vecelement", "blas", "sparse",
23-
"bitarray", "copy", "math", "fastmath", "functional",
23+
"bitarray", "copy", "math", "fastmath", "functional", "itertools",
2424
"operators", "path", "ccall", "parse", "loading", "bigint",
2525
"bigfloat", "sorting", "statistics", "spawn", "backtrace",
2626
"priorityqueue", "file", "read", "mmap", "version", "resolve",

0 commit comments

Comments
 (0)