Skip to content

Instantly share code, notes, and snippets.

@thautwarm
Created April 1, 2020 10:33
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 thautwarm/d999cfee6b69e85dd5938cb468e91021 to your computer and use it in GitHub Desktop.
Save thautwarm/d999cfee6b69e85dd5938cb468e91021 to your computer and use it in GitHub Desktop.
use for static julia type utils
function sort_type(ts :: Vector{Type})
n = length(ts)
if n == 1
ts
elseif n == 2
a, b = ts[1], ts[2]
if a <: b
[a, b]
elseif b <: a
[b, a]
else
error("overlapping types $a ~ $b")
end
else
hd = ts[1]
gt = Type[]
lt = Type[]
eq = Type[hd]
for each in ts[2:end]
arr = if each == hd
eq
elseif each <: hd
lt
elseif each >: hd
gt
else
error("overlapping types $hd ~ $each")
end
push!(arr, each)
end
# [a;b] is array concat
[sort_type(lt);eq;sort_type(gt)]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment