Skip to content

Instantly share code, notes, and snippets.

@JeffBezanson
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffBezanson/10612461 to your computer and use it in GitHub Desktop.
Save JeffBezanson/10612461 to your computer and use it in GitHub Desktop.
argtail(x, rest...) = rest
tail(x::Tuple) = argtail(x...)
# x >= y ? t : f
_cmp_ge(x::(), y::(), t, f) = t
_cmp_ge(x::(), y::Tuple, t, f) = f
_cmp_ge(x::Tuple, y::(), t, f) = t
_cmp_ge(x::Tuple, y::Tuple, t, f) = _cmp_ge(tail(x), tail(y), t, f)
typealias NN{n} NTuple{n,()}
_n{N}(::NN{N}) = N
_max{m,n}(a::NN{m}, b::NN{n}) = _cmp_ge(a, b, a, b)
_min{m,n}(a::NN{m}, b::NN{n}) = _cmp_ge(a, b, b, a)
_add{m,n}(a::NN{m}, b::NN{n}) = tuple(a...,b...)
_sub(::(), ::()) = ()
_sub(t::Tuple, ::()) = t
_sub(t::Tuple, s::Tuple) = _sub(tail(t), tail(s))
# example
foo{n,m}(::Array{Int,n}, ::Array{Int,m}) = bar(_add(NN{n},NN{m}))
# wrapped type constructor
# an NN must pass through a static parameter to become an integer again.
# unfortunately calling _n() does not work for this.
bar{n}(::NN{n}) = BitArray{n}
@code_typed foo([1,2], Array(Int,(1,1,1)))
# output:
#1-element Array{Any,1}:
# :($(Expr(:lambda, {:#s53,:#s52}, {{},{{:#s53,Array{Int64,1},0},{:#s52,Array{Int64,3},0}},{}}, :(begin # none, line 1:
# return BitArray{4}
# end::Type{BitArray{4}}))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment