From b176ee0242398dc470d43973b3f743487a497389 Mon Sep 17 00:00:00 2001 From: Eric Davies Date: Wed, 21 Sep 2016 11:35:57 -0500 Subject: [PATCH] Drop 0.3 and support 0.5 (#72) * 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 --- .travis.yml | 2 +- LICENSE.md | 5 +- README.md | 1 - REQUIRE | 2 +- src/Iterators.jl | 183 +++------------------------------------------ test/runtests.jl | 40 +++++++--- test/test.jl | 189 ----------------------------------------------- 7 files changed, 47 insertions(+), 375 deletions(-) delete mode 100644 test/test.jl diff --git a/.travis.yml b/.travis.yml index e0ea53d..6508998 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,8 +3,8 @@ os: - linux - osx julia: - - 0.3 - 0.4 + - 0.5 - nightly notifications: email: false diff --git a/LICENSE.md b/LICENSE.md index 536d2ba..712697e 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -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 diff --git a/README.md b/README.md index 0c0a72f..eed89fe 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/REQUIRE b/REQUIRE index df77a41..0cf4faf 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,2 +1,2 @@ -julia 0.3 +julia 0.4 Compat diff --git a/src/Iterators.jl b/src/Iterators.jl index 2d2f023..536608b 100644 --- a/src/Iterators.jl +++ b/src/Iterators.jl @@ -1,4 +1,4 @@ -VERSION >= v"0.4.0-dev+6521" && __precompile__() +__precompile__() module Iterators @@ -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. @@ -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...) @@ -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) @@ -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 @@ -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 @@ -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] @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 5e5cb3d..48bee2e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 # ------- @@ -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 # ---- @@ -123,7 +127,12 @@ end # Empty arrays test_imap( Any[], - @compat(Union{})[] + [] +) + +test_imap( + Any[], + Union{}[] ) # Simple operation @@ -158,7 +167,12 @@ end # Empty arrays test_groupby( - @compat(Union{})[], + [], + Any[] +) + +test_groupby( + Union{}[], Any[] ) @@ -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) @@ -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 # ---------- @@ -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 # ----- @@ -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 @@ -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 @@ -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 @@ -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)] diff --git a/test/test.jl b/test/test.jl deleted file mode 100644 index 1076cda..0000000 --- a/test/test.jl +++ /dev/null @@ -1,189 +0,0 @@ -using Iterators, Base.Test - -# count -# ----- - -i = 0 -for j = count(0, 2) - @test j == i*2 - i += 1 - i <= 10 || break -end - -# take -# ---- - -i = 0 -for j = take(0:2:8, 10) - @test j == i*2 - i += 1 -end -@test i == 5 - -i = 0 -for j = take(0:2:100, 10) - @test j == i*2 - i += 1 -end -@test i == 10 - -# drop -# ---- - -i = 0 -for j = drop(0:2:10, 2) - @test j == (i+2)*2 - i += 1 -end -@test i == 4 - -# cycle -# ----- - -i = 0 -for j = cycle(0:3) - @test j == i % 4 - i += 1 - i <= 10 || break -end - -# repeated -# -------- - -i = 0 -for j = repeated(1, 10) - @test j == 1 - i += 1 -end -@test i == 10 -i = 0 -for j = repeated(1) - @test j == 1 - i += 1 - i <= 10 || break -end - -# chain -# ----- - -@test collect(chain(1:2:5, 0.2:0.1:1.6)) == [1:2:5, 0.2:0.1:1.6] - -# product -# ------- - -x1 = 1:2:10 -x2 = 1:5 -@test collect(product(x1, x2)) == vec([(y1, y2) for y1 in x1, y2 in x2]) - -# distinct -# -------- - -x = [5, 2, 2, 1, 2, 1, 1, 2, 4, 2] -@test collect(distinct(x)) == unique(x) - -# partition -# --------- - -@test collect(partition(take(count(1), 6), 2)) == [(1,2), (3,4), (5,6)] -@test collect(partition(take(count(1), 4), 2, 1)) == [(1,2), (2,3), (3,4)] -@test collect(partition(take(count(1), 8), 2, 3)) == [(1,2), (4,5), (7,8)] - -# imap -# ---- - -function test_imap(expected, input...) - result = collect(imap(+, input...)) - @test result == expected -end - - -# Empty arrays -test_imap( - {}, - [] -) - -# Simple operation -test_imap( - {1,2,3}, - [1,2,3] -) - -# Multiple arguments -test_imap( - {5,7,9}, - [1,2,3], - [4,5,6] -) - -# Different-length arguments -test_imap( - {2,4,6}, - [1,2,3], - count(1) -) - - -# groupby -# ------- - -function test_groupby(input, expected) - result = collect(groupby(input, x -> x[1])) - @test result == expected -end - - -# Empty arrays -test_groupby( - [], - {} -) - -# Singletons -test_groupby( - ["xxx"], - {["xxx"]} -) - -# Typical operation -test_groupby( - ["face", "foo", "bar", "book", "baz"], - {["face", "foo"], ["bar", "book", "baz"]} -) - -# Trailing singletons -test_groupby( - ["face", "foo", "bar", "book", "baz", "xxx"], - {["face", "foo"], ["bar", "book", "baz"], ["xxx"]} -) - -# Leading singletons -test_groupby( - ["xxx", "face", "foo", "bar", "book", "baz"], - {["xxx"], ["face", "foo"], ["bar", "book", "baz"]} -) - -# Middle singletons -test_groupby( - ["face", "foo", "xxx", "bar", "book", "baz"], - {["face", "foo"], ["xxx"], ["bar", "book", "baz"]} -) - - -# subsets - -@test collect(subsets({})) == {{}} - -@test collect(subsets([:a])) == {Symbol[], Symbol[:a]} - -@test collect(subsets([:a, :b, :c])) == - {Symbol[], Symbol[:a], Symbol[:b], Symbol[:a, :b], Symbol[:c], - Symbol[:a, :c], Symbol[:b, :c], Symbol[:a, :b, :c]} - -# every n'th element -@test collect(everynth([], 10)) == [] -@test_throws ArgumentError everynth([], 0) -@test collect(everynth(10:20, 3)) == [12,15,18] -@test collect(everynth(10:20, 1)) == [10:20] - -