IterTools
Installation
Install this package with Pkg.add("IterTools")
Usage
chain(xs...)
Iterate through any number of iterators in sequence.
IterTools.chain
— Function.chain(xs...)
Iterate through any number of iterators in sequence.
julia> for i in chain(1:3, ['a', 'b', 'c'])
@show i
end
i = 1
i = 2
i = 3
i = 'a'
i = 'b'
i = 'c'
distinct(xs)
Iterate through values skipping over those already encountered.
IterTools.distinct
— Function.distinct(xs)
Iterate through values skipping over those already encountered.
julia> for i in distinct([1,1,2,1,2,4,1,2,3,4])
@show i
end
i = 1
i = 2
i = 4
i = 3
groupby(f, xs)
Group consecutive values that share the same result of applying f
.
IterTools.groupby
— Function.groupby(f, xs)
Group consecutive values that share the same result of applying f
.
julia> for i in groupby(x -> x[1], ["face", "foo", "bar", "book", "baz", "zzz"])
@show i
end
i = String["face","foo"]
i = String["bar","book","baz"]
i = String["zzz"]
imap(f, xs1, [xs2, ...])
Iterate over values of a function applied to successive values from one or more iterators.
IterTools.imap
— Function.imap(f, xs1, [xs2, ...])
Iterate over values of a function applied to successive values from one or more iterators.
julia> for i in imap(+, [1,2,3], [4,5,6])
@show i
end
i = 5
i = 7
i = 9
iterate(f, x)
Iterate over successive applications of f
, as in x, f(x), f(f(x)), f(f(f(x))), ...
.
IterTools.iterate
— Function.iterate(f, x)
Iterate over successive applications of f
, as in x
, f(x)
, f(f(x))
, f(f(f(x)))
, ...
Use Base.take()
to obtain the required number of elements.
julia> for i in take(iterate(x -> 2x, 1), 5)
@show i
end
i = 1
i = 2
i = 4
i = 8
i = 16
julia> for i in take(iterate(sqrt, 100), 6)
@show i
end
i = 100
i = 10.0
i = 3.1622776601683795
i = 1.7782794100389228
i = 1.333521432163324
i = 1.1547819846894583
ncycle(xs, n)
Cycles through an iterator n
times.
IterTools.ncycle
— Function.ncycle(xs, n)
Cycle through iter
n
times.
julia> for i in ncycle(1:3, 2)
@show i
end
i = 1
i = 2
i = 3
i = 1
i = 2
i = 3
nth(xs, n)
Return the n
th element of xs
.
IterTools.nth
— Function.nth(xs, n)
Return the n
th element of xs
. This is mostly useful for non-indexable collections.
julia> mersenne = Set([3, 7, 31, 127])
Set([7,31,3,127])
julia> nth(mersenne, 3)
3
partition(xs, n, [step])
Group values into n
-tuples.
IterTools.partition
— Function.partition(xs, n, [step])
Group values into n
-tuples.
julia> for i in partition(1:9, 3)
@show i
end
i = (1,2,3)
i = (4,5,6)
i = (7,8,9)
If the step
parameter is set, each tuple is separated by step
values.
julia> for i in partition(1:9, 3, 2)
@show i
end
i = (1,2,3)
i = (3,4,5)
i = (5,6,7)
i = (7,8,9)
julia> for i in partition(1:9, 3, 3)
@show i
end
i = (1,2,3)
i = (4,5,6)
i = (7,8,9)
julia> for i in partition(1:9, 2, 3)
@show i
end
i = (1,2)
i = (4,5)
i = (7,8)
peekiter(xs)
Peek at the head element of an iterator without updating the state.
IterTools.peekiter
— Function.peekiter(xs)
Lets you peek at the head element of an iterator without updating the state.
julia> it = peekiter(["face", "foo", "bar", "book", "baz", "zzz"])
IterTools.PeekIter{Array{String,1}}(String["face","foo","bar","book","baz","zzz"])
julia> s = start(it)
(2,Nullable{String}("face"))
julia> @show peek(it, s)
peek(it,s) = Nullable{String}("face")
Nullable{String}("face")
julia> @show peek(it, s)
peek(it,s) = Nullable{String}("face")
Nullable{String}("face")
julia> x, s = next(it, s)
("face",(3,Nullable{String}("foo"),false))
julia> @show x
x = "face"
"face"
julia> @show peek(it, s)
peek(it,s) = Nullable{String}("foo")
Nullable{String}("foo")
product(xs...)
Iterate over all combinations in the Cartesian product of the inputs.
IterTools.product
— Function.product(xs...)
Iterate over all combinations in the Cartesian product of the inputs.
julia> for p in product(1:3,4:5)
@show p
end
p = (1,4)
p = (2,4)
p = (3,4)
p = (1,5)
p = (2,5)
p = (3,5)
repeatedly(f, [n])
Call a function n
times, or infinitely if n
is omitted.
IterTools.repeatedly
— Function.repeatedly(f, n)
Call function f
n
times, or infinitely if n
is omitted.
julia> t() = (sleep(0.1); Dates.millisecond(now()))
t (generic function with 1 method)
julia> collect(repeatedly(t, 5))
5-element Array{Any,1}:
993
97
200
303
408
takenth(xs, n)
Iterate through every n'th element of xs
IterTools.takenth
— Function.takenth(xs, n)
Iterate through every n
th element of xs
.
julia> collect(takenth(5:15,3))
3-element Array{Int64,1}:
7
10
13
subsets(xs, [k])
Iterate over every subset of a collection xs
, or iterate over every subset of size k
from a collection xs
.
IterTools.subsets
— Function.subsets(xs)
subsets(xs, k)
Iterate over every subset of the collection xs
. You can restrict the subsets to a specific size k
.
julia> for i in subsets([1, 2, 3])
@show i
end
i = Int64[]
i = [1]
i = [2]
i = [1,2]
i = [3]
i = [1,3]
i = [2,3]
i = [1,2,3]
julia> for i in subsets(1:4, 2)
@show i
end
i = [1,2]
i = [1,3]
i = [1,4]
i = [2,3]
i = [2,4]
i = [3,4]
takestrict(xs, n)
Equivalent to take
, but will throw an exception if fewer than n
items are encountered in xs
.
IterTools.takestrict
— Function.takestrict(xs, n::Int)
Like take()
, an iterator that generates at most the first n
elements of xs
, but throws an exception if fewer than n
items are encountered in xs
.
julia> a = :1:2:11
1:2:11
julia> collect(takestrict(a, 3))
3-element Array{Int64,1}:
1
3
5
The @itr
macro for automatic inlining in for
loops
Using functional iterators is powerful and concise, but may incur in some overhead, and manually inlining the operations can typically improve performance in critical parts of the code. The @itr
macro is provided to do that automatically in some cases.
Its usage is trivial: for example, given this code:
for (x,y) in zip(a,b)
@show x,y
end
the automatically inlined version can be obtained by simply doing:
@itr for (x,y) in zip(a,b)
@show x,y
end
This typically results in faster code, but its applicability has limitations:
it only works with
for
loops;if multiple nested iterators are used, only the outermost is affected by the transformation;
explicit expressions are required (i.e. when a
Tuple
is expected, an explicit tuple must be provided, a tuple variable won't be accepted);splicing is not supported;
multidimensional loops (i.e. expressions such as
for x in a, y in b
) are not supported
The @itr
macro can be used with the following supported iterators:
zip
enumerate
take
takestrict
drop
chain
IterTools.@itr
— Macro.@itr(ex)
The @itr
macro automaticaly inlines some iterators in for
loops, to produce faster code.
The macro can be used with the following supported iterators: zip()
, enumerate()
, take()
, takestrict()
, drop()
, and chain()
.
julia> for (x,y) in zip(1:3, 4:6)
@show x,y
end
(x,y) = (1,4)
(x,y) = (2,5)
(x,y) = (3,6)
julia> @itr for (x,y) in zip(1:3, 4:6)
@show x,y
end
(x,y) = (1,4)
(x,y) = (2,5)
(x,y) = (3,6)