diff options
Diffstat (limited to 'source/lux')
-rw-r--r-- | source/lux/data/cont.lux | 38 | ||||
-rw-r--r-- | source/lux/data/either.lux | 42 | ||||
-rw-r--r-- | source/lux/data/error.lux | 31 | ||||
-rw-r--r-- | source/lux/data/list.lux | 24 |
4 files changed, 52 insertions, 83 deletions
diff --git a/source/lux/data/cont.lux b/source/lux/data/cont.lux deleted file mode 100644 index 2c55eb641..000000000 --- a/source/lux/data/cont.lux +++ /dev/null @@ -1,38 +0,0 @@ -## 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/. - -(;import lux - (lux/control (functor #as F #refer #all) - (monad #as M #refer #all))) - -## [Types] -(deftype #export (Cont r a) - (-> (-> a r) r)) - -## [Structures] -(defstruct #export Cont/Functor (All [r] - (Functor (Cont r))) - (def (F;map f fa) - (lambda [k] - (k (fa f))))) - -(defstruct #export Cont/Monad (All [r] - (Monad (Cont r))) - (def M;_functor Cont/Functor) - - (def (M;wrap x) - (lambda [k] - (k x))) - - (def (M;join mma) - (lambda [k] - (mma (lambda [ma] (ma k)))))) - -## [Functions] -(def #export (call/cc body) - (All [r a b] - (-> (-> (-> a (Cont r b)) (Cont r a)) (Cont r a))) - (lambda [k] - (body k))) diff --git a/source/lux/data/either.lux b/source/lux/data/either.lux index a945c32b9..86d778965 100644 --- a/source/lux/data/either.lux +++ b/source/lux/data/either.lux @@ -4,7 +4,9 @@ ## You can obtain one at http://mozilla.org/MPL/2.0/. (;import lux - (lux/data (list #refer (#exclude partition)))) + (lux (control (functor #as F #refer #all) + (monad #as M #refer #all)) + (data (list #refer (#exclude partition))))) ## [Types] ## (deftype (Either l r) @@ -30,14 +32,32 @@ [rights b #;Right] ) -(def #export (partition es) +(def #export (partition xs) (All [a b] (-> (List (Either a b)) (, (List a) (List b)))) - (foldL (: (All [a b] - (-> (, (List a) (List b)) (Either a b) (, (List a) (List b)))) - (lambda [tails e] - (let [[ltail rtail] tails] - (case e - (#;Left x) [(#;Cons [x ltail]) rtail] - (#;Right x) [ltail (#;Cons [x rtail])])))) - [(list) (list)] - (reverse es))) + (case xs + #;Nil + [#;Nil #;Nil] + + (#;Cons x xs') + (let [[lefts rights] (partition xs')] + (case x + (#;Left x') [(#;Cons x' lefts) rights] + (#;Right x') [lefts (#;Cons x' rights)])))) + +## [Structures] +(defstruct #export Error/Functor (All [a] (Functor (Either a))) + (def (F;map f ma) + (case ma + (#;Left msg) (#;Left msg) + (#;Right datum) (#;Right (f datum))))) + +(defstruct #export Error/Monad (All [a] (Monad (Either a))) + (def M;_functor Error/Functor) + + (def (M;wrap a) + (#;Right a)) + + (def (M;join mma) + (case mma + (#;Left msg) (#;Left msg) + (#;Right ma) ma))) diff --git a/source/lux/data/error.lux b/source/lux/data/error.lux deleted file mode 100644 index 9c595144b..000000000 --- a/source/lux/data/error.lux +++ /dev/null @@ -1,31 +0,0 @@ -## 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/. - -(;import lux - (lux/control (functor #as F #refer #all) - (monad #as M #refer #all))) - -## [Types] -(deftype #export (Error a) - (| (#Fail Text) - (#Ok a))) - -## [Structures] -(defstruct #export Error/Functor (Functor Error) - (def (F;map f ma) - (case ma - (#Fail msg) (#Fail msg) - (#Ok datum) (#Ok (f datum))))) - -(defstruct #export Error/Monad (Monad Error) - (def M;_functor Error/Functor) - - (def (M;wrap a) - (#Ok a)) - - (def (M;join mma) - (case mma - (#Fail msg) (#Fail msg) - (#Ok ma) ma))) diff --git a/source/lux/data/list.lux b/source/lux/data/list.lux index a4a6a6d0e..1277fc6ae 100644 --- a/source/lux/data/list.lux +++ b/source/lux/data/list.lux @@ -8,6 +8,7 @@ (functor #as F #refer #all) (monad #as M #refer #all) (eq #as E) + (ord #as O) (dict #as D #refer #all) (stack #as S)) (data (number (int #open ("i" Int/Number Int/Ord))) @@ -248,9 +249,12 @@ ## [#;Nil #;Nil] ## true -## [(#;Cons [x xs']) (#;Cons [y ys'])] +## [(#;Cons x xs') (#;Cons y ys')] ## (and (:: eq (E;= x y)) ## (E;= xs' ys')) + +## [_ _] +## false ## ))) (defstruct #export List/Monoid (All [a] @@ -258,8 +262,8 @@ (def m;unit #;Nil) (def (m;++ xs ys) (case xs - #;Nil ys - (#;Cons [x xs']) (#;Cons [x (++ xs' ys)])))) + #;Nil ys + (#;Cons x xs') (#;Cons x (++ xs' ys))))) (defstruct #export List/Functor (Functor List) (def (F;map f ma) @@ -327,3 +331,17 @@ (case xs #;Nil #;None (#;Cons x xs') (#;Some x)))) + +## [Functions] +(def #export (sort ord xs) + (All [a] (-> (O;Ord a) (List a) (List a))) + (case xs + #;Nil + #;Nil + + (#;Cons x xs') + (using ord + (let [pre (filter (>= x) xs') + post (filter (< x) xs') + ++ (:: List/Monoid m;++)] + ($ ++ (sort ord pre) (list x) (sort ord post)))))) |