项目作者: JuliaCollections

项目描述 :
Common functional iterator patterns. DEPRECATED in favour of IterTools.jl
高级语言: Julia
项目地址: git://github.com/JuliaCollections/Iterators.jl.git
创建时间: 2012-11-13T23:37:59Z
项目社区:https://github.com/JuliaCollections/Iterators.jl

开源协议:Other

下载


Iterators.jl

Iterators
Iterators

Build Status
Coverage Status

Common functional iterator patterns.

DEPRECATION

Iterators.jl has been deprecated in favour of IterTools.jl.
Please update your package dependencies: Iterators 0.3.1 maps to IterTools 0.1.0.

See #104 for more information.

Installation

Install this package with Pkg.add("Iterators")

Usage


  • takestrict(xs, n)

    Equivalent to take, but will throw an exception if fewer than n items
    are encountered in xs.

  • repeatedly(f, [n])

    Call a function n times, or infinitely if n is omitted.

    Example:

    1. for t in repeatedly(time_ns, 3)
    2. @show t
    3. end
    1. t = 0x0000592ff83caf87
    2. t = 0x0000592ff83d8cf4
    3. t = 0x0000592ff83dd11e
  • chain(xs…)

    Iterate through any number of iterators in sequence.

    Example:

    1. for i in chain(1:3, ['a', 'b', 'c'])
    2. @show i
    3. end
    1. i = 1
    2. i = 2
    3. i = 3
    4. i = 'a'
    5. i = 'b'
    6. i = 'c'
  • product(xs…)

    Iterate over all combinations in the cartesian product of the inputs.

    Example:

    1. for p in product(1:3,1:2)
    2. @show p
    3. end

    yields

    1. p = (1,1)
    2. p = (2,1)
    3. p = (3,1)
    4. p = (1,2)
    5. p = (2,2)
    6. p = (3,2)
  • distinct(xs)

    Iterate through values skipping over those already encountered.

    Example:

    1. for i in distinct([1,1,2,1,2,4,1,2,3,4])
    2. @show i
    3. end
    1. i = 1
    2. i = 2
    3. i = 4
    4. i = 3
  • nth(xs, n)

    Return the n’th element of xs. Mostly useful for non indexable collections.

    Example:

    1. nth(1:3, 3)
    1. 3
  • takenth(xs, n)

    Iterate through every n’th element of xs

    Example:

    1. collect(takenth(5:15,3))
    1. 3-element Array{Int32,1}:
    2. 7
    3. 10
    4. 13
  • partition(xs, n, [step])

    Group values into n-tuples.

    Example:

    1. for i in partition(1:9, 3)
    2. @show i
    3. end
    1. i = (1,2,3)
    2. i = (4,5,6)
    3. i = (7,8,9)

    If the step parameter is set, each tuple is separated by step values.

    Example:

    1. for i in partition(1:9, 3, 2)
    2. @show i
    3. end
    1. i = (1,2,3)
    2. i = (3,4,5)
    3. i = (5,6,7)
    4. i = (7,8,9)
  • groupby(f, xs)

    Group consecutive values that share the same result of applying f.

    Example:

    1. for i in groupby(x -> x[1], ["face", "foo", "bar", "book", "baz", "zzz"])
    2. @show i
    3. end
    1. i = ASCIIString["face","foo"]
    2. i = ASCIIString["bar","book","baz"]
    3. i = ASCIIString["zzz"]
  • imap(f, xs1, [xs2, …])

    Iterate over values of a function applied to successive values from one or
    more iterators.

    Example:

    1. for i in imap(+, [1,2,3], [4,5,6])
    2. @show i
    3. end
    1. i = 5
    2. i = 7
    3. i = 9
  • subsets(xs)

    Iterate over every subset of a collection xs.

    Example:

    1. for i in subsets([1,2,3])
    2. @show i
    3. end
    1. i = []
    2. i = [1]
    3. i = [2]
    4. i = [1,2]
    5. i = [3]
    6. i = [1,3]
    7. i = [2,3]
    8. i = [1,2,3]
  • subsets(xs, k)

    Iterate over every subset of size k from a collection xs.

    Example:

    1. for i in subsets([1,2,3],2)
    2. @show i
    3. end
    1. i = [1,2]
    2. i = [1,3]
    3. i = [2,3]
  • peekiter(xs)

    Add possibility to peek head element of an iterator without updating the state.

    Example:

    1. it = peekiter(["face", "foo", "bar", "book", "baz", "zzz"])
    2. s = start(it)
    3. @show peek(it, s)
    4. @show peek(it, s)
    5. x, s = next(it, s)
    6. @show x
    7. @show peek(it, s)
    1. peek(it,s) = Nullable("face")
    2. peek(it,s) = Nullable("face") # no change
    3. x = "face"
    4. peek(it,s) = Nullable("foo")
  • ncycle(xs,n)

    Cycles through an iterator n times

    Example:

    1. for i in ncycle(1:3, 2)
    2. @show i
    3. end
    1. i = 1
    2. i = 2
    3. i = 3
    4. i = 1
    5. i = 2
    6. i = 3
  • iterate(f, x)

    Iterate over successive applications of f, as in f(x), f(f(x)), f(f(f(x))), ....

    Example:

    1. for i in take(iterate(x -> 2x, 1), 5)
    2. @show i
    3. end
    1. i = 1
    2. i = 2
    3. i = 4
    4. i = 8
    5. i = 16

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:

  1. for (x,y) in zip(a,b)
  2. @show x,y
  3. end

the automatically inlined version can be obtained by simply doing:

  1. @itr for (x,y) in zip(a,b)
  2. @show x,y
  3. 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