From a977e34b04fadb135bc8c209499652a777e81443 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Fri, 3 Jul 2015 10:51:33 +0100 Subject: [PATCH] Create AlephNull type for length of unbounded iterators. --- base/exports.jl | 2 ++ base/iterator.jl | 39 ++++++++++++++++++++++++++++++++++++--- test/functional.jl | 14 ++++++++------ 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/base/exports.jl b/base/exports.jl index 2971b68c4194a..78db50604ec53 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -28,6 +28,7 @@ export AbstractSparseVector, AbstractVector, AbstractVecOrMat, + AlephNull, Array, Associative, Bidiagonal, @@ -198,6 +199,7 @@ export catalan, φ, golden, I, + ℵ₀, # Operators !, diff --git a/base/iterator.jl b/base/iterator.jl index 4fb2bcb8c34a5..75f83e5b33941 100644 --- a/base/iterator.jl +++ b/base/iterator.jl @@ -1,5 +1,38 @@ # This file is a part of Julia. License is MIT: http://julialang.org/license +# For unbounded length iterators +immutable AlephNull <: Number +end + +show(io::IO, ::AlephNull) = print(io, ℵ₀) +const ℵ₀ = AlephNull() + +==(::Integer,::AlephNull) = false +==(::AlephNull,::Integer) = false +==(::AlephNull,::AlephNull) = true + +<(::Integer,::AlephNull) = true +<(::AlephNull,::Integer) = false +<(::AlephNull,::AlephNull) = false + +<=(::Integer,::AlephNull) = true +<=(::AlephNull,::Integer) = false +<=(::AlephNull,::AlephNull) = true + +min(x::Integer, ::AlephNull) = x +min(::AlephNull, x::Integer) = x +min(::AlephNull, ::AlephNull) = AlephNull() + +max(x::Integer, ::AlephNull) = AlephNull() +max(::AlephNull, ::Integer) = AlephNull() +max(::AlephNull, ::AlephNull) = AlephNull() + ++(x::Integer, ::AlephNull) = AlephNull() ++(::AlephNull, ::Integer) = AlephNull() ++(::AlephNull, ::AlephNull) = AlephNull() + +-(::AlephNull, ::Integer) = AlephNull() + isempty(itr) = done(itr, start(itr)) # enumerate @@ -120,7 +153,7 @@ countfrom(start::Number) = Count(start, one(start)) countfrom() = Count(1, 1) eltype{S}(it::Count{S}) = S -length(it::Count) = Inf +length(it::Count) = AlephNull() start(it::Count) = it.start next(it::Count, state) = (state, state + it.step) @@ -184,7 +217,7 @@ end cycle(xs) = Cycle(xs) eltype(it::Cycle) = eltype(it.xs) -length(it::Cycle) = Inf +length(it::Cycle) = AlephNull() function start(it::Cycle) s = start(it.xs) @@ -209,7 +242,7 @@ immutable Repeated{O} end repeated(x) = Repeated(x) eltype{O}(r::Repeated{O}) = O -length(x::Repeated) = Inf +length(x::Repeated) = AlephNull() start(it::Repeated) = nothing next(it::Repeated, state) = (it.x, nothing) diff --git a/test/functional.jl b/test/functional.jl index db7fe874204d3..ed16f7c127fc1 100644 --- a/test/functional.jl +++ b/test/functional.jl @@ -48,8 +48,10 @@ let b = IOBuffer("1\n2\n3\n"), a = [] push!(a, (i,x)) end @test a == [(1,"1\n"),(2,"2\n"),(3,"3\n")] - @test length(enumerate(eachline(b))) == 3 end +@test length(enumerate(1:3)) == 3 +@test length(repeated(0)) == ℵ₀ + # zip eachline (issue #7369) let zeb = IOBuffer("1\n2\n3\n4\n5\n"), @@ -83,8 +85,8 @@ let i = 0 i <= 10 || break end end -@test length(countfrom(0,2)) == Inf -@test length(countfrom(0)) == Inf +@test length(countfrom(0,2)) == ℵ₀ +@test length(countfrom(0)) == ℵ₀ # take # ---- @@ -124,7 +126,7 @@ end @test length(drop(1:10,5)) == 5 @test length(drop(1:10,15)) == 0 -@test length(drop(countfrom(0),5)) == Inf +@test length(drop(countfrom(0),5)) == ℵ₀ # cycle @@ -138,7 +140,7 @@ let i = 0 end end -@test length(cycle(0:3)) == Inf +@test length(cycle(0:3)) == ℵ₀ # repeated # -------- @@ -158,5 +160,5 @@ let i = 0 end end -@test length(repeated(1)) == Inf +@test length(repeated(1)) == ℵ₀ @test length(repeated(1,10)) == 10