Skip to content
This repository has been archived by the owner on Sep 1, 2020. It is now read-only.

Commit

Permalink
Drop 0.3 and support 0.5 (#72)
Browse files Browse the repository at this point in the history
* Removed old test file

* Drop support for 0.3

* Update copyright

* Improve coverage

* Turn back the clock on Any[...] --> [...]

* Test using both Any[] and Union{}[]

* Add back more array types

* Missed one
  • Loading branch information
iamed2 authored Sep 21, 2016
1 parent 3f3969d commit b176ee0
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 375 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ os:
- linux
- osx
julia:
- 0.3
- 0.4
- 0.5
- nightly
notifications:
email: false
5 changes: 3 additions & 2 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Iterators.jl is licensed under the MIT License:

> Copyright (c) 2012-2013: Daniel Jones, Stefan Karpinski, Simon Kornblith,
> Kevin Squire, Jeff Bezanson, Tim Holy, Jonathan Malmaud, and other contributors.
> Copyright (c) 2012-2016: Daniel Jones, Stefan Karpinski, Simon Kornblith,
> Kevin Squire, Jeff Bezanson, Tim Holy, Jonathan Malmaud, Eric Davies, and
> other contributors.
> Permission is hereby granted, free of charge, to any person obtaining
> a copy of this software and associated documentation files (the
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Iterators.jl

[![Iterators](http://pkg.julialang.org/badges/Iterators_0.3.svg)](http://pkg.julialang.org/?pkg=Iterators&ver=0.3)
[![Iterators](http://pkg.julialang.org/badges/Iterators_0.4.svg)](http://pkg.julialang.org/?pkg=Iterators&ver=0.4)
[![Iterators](http://pkg.julialang.org/badges/Iterators_0.5.svg)](http://pkg.julialang.org/?pkg=Iterators&ver=0.5)

Expand Down
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
julia 0.3
julia 0.4
Compat
183 changes: 12 additions & 171 deletions src/Iterators.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION >= v"0.4.0-dev+6521" && __precompile__()
__precompile__()

module Iterators

Expand Down Expand Up @@ -32,154 +32,11 @@ end


# Some iterators have been moved into Base (and count has been renamed as well)
if VERSION >= v"0.4.0-dev+3323"

import Base: count
Base.@deprecate count(start::Number, step::Number) countfrom(start, step)
Base.@deprecate count(start::Number) countfrom(start)
Base.@deprecate count() countfrom()
else

import Base: take, count

export
countfrom,
count,
take,
drop,
cycle,
repeated

# Infinite counting

immutable Count{S<:Number}
start::S
step::S
end

eltype{S}(it::Count{S}) = S

count(start::Number, step::Number) = Count(promote(start, step)...)
count(start::Number) = Count(start, one(start))
count() = Count(0, 1)

# Deprecate on 0.3 as well?
countfrom(start::Number, step::Number) = count(start, step)
countfrom(start::Number) = count(start)
countfrom() = count(1)

start(it::Count) = it.start
next(it::Count, state) = (state, state + it.step)
done(it::Count, state) = false


# Iterate through the first n elements

immutable Take{I}
xs::I
n::Int
end

eltype(it::Take) = eltype(it.xs)

take(xs, n::Int) = Take(xs, n)

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


# Iterator through all but the first n elements

immutable Drop{I}
xs::I
n::Int
end

eltype(it::Drop) = eltype(it.xs)

drop(xs, n::Int) = Drop(xs, n)

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

eltype(it::Cycle) = eltype(it.xs)

cycle(xs) = Cycle(xs)

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 n (or infinitely many) times.

immutable Repeat{O}
x::O
n::Int
end

eltype{O}(it::Repeat{O}) = O
length(it::Repeat) = it.n

repeated(x, n) = Repeat(x, n)

start(it::Repeat) = it.n
next(it::Repeat, state) = (it.x, state - 1)
done(it::Repeat, state) = state <= 0


immutable RepeatForever{O}
x::O
end

eltype{O}(r::RepeatForever{O}) = O

repeated(x) = RepeatForever(x)

start(it::RepeatForever) = nothing
next(it::RepeatForever, state) = (it.x, nothing)
done(it::RepeatForever, state) = false
end
import Base: count
Base.@deprecate count(start::Number, step::Number) countfrom(start, step)
Base.@deprecate count(start::Number) countfrom(start)
Base.@deprecate count() countfrom()

# Iterate through the first n elements, throwing an exception if
# fewer than n items ar encountered.
Expand Down Expand Up @@ -305,13 +162,7 @@ immutable Product
end
iteratorsize{T<:Product}(::Type{T}) = SizeUnknown()

# Using @compat causes error JuliaLang/Compat.jl#81
# eltype(p::Product) = @compat(Tuple{map(eltype, p.xss)...})
if VERSION >= v"0.4-dev"
eltype(p::Product) = Tuple{map(eltype, p.xss)...}
else
eltype(p::Product) = tuple(map(eltype, p.xss)...)
end
eltype(p::Product) = Tuple{map(eltype, p.xss)...}
length(p::Product) = mapreduce(length, *, 1, p.xss)

product(xss...) = Product(xss...)
Expand Down Expand Up @@ -407,13 +258,7 @@ immutable Partition{I}
end
iteratorsize{T<:Partition}(::Type{T}) = SizeUnknown()

# Using @compat causes error JuliaLang/Compat.jl#81
# eltype(it::Partition) = @compat(Tuple{fill(eltype(it.xs),it.n)...})
if VERSION >= v"0.4-dev"
eltype(it::Partition) = Tuple{fill(eltype(it.xs),it.n)...}
else
eltype(it::Partition) = tuple(fill(eltype(it.xs),it.n)...)
end
eltype(it::Partition) = Tuple{fill(eltype(it.xs),it.n)...}

function partition(xs, n::Int)
Partition(xs, n, n)
Expand Down Expand Up @@ -629,7 +474,7 @@ subsets(xs,k) = Binomial(xs,length(xs),k)

start(it::Binomial) = (collect(Int64, 1:it.k), false)

function next(it::Binomial, state::(@compat Tuple{Array{Int64,1}, Bool}))
function next(it::Binomial, state::Tuple{Array{Int64,1}, Bool})
idx = state[1]
set = it.xs[idx]
i = it.k
Expand All @@ -649,15 +494,11 @@ function next(it::Binomial, state::(@compat Tuple{Array{Int64,1}, Bool}))
end
end

done(it::Binomial, state::(@compat Tuple{Array{Int64,1}, Bool})) = state[2]
done(it::Binomial, state::Tuple{Array{Int64,1}, Bool}) = state[2]


# nth : return the nth element in a collection

if VERSION < v"0.4"
Core.BoundsError(xs, n) = Core.BoundsError()
end

function nth(xs, n::Integer)
n > 0 || throw(BoundsError(xs, n))
# catch, if possible
Expand All @@ -670,8 +511,8 @@ function nth(xs, n::Integer)
i == n && return val
end
# catch iterators with no length but actual finite size less then n
throw(BoundsError(xs, n))
end
throw(BoundsError(xs, n))
end

nth(xs::AbstractArray, n::Integer) = xs[n]

Expand Down Expand Up @@ -731,7 +572,7 @@ iteratorsize{T<:Iterate}(::Type{T}) = SizeUnknown()
iterate(f, seed) = Iterate(f, seed)
start(it::Iterate) = it.seed
next(it::Iterate, state) = (state, it.f(state))
@compat done(it::Iterate, state) = (state==Union{})
done(it::Iterate, state) = (state==Union{})

using Base.Meta

Expand Down
40 changes: 30 additions & 10 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ end
# -----

@test collect(chain(1:2:5, 0.2:0.1:1.6)) == [1:2:5; 0.2:0.1:1.6]
@test collect(chain(1:0, 1:2:5, 0.2:0.1:1.6)) == [1:2:5; 0.2:0.1:1.6]

# product
# -------
Expand All @@ -110,6 +111,9 @@ x = [5, 2, 2, 1, 2, 1, 1, 2, 4, 2]
@test collect(partition(take(countfrom(1), 6), 2)) == [(1,2), (3,4), (5,6)]
@test collect(partition(take(countfrom(1), 4), 2, 1)) == [(1,2), (2,3), (3,4)]
@test collect(partition(take(countfrom(1), 8), 2, 3)) == [(1,2), (4,5), (7,8)]
@test collect(partition(take(countfrom(1), 0), 2, 1)) == []

@test_throws ArgumentError partition(take(countfrom(1), 8), 2, 0)

# imap
# ----
Expand All @@ -123,7 +127,12 @@ end
# Empty arrays
test_imap(
Any[],
@compat(Union{})[]
[]
)

test_imap(
Any[],
Union{}[]
)

# Simple operation
Expand Down Expand Up @@ -158,7 +167,12 @@ end

# Empty arrays
test_groupby(
@compat(Union{})[],
[],
Any[]
)

test_groupby(
Union{}[],
Any[]
)

Expand Down Expand Up @@ -224,12 +238,12 @@ for xs in Any[[1, 2, 3], 1:3, reshape(1:3, 3, 1)]
@test nth(xs, 3) == 3
@test_throws BoundsError nth(xs, 0)
@test_throws BoundsError nth(xs, 4)
end
end

for xs in Any[take(1:3, 3), drop(-1:3, 2)]
@test nth(xs, 3) == 3
@test_throws BoundsError nth(xs, 0)
end
end

s = subsets([1, 2, 3])
@test_throws BoundsError nth(s, 0)
Expand Down Expand Up @@ -271,7 +285,8 @@ end

@test_zip [1,2,3] [:a, :b, :c] ['x', 'y', 'z']
@test_zip [1,2,3] [:a, :b] ['w', 'x', 'y', 'z']
@test_zip [1,2,3] @compat(Union{})[] ['w', 'x', 'y', 'z']
@test_zip [1,2,3] Any[] ['w', 'x', 'y', 'z']
@test_zip [1,2,3] Union{}[] ['w', 'x', 'y', 'z']

# @enumerate
# ----------
Expand All @@ -294,7 +309,8 @@ macro test_enumerate(input)
end

@test_enumerate [:a, :b, :c]
@test_enumerate @compat(Union{})[]
@test_enumerate Union{}[]
@test_enumerate Any[]

# @take
# -----
Expand All @@ -318,7 +334,8 @@ end
@test_take [:a, :b, :c] 2
@test_take [:a, :b, :c] 5
@test_take [:a, :b, :c] 0
@test_take @compat(Union{})[] 2
@test_take Any[] 2
@test_take Union{}[] 2
@test_take Any[] 0
@test_take [(:a,1), (:b,2), (:c,3)] 2

Expand Down Expand Up @@ -357,7 +374,8 @@ end
@test_takestrict [:a, :b, :c] 3
@test_takestrict [:a, :b, :c] 5
@test_takestrict [:a, :b, :c] 0
@test_takestrict @compat(Union{})[] 2
@test_takestrict Any[] 2
@test_takestrict Union{}[] 2
@test_takestrict Any[] 0
@test_takestrict [(:a,1), (:b,2), (:c,3)] 2
@test_takestrict [(:a,1), (:b,2), (:c,3)] 3
Expand Down Expand Up @@ -385,7 +403,8 @@ end
@test_drop [:a, :b, :c] 2
@test_drop [:a, :b, :c] 5
@test_drop [:a, :b, :c] 0
@test_drop @compat(Union{})[] 2
@test_drop Any[] 2
@test_drop Union{}[] 2
@test_drop Any[] 0
@test_drop [(:a,1), (:b,2), (:c,3)] 2

Expand All @@ -411,6 +430,7 @@ end

@test_chain [1,2,3] [:a, :b, :c] ['x', 'y', 'z']
@test_chain [1,2,3] [:a, :b] ['w', 'x', 'y', 'z']
@test_chain [1,2,3] @compat(Union{})[] ['w', 'x', 'y', 'z']
@test_chain [1,2,3] Any[] ['w', 'x', 'y', 'z']
@test_chain [1,2,3] Union{}[] ['w', 'x', 'y', 'z']
@test_chain [1,2,3] 4 [('w',3), ('x',2), ('y',1), ('z',0)]

Loading

0 comments on commit b176ee0

Please sign in to comment.