Last active
August 29, 2015 13:59
-
-
Save JeffBezanson/10612461 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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