aboutsummaryrefslogtreecommitdiff
path: root/documentation/book/the_lux_programming_language/appendix_d.md
diff options
context:
space:
mode:
Diffstat (limited to 'documentation/book/the_lux_programming_language/appendix_d.md')
-rw-r--r--documentation/book/the_lux_programming_language/appendix_d.md69
1 files changed, 36 insertions, 33 deletions
diff --git a/documentation/book/the_lux_programming_language/appendix_d.md b/documentation/book/the_lux_programming_language/appendix_d.md
index 2d8adcd8e..07b6ea5b4 100644
--- a/documentation/book/the_lux_programming_language/appendix_d.md
+++ b/documentation/book/the_lux_programming_language/appendix_d.md
@@ -12,6 +12,8 @@ I try to figure out ways to get my code to be more pipe-sensitive, to see how fa
My personal record is 14 steps.
+ **Note**: The following examples assume that you `.require` the `["|" library/lux/control/pipe]` module.
+
## Piping macros in the standard library
Anyhow, after looking at some of the innovations of Clojure in the piping department, I decided to come up with my own tricks to try to get Lux to become a piping superpower.
@@ -24,17 +26,17 @@ Take a look at these babies:
... Loops for pipes.
... Both the testing and calculating steps are pipes and must be given inside tuples.
(|> 1
- (loop> [(n.< 10)]
- [++]))
+ (|.loop [(n.< 10)]
+ [++]))
```
-`loop>` takes a test _tuple_ and a body _tuple_.
+`loop` takes a test _tuple_ and a body _tuple_.
The reason is that each of those tuples represents the steps on an implicit piping macro (_oh, yeah!_).
So `[(n.< 10)]` is like `(|> value (n.< 10))`, and `[++]` is like `(|> value ++)`.
-Which value? Whatever has been piped into `loop>` from the underlying `|>` (in this case, the value `1`).
+Which value? Whatever has been piped into `loop` from the underlying `|>` (in this case, the value `1`).
---
@@ -43,15 +45,15 @@ Which value? Whatever has been piped into `loop>` from the underlying `|>` (in t
... Both the tests and the bodies are piped-code, and must be given inside a tuple.
... If a last else-pipe isn't given, the piped-argument will be used instead.
(|> 5
- (cond> [i.even?] [(i.* 2)]
- [i.odd?] [(i.* 3)]
- ... else branch
- [(new> -1 [])]))
+ (|.cond [i.even?] [(i.* 2)]
+ [i.odd?] [(i.* 3)]
+ ... else branch
+ [(|.new -1 [])]))
```
We have looping, and now we have branching; with a `cond`-inspired piping macro (complete with _else_ branch, just in case).
-But what's that thing over there? That `new>` thing?
+But what's that thing over there? That `new` thing?
Well, it's another piping macro. Of course!
@@ -60,10 +62,10 @@ Well, it's another piping macro. Of course!
(|> 20
(i.* 3)
(i.+ 4)
- (new> 0 [++]))
+ (|.new 0 [++]))
```
-`new>` establishes a new piping sequence that ignores any previous one.
+`new` establishes a new piping sequence that ignores any previous one.
Useful in certain kinds of situations.
@@ -72,10 +74,10 @@ Useful in certain kinds of situations.
```clojure
... Gives a name to the piped-argument, within the given expression.
(|> 5
- (let> @ (+ @ @)))
+ (|.let @ (+ @ @)))
```
-`let>` binds the current value piped into it so you can refer to it multiple times within its body.
+`let` binds the current value piped into it so you can refer to it multiple times within its body.
Pretty nifty, huh?
@@ -85,17 +87,17 @@ Pretty nifty, huh?
... Pattern-matching for pipes.
... The bodies of each branch are NOT pipes; just regular values.
(|> 5
- (case> 0 "zero"
- 1 "one"
- 2 "two"
- 3 "three"
- 4 "four"
- 5 "five"
- 6 "six"
- 7 "seven"
- 8 "eight"
- 9 "nine"
- _ "???"))
+ (|.when 0 "zero"
+ 1 "one"
+ 2 "two"
+ 3 "three"
+ 4 "four"
+ 5 "five"
+ 6 "six"
+ 7 "seven"
+ 8 "eight"
+ 9 "nine"
+ _ "???"))
```
Yeah, that's right!
@@ -110,10 +112,10 @@ You'll thank me later.
... Monadic pipes.
... Each step in the monadic computation is a pipe and must be given inside a tuple.
(|> 5
- (do> identity.monad
- [(i.* 3)]
- [(i.+ 4)]
- [+]))
+ (|.do identity.monad
+ [(i.* 3)]
+ [(i.+ 4)]
+ [+]))
```
And just to show you I'm serious, I did the unthinkable.
@@ -129,11 +131,12 @@ All you need is a macro that takes anything you want as parameters, but always t
As an example, here's the definition for `let>`:
```clojure
-(syntax: .public (let> [binding <code>.any
- body <code>.any
- prev <code>.any])
- (in (list (` (let [(~ binding) (~ prev)]
- (~ body))))))
+(def .public let
+ (syntax (_ [binding <code>.any
+ body <code>.any
+ prev <code>.any])
+ (in (list (` (.let [(, binding) (, prev)]
+ (, body)))))))
```
---