Skip to content

Commit

Permalink
Add tuple-padding, fill_to_length(::Tuple, val, Val{N})
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Mar 11, 2016
1 parent 0a3ffac commit 345aa5b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
16 changes: 16 additions & 0 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ tails(t::Tuple, ts::Tuple...) = (tail(t), tails(ts...)...)
map(f, ::Tuple{}, ts::Tuple...) = ()
map(f, ts::Tuple...) = (f(heads(ts...)...), map(f, tails(ts...)...)...)

# type-stable padding
fill_to_length{N}(t::Tuple, val, ::Type{Val{N}}) = _ftl((), val, Val{N}, t...)
_ftl{N}(out::NTuple{N}, val, ::Type{Val{N}}) = out
function _ftl{N}(out::NTuple{N}, val, ::Type{Val{N}}, t...)
@_inline_meta
error("input tuple of length $(N+length(t)), requested $N")
end
function _ftl{N}(out, val, ::Type{Val{N}}, t1, t...)
@_inline_meta
_ftl((out..., t1), val, Val{N}, t...)
end
function _ftl{N}(out, val, ::Type{Val{N}})
@_inline_meta
_ftl((out..., val), val, Val{N})
end

## comparison ##

function isequal(t1::Tuple, t2::Tuple)
Expand Down
4 changes: 4 additions & 0 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@

@test getindex((5,6,7,8), []) === ()

## filling to specified length
@test @inferred(Base.fill_to_length((1,2,3), -1, Val{5})) == (1,2,3,-1,-1)
@test_throws ErrorException Base.fill_to_length((1,2,3), -1, Val{2})

## iterating ##
@test start((1,2,3)) === 1

Expand Down

0 comments on commit 345aa5b

Please sign in to comment.