Skip to content

Commit

Permalink
Merge pull request #5 from timholy/teh/julia-0.5
Browse files Browse the repository at this point in the history
Updates for julia 0.5
  • Loading branch information
mbauman committed Jul 22, 2016
2 parents fc4371c + b11d269 commit 10b0d3e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions REQUIRE
@@ -0,0 +1 @@
Compat
16 changes: 9 additions & 7 deletions src/implementation.jl
@@ -1,5 +1,7 @@
module Implementation

using Compat

import Base: isvatuple

check(T) = (T===Tuple || T===NTuple) && throw(ArgumentError("parameters of $T are undefined"))
Expand All @@ -8,7 +10,7 @@ check(T) = (T===Tuple || T===NTuple) && throw(ArgumentError("parameters of $T ar
const N=4
ie = :()
for p=0:N
params = ntuple(i->symbol(:P,i), p)
params = ntuple(i->Symbol(:P,i), p)
global tgetindex, tlength, concatenate

# Accessing by a constant value
Expand All @@ -24,16 +26,16 @@ for p=0:N
ie = :(ifelse(i == $p, $(params[p]), $ie))
end
@eval tgetindex{$(params...)}(t::Type{Tuple{$(params...)}}, i::Int) = (1 <= i <= $p || throw(BoundsError(t, i)); $ie)
# It'd be nice to simply define the constant `i` computation in the type
# It'd be nice to simply define the constant `i` computation in the type
# domain instead of relying upon dispatch, but it's not constant-folding:
# @eval getparam{$(params...), i}(t::Type{Tuple{$(params...)}}, ::Type{Val{i}}) = (1 <= i <= $p || throw(BoundsError(t, i)); $ie)

# concatenation
for q=0:N
qarams = ntuple(i->symbol(:Q,i), q)
qarams = ntuple(i->Symbol(:Q,i), q)
@eval concatenate{$(params...), $(qarams...)}(::Type{Tuple{$(params...)}},::Type{Tuple{$(qarams...)}}) = Tuple{$(params...), $(qarams...)}
end

# length
@eval tlength{$(params...)}(t::Type{Tuple{$(params...)}}) = $p
end
Expand Down Expand Up @@ -62,16 +64,16 @@ function tgetindex(T::Type, i::Integer)
end
end

tgetindex{I<:Integer}(T::Type, is::AbstractVector{I}) = Base.svec([tgetindex(T,i) for i in is]...)
tgetindex{I<:Integer}(T::Type, is::AbstractVector{I}) = Core.svec([tgetindex(T,i) for i in is]...)

function concatenate{T<:Tuple, S<:Tuple}(::Type{T}, ::Type{S})
check(T); check(S);
check(T); check(S);
isvatuple(T) && throw(ArgumentError("cannot concatenate the varargs tuple $T with $S"))
Tuple{T.parameters..., S.parameters...}
end

## Allow constructing Tuples like tuples with NTuple (akin to ntuple)
Base.call(::Type{NTuple}, f, n::Integer) = Tuple{ntuple(f, n)...}
@compat (::Type{NTuple}){F}(f::F, n::Integer) = Tuple{ntuple(f, n)...}


end # module
9 changes: 5 additions & 4 deletions test/runtests.jl
@@ -1,5 +1,6 @@
using Tuples
using Base.Test
using Compat: String

## Tuples.collect

Expand All @@ -18,7 +19,7 @@ using Base.Test
@test Tuples.getindex(Tuple{1,2,3}, Val{1}) === 1
@test Tuples.getindex(Tuple{1,2,3}, Val{2}) === 2
@test Tuples.getindex(Tuple{1,2,3}, Val{3}) === 3
@test Tuples.getindex(Tuple{1,2,3}, 2:3) === Base.svec(2,3)
@test Tuples.getindex(Tuple{1,2,3}, 2:3) === Core.svec(2,3)
@test_throws BoundsError Tuples.getindex(Tuple{1,2,3}, 0)
@test_throws BoundsError Tuples.getindex(Tuple{1,2,3}, 4)
@test_throws BoundsError Tuples.getindex(Tuple{1,2,3}, Val{0})
Expand All @@ -27,7 +28,7 @@ using Base.Test
@test Tuples.getindex(Tuple{Int, String}, 2) === String
@test Tuples.getindex(Tuple{Int, String}, Val{1}) === Int
@test Tuples.getindex(Tuple{Int, String}, Val{2}) === String
@test Tuples.getindex(Tuple{Int, String}, [2,1]) === Base.svec(String,Int)
@test Tuples.getindex(Tuple{Int, String}, [2,1]) === Core.svec(String,Int)
@test_throws BoundsError Tuples.getindex(Tuple{Int, String}, 0)
@test_throws BoundsError Tuples.getindex(Tuple{Int, String}, 3)
@test_throws BoundsError Tuples.getindex(Tuple{Int, String}, Val{0})
Expand All @@ -37,7 +38,7 @@ using Base.Test
@test Tuples.getindex(Tuple{Vararg{Int}}, Val{1}) === Int
@test Tuples.getindex(Tuple{Vararg{Int}}, Val{1000}) === Int
@test Tuples.getindex(Tuple{Vararg{Int}}, Val{10^10}) === Int
@test Tuples.getindex(Tuple{Vararg{Int}}, [10^10, 10^10+1]) === Base.svec(Int,Int)
@test Tuples.getindex(Tuple{Vararg{Int}}, [10^10, 10^10+1]) === Core.svec(Int,Int)
@test_throws BoundsError Tuples.getindex(Tuple{Vararg{Int}}, 0)
@test_throws BoundsError Tuples.getindex(Tuple{Vararg{Int}}, Val{0})
@test Tuples.getindex(Tuple{Int, Vararg{String}}, 1) === Int
Expand All @@ -46,7 +47,7 @@ using Base.Test
@test Tuples.getindex(Tuple{Int, Vararg{String}}, Val{1}) === Int
@test Tuples.getindex(Tuple{Int, Vararg{String}}, Val{2}) === String
@test Tuples.getindex(Tuple{Int, Vararg{String}}, Val{3}) === String
@test Tuples.getindex(Tuple{Int, Vararg{String}}, [1,3]) === Base.svec(Int,String)
@test Tuples.getindex(Tuple{Int, Vararg{String}}, [1,3]) === Core.svec(Int,String)
@test_throws BoundsError Tuples.getindex(Tuple{Int, Vararg{String}}, 0)
@test_throws BoundsError Tuples.getindex(Tuple{Int, Vararg{String}}, Val{0})

Expand Down

0 comments on commit 10b0d3e

Please sign in to comment.