Created
April 1, 2020 10:33
-
-
Save thautwarm/d999cfee6b69e85dd5938cb468e91021 to your computer and use it in GitHub Desktop.
use for static julia type utils
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
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