Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add each-function #79

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions src/IterTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export
takewhile,
properties,
propertyvalues,
fieldvalues
fieldvalues,
each

function has_length(it)
it_size = IteratorSize(it)
Expand Down Expand Up @@ -590,14 +591,14 @@ iterate(it::StaticSizeBinomial{0}, state=false) = state ? nothing : ((), true)
pop(t::NTuple) = reverse(tail(reverse(t))), t[end]

function advance(it::StaticSizeBinomial{K}, idx) where {K}
xs = it.xs
lidx, i = pop(idx)
xs = it.xs
lidx, i = pop(idx)
i += 1
if i > length(xs) - K + length(idx)
lidx = advance(it, lidx)
i = lidx[end] + 1
end
return (lidx..., i)
if i > length(xs) - K + length(idx)
lidx = advance(it, lidx)
i = lidx[end] + 1
end
return (lidx..., i)
end
advance(it::StaticSizeBinomial, idx::NTuple{1}) = (idx[end]+1,)

Expand Down Expand Up @@ -1031,4 +1032,33 @@ function iterate(fs::FieldValues, state=1)
return (getfield(fs.x, state), state + 1)
end

"""
each(ElementType::Type, collection)

If `collection` can be `convert`ed into `ElementType`, it is converted and wrapped in a
`Tuple{ElementType}`. Otherwise, the *elements* of `collection` are converted into
`ElementType`.

# Examples
```jldoctest
julia> println.(each(Tuple, [(1, 2), (3, 4)]));
(1, 2)
(3, 4)

julia> println.(each(Tuple, (1, 2)));
(1, 2)

julia> println.(each(String, (1, 2)));
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type String
[...]
```
"""
function each(::Type{T},collection) where T
try
return (convert(T,collection),)
catch
return convert.(T,collection)
end
end

end # module IterTools
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ include("testing_macros.jl")
@test eltype(eltype(sk5)) == Int
@test length(collect(sk5)) == binomial(4, i)
end
@test iterate(subsets(collect(1:4),Val{2}()),(1,4)) == ((1,4), (2,3))

function collect_pairs(x)
p = Vector{NTuple{2, eltype(x)}}(undef, binomial(length(x), 2))
Expand Down Expand Up @@ -554,5 +555,12 @@ include("testing_macros.jl")
@test IteratorSize(typeof(g)) == IteratorSize(typeof(iter))
end
end

@testset "each" begin
@test each(Tuple,[(1,2),(1,2)]) == [(1,2),(1,2)]
@test each(Tuple,(1,2)) == ((1,2),)
@test_throws MethodError each(Tuple,["something"])
@test eltype(each(Int32,(1,2))) == Int32
end
end
end