diff options
-rw-r--r-- | stdlib/source/lux/control/pipe.lux | 59 | ||||
-rw-r--r-- | stdlib/test/test/lux/control/pipe.lux | 43 |
2 files changed, 46 insertions, 56 deletions
diff --git a/stdlib/source/lux/control/pipe.lux b/stdlib/source/lux/control/pipe.lux index 937935684..6f7721cf6 100644 --- a/stdlib/source/lux/control/pipe.lux +++ b/stdlib/source/lux/control/pipe.lux @@ -1,4 +1,4 @@ -(;module: {#;doc "Composable extensions to the piping macro |> that enhance it with various abilities."} +(;module: {#;doc "Composable extensions to the piping macros (|> and <|) that enhance them with various abilities."} lux (lux (control ["M" monad #+ do Monad] ["p" parser]) @@ -13,12 +13,12 @@ (Syntax (List Code)) (s;tuple (p;many s;any))) -(syntax: #export (_> [tokens (p;at-least +2 s;any)]) +(syntax: #export (new> [tokens (p;at-least +2 s;any)]) {#;doc (doc "Ignores the piped argument, and begins a new pipe." (|> 20 (i.* 3) (i.+ 4) - (_> 0 i.inc)))} + (new> 0 i.inc)))} (case (list;reverse tokens) (^ (list& _ r-body)) (wrap (list (` (|> (~@ (list;reverse r-body)))))) @@ -26,33 +26,26 @@ _ (undefined))) -(syntax: #export (@> [name (p;default "@" s;local-symbol)] - [body body^] - prev) +(syntax: #export (let> lhs [body body^] prev) {#;doc (doc "Gives a name to the piped-argument, within the given expression." - "If given no name, defaults to '@'." (|> 5 - (@> X [(i.+ X X)])) - - (|> 5 - (@> [(i.+ @ @)])))} + (let> X [(i.+ X X)])))} (wrap (list (L/fold (function [next prev] - (` (with-expansions - [(~ (code;symbol ["" name])) (~ prev)] + (` (let [(~ lhs) (~ prev)] (~ next)))) prev body)))) -(syntax: #export (?> [branches (p;many (p;seq body^ body^))] - [?else (p;maybe body^)] - prev) +(syntax: #export (cond> [branches (p;many (p;seq body^ body^))] + [?else (p;maybe body^)] + prev) {#;doc (doc "Branching for pipes." "Both the tests and the bodies are piped-code, and must be given inside a tuple." "If a last else-pipe is not given, the piped-argument will be used instead." (|> 5 - (?> [i.even?] [(i.* 2)] - [i.odd?] [(i.* 3)] - [(_> -1)])))} + (cond> [i.even?] [(i.* 2)] + [i.odd?] [(i.* 3)] + [(new> -1)])))} (with-gensyms [g!temp] (wrap (list (` (with-expansions [(~ g!temp) (~ prev)] @@ -67,26 +60,26 @@ _ g!temp))))))))) -(syntax: #export (!> [test body^] [then body^] prev) +(syntax: #export (loop> [test body^] [then body^] prev) {#;doc (doc "Loops for pipes." "Both the testing and calculating steps are pipes and must be given inside tuples." (|> 1 - (!> [(i.< 10)] - [i.inc])))} + (loop> [(i.< 10)] + [i.inc])))} (with-gensyms [g!temp] (wrap (list (` (loop [(~ g!temp) (~ prev)] (if (|> (~ g!temp) (~@ test)) ((~' recur) (|> (~ g!temp) (~@ then))) (~ g!temp)))))))) -(syntax: #export (%> monad [steps (p;some body^)] prev) +(syntax: #export (do> monad [steps (p;some body^)] prev) {#;doc (doc "Monadic pipes." "Each steps in the monadic computation is a pipe and must be given inside a tuple." (|> 5 - (%> Id/Monad - [(i.* 3)] - [(i.+ 4)] - [i.inc])))} + (do> Monad<Identity> + [(i.* 3)] + [(i.+ 4)] + [i.inc])))} (with-gensyms [g!temp] (case (list;reverse steps) (^ (list& last-step prev-steps)) @@ -101,11 +94,11 @@ _ (wrap (list prev))))) -(syntax: #export (~> [body body^] prev) +(syntax: #export (exec> [body body^] prev) {#;doc (doc "Non-updating pipes." "Will generate piped computations, but their results will not be used in the larger scope." (|> 5 - (~> [int-to-nat %n log!]) + (exec> [int-to-nat %n log!]) (i.* 10)))} (do @ [g!temp (meta;gensym "")] @@ -113,13 +106,13 @@ (exec (|> (~ g!temp) (~@ body)) (~ g!temp)))))))) -(syntax: #export (&> [paths (p;many body^)] prev) +(syntax: #export (tuple> [paths (p;many body^)] prev) {#;doc (doc "Parallel branching for pipes." "Allows to run multiple pipelines for a value and gives you a tuple of the outputs." (|> 5 - (&> [(i.* 10)] - [i.dec (i./ 2)] - [Int/encode])) + (tuple> [(i.* 10)] + [i.dec (i./ 2)] + [Int/encode])) "Will become: [50 2 \"5\"]")} (do @ [g!temp (meta;gensym "")] diff --git a/stdlib/test/test/lux/control/pipe.lux b/stdlib/test/test/lux/control/pipe.lux index 23e6cfe60..527db91c3 100644 --- a/stdlib/test/test/lux/control/pipe.lux +++ b/stdlib/test/test/lux/control/pipe.lux @@ -1,13 +1,13 @@ (;module: lux (lux [io] - (control ["M" monad #+ do Monad] + (control [monad #+ do Monad] pipe) (data text/format [number] [product] identity - [text "T/" Eq<Text>]) + [text "text/" Eq<Text>]) ["r" math/random]) lux/test) @@ -17,44 +17,41 @@ (|> 20 (i.* 3) (i.+ 4) - (_> 0 i.inc) + (new> 0 i.inc) (i.= 1))) (test "Can give names to piped values within a pipeline's scope." - (and (|> 5 - (@> [(i.+ @ @)]) - (i.= 10)) - (|> 5 - (@> X [(i.+ X X)]) - (i.= 10)))) + (|> 5 + (let> X [(i.+ X X)]) + (i.= 10))) (test "Can do branching in pipelines." (and (|> 5 - (?> [i.even?] [(i.* 2)] - [i.odd?] [(i.* 3)] - [(_> -1)]) + (cond> [i.even?] [(i.* 2)] + [i.odd?] [(i.* 3)] + [(new> -1)]) (i.= 15)) (|> 4 - (?> [i.even?] [(i.* 2)] - [i.odd?] [(i.* 3)]) + (cond> [i.even?] [(i.* 2)] + [i.odd?] [(i.* 3)]) (i.= 8)) (|> 5 - (?> [i.even?] [(i.* 2)] - [(_> -1)]) + (cond> [i.even?] [(i.* 2)] + [(new> -1)]) (i.= -1)))) (test "Can loop within pipelines." (|> 1 - (!> [(i.< 10)] - [i.inc]) + (loop> [(i.< 10)] + [i.inc]) (i.= 10))) (test "Can use monads within pipelines." (|> 5 - (%> Monad<Identity> - [(i.* 3)] - [(i.+ 4)] - [i.inc]) + (do> Monad<Identity> + [(i.* 3)] + [(i.+ 4)] + [i.inc]) (i.= 20))) (test "Can pattern-match against piped values." @@ -70,5 +67,5 @@ 8 "eight" 9 "nine" _ "???") - (T/= "five"))) + (text/= "five"))) )) |