diff options
Diffstat (limited to 'source/lux/codata/stream.lux')
-rw-r--r-- | source/lux/codata/stream.lux | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/source/lux/codata/stream.lux b/source/lux/codata/stream.lux index 96de64fd4..1306e3d8b 100644 --- a/source/lux/codata/stream.lux +++ b/source/lux/codata/stream.lux @@ -9,11 +9,13 @@ (comonad #as CM #refer #all)) (meta lux syntax) - (data (list #as l #refer (#only @list @list& List/Monad)) + (data (list #as l #refer (#only @list @list& List/Monad) #open ("" List/Fold)) (number (int #open ("i" Int/Number Int/Ord))) bool) (codata (lazy #as L #refer #all)))) +(open List/Monad "list:") + ## [Types] (deftype #export (Stream a) (Lazy (, a (Stream a)))) @@ -117,15 +119,22 @@ (def _functor Stream/Functor) (def unwrap head) (def (split wa) - (:: Stream/Functor (F;map repeat wa)))) + (let [[head tail] (! wa)] + (... [wa (split tail)])))) ## [Pattern-matching] -(defsyntax #export (\stream body [patterns' (+^ id^)]) - (do Lux/Monad - [patterns (map% Lux/Monad macro-expand-1 patterns') - g!s (gensym "s") - #let [patterns+ (: (List AST) - (do List/Monad - [pattern (l;reverse patterns)] - (: (List AST) (@list (` [(~ pattern) (~ g!s)]) (` (L;! (~ g!s)))))))]] - (wrap (@list g!s (` (;let [(~@ patterns+)] (~ body))))))) +(defsyntax #export (\stream body [patterns (+^ id^)]) + (case (l;reverse patterns) + (\ (@list& last prevs)) + (do Lux/Monad + [prevs (map% Lux/Monad macro-expand-1 prevs) + g!s (gensym "s") + body+ (foldL (lambda [inner outer] + (` (let [[(~ outer) (~ g!s)] (! (~ g!s))] + (~ inner)))) + (` (let [(~ last) (~ g!s)] (~ body))) + prevs)] + (wrap (@list g!s body+))) + + _ + (fail "Wrong syntax for \\stream"))) |