aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/pipe.lux
blob: 07f38e15ee5bcae5191453f1309d7d52f3955ef2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
(.module:
  {#.doc "Composable extensions to the piping macros (|> and <|) that enhance them with various abilities."}
  [library
   [lux #*
    [abstract
     [monad (#+ do)]]
    [control
     ["e" try]
     ["p" parser
      ["s" code (#+ Parser)]]]
    [data
     ["." identity]
     [collection
      ["." list ("#\." fold monad)]]]
    [macro (#+ with_identifiers)
     [syntax (#+ syntax:)]
     ["." code]]
    [math
     [number
      ["n" nat]
      ["i" int]]]]])

(def: body^
  (Parser (List Code))
  (s.tuple (p.some s.any)))

(syntax: .public (new> start
                       {body body^}
                       prev)
  {#.doc (example "Ignores the piped argument, and begins a new pipe."
                  (n.= 1
                       (|> 20
                           (n.* 3)
                           (n.+ 4)
                           (new> 0 [inc]))))}
  (in (list (` (|> (~ start) (~+ body))))))

(syntax: .public (let> binding body prev)
  {#.doc (example "Gives a name to the piped-argument, within the given expression."
                  (n.= 10
                       (|> 5
                           (let> x (n.+ x x)))))}
  (in (list (` (let [(~ binding) (~ prev)]
                 (~ body))))))

(def: _reversed_
  (Parser Any)
  (function (_ tokens)
    (#e.Success [(list.reversed tokens) []])))

(syntax: .public (cond> {_ _reversed_}
                        prev
                        {else body^}
                        {_ _reversed_}
                        {branches (p.some (p.and body^ body^))})
  {#.doc (example "Branching for pipes."
                  "Both the tests and the bodies are piped-code, and must be given inside a tuple."
                  (|> +5
                      (cond> [i.even?] [(i.* +2)]
                             [i.odd?] [(i.* +3)]
                             [(new> -1 [])])))}
  (with_identifiers [g!temp]
    (in (list (` (let [(~ g!temp) (~ prev)]
                   (cond (~+ (do list.monad
                               [[test then] branches]
                               (list (` (|> (~ g!temp) (~+ test)))
                                     (` (|> (~ g!temp) (~+ then))))))
                         (|> (~ g!temp) (~+ else)))))))))

(syntax: .public (if> {test body^} {then body^} {else body^} prev)
  {#.doc (example "If-branching."
                  (is? (if (n.even? sample)
                         "even"
                         "odd")
                       (|> sample
                           (if> [n.even?]
                                [(new> "even" [])]
                                [(new> "odd" [])]))))}
  (in (list (` (cond> [(~+ test)] [(~+ then)]
                      [(~+ else)]
                      (~ prev))))))

(syntax: .public (when> {test body^} {then body^} prev)
  {#.doc (example "Only execute the body when the test passes."
                  (is? (if (n.even? sample)
                         (n.* 2 sample)
                         sample)
                       (|> sample
                           (when> [n.even?]
                                  [(n.* 2)]))))}
  (in (list (` (cond> [(~+ test)] [(~+ then)]
                      []
                      (~ prev))))))

(syntax: .public (loop> {test body^}
                        {then body^}
                        prev)
  {#.doc (example "Loops for pipes."
                  "Both the testing and calculating steps are pipes and must be given inside tuples."
                  (|> +1
                      (loop> [(i.< +10)]
                             [inc])))}
  (with_identifiers [g!temp]
    (in (list (` (loop [(~ g!temp) (~ prev)]
                   (if (|> (~ g!temp) (~+ test))
                     ((~' recur) (|> (~ g!temp) (~+ then)))
                     (~ g!temp))))))))

(syntax: .public (do> monad
                      {steps (p.some body^)}
                      prev)
  {#.doc (example "Monadic pipes."
                  "Each steps in the monadic computation is a pipe and must be given inside a tuple."
                  (|> +5
                      (do> identity.monad
                           [(i.* +3)]
                           [(i.+ +4)]
                           [inc])))}
  (with_identifiers [g!temp]
    (case (list.reversed steps)
      (^ (list& last_step prev_steps))
      (let [step_bindings (do list.monad
                            [step (list.reversed prev_steps)]
                            (list g!temp (` (|> (~ g!temp) (~+ step)))))]
        (in (list (` ((~! do) (~ monad)
                      [.let [(~ g!temp) (~ prev)]
                       (~+ step_bindings)]
                      (|> (~ g!temp) (~+ last_step)))))))

      _
      (in (list prev)))))

(syntax: .public (exec> {body body^}
                        prev)
  {#.doc (example "Non-updating pipes."
                  "Will generate piped computations, but their results will not be used in the larger scope."
                  (|> +5
                      (exec> [.nat %n log!])
                      (i.* +10)))}
  (with_identifiers [g!temp]
    (in (list (` (let [(~ g!temp) (~ prev)]
                   (exec (|> (~ g!temp) (~+ body))
                     (~ g!temp))))))))

(syntax: .public (tuple> {paths (p.many body^)}
                         prev)
  {#.doc (example "Parallel branching for pipes."
                  "Allows to run multiple pipelines for a value and gives you a tuple of the outputs."
                  (|> +5
                      (tuple> [(i.* +10)]
                              [dec (i./ +2)]
                              [Int/encode]))
                  "Will become: [+50 +2 '+5']")}
  (with_identifiers [g!temp]
    (in (list (` (let [(~ g!temp) (~ prev)]
                   [(~+ (list\map (function (_ body) (` (|> (~ g!temp) (~+ body))))
                                  paths))]))))))

(syntax: .public (case> {branches (p.many (p.and s.any s.any))}
                        prev)
  {#.doc (example "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"
                             _ "???")))}
  (in (list (` (case (~ prev)
                 (~+ (list\join (list\map (function (_ [pattern body]) (list pattern body))
                                          branches))))))))