diff options
Diffstat (limited to 'stdlib/source/lux/data/sum.lux')
-rw-r--r-- | stdlib/source/lux/data/sum.lux | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/stdlib/source/lux/data/sum.lux b/stdlib/source/lux/data/sum.lux new file mode 100644 index 000000000..f01d88727 --- /dev/null +++ b/stdlib/source/lux/data/sum.lux @@ -0,0 +1,45 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + +(;module: lux) + +## [Values] +(do-template [<name> <type> <index>] + [(def: #export (<name> value) + (All [a b] (-> <type> (| a b))) + (<index> value))] + + [left a +0] + [right b +1]) + +(def: #export (either f g s) + (All [a b c] (-> (-> a c) (-> b c) (| a b) c)) + (case s + (+0 x) (f x) + (+1 x) (g x))) + +(do-template [<name> <side> <tag>] + [(def: #export (<name> es) + (All [a b] (-> (List (| a b)) (List <side>))) + (case es + #;Nil #;Nil + (#;Cons (<tag> x) es') (#;Cons [x (<name> es')]) + (#;Cons _ es') (<name> es')))] + + [lefts a +0] + [rights b +1] + ) + +(def: #export (partition xs) + (All [a b] (-> (List (| a b)) [(List a) (List b)])) + (case xs + #;Nil + [#;Nil #;Nil] + + (#;Cons x xs') + (let [[lefts rights] (partition xs')] + (case x + (+0 x') [(#;Cons x' lefts) rights] + (+1 x') [lefts (#;Cons x' rights)])))) |