aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/documentation/lux/control/pipe.lux
blob: 3d80b5ade2a878e4f5ebb214f031157c1c3f7e4b (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
(.using
 [library
  [lux (.except let cond if exec case)
   ["$" documentation (.only documentation:)]
   [data
    [text (.only \n)
     ["%" \\format (.only format)]]]
   [macro
    ["[0]" template]]]]
 [\\library
  ["[0]" /]])

(documentation: /.new
  "Ignores the piped argument, and begins a new pipe."
  [(n.= 1
        (|> 20
            (n.* 3)
            (n.+ 4)
            (new 0 [++])))])

(documentation: /.let
  "Gives a name to the piped-argument, within the given expression."
  [(n.= 10
        (|> 5
            (let x (n.+ x x))))])

(documentation: /.cond
  (format "Branching for pipes."
          \n "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 [])]))])

(documentation: /.if
  "If-branching."
  [(same? (if (n.even? sample)
            "even"
            "odd")
          (|> sample
              (if [n.even?]
                [(new "even" [])]
                [(new "odd" [])])))])

(documentation: /.when
  "Only execute the body when the test passes."
  [(same? (if (n.even? sample)
            (n.* 2 sample)
            sample)
          (|> sample
              (when [n.even?]
                    [(n.* 2)])))])

(documentation: /.while
  (format "While loops for pipes."
          \n "Both the testing and calculating steps are pipes and must be given inside tuples.")
  [(|> +1
       (while [(i.< +10)]
              [++]))])

(documentation: /.do
  (format "Monadic pipes."
          \n "Each steps in the monadic computation is a pipe and must be given inside a tuple.")
  [(|> +5
       (do identity.monad
         [(i.* +3)]
         [(i.+ +4)]
         [++]))])

(documentation: /.exec
  (format "Non-updating pipes."
          \n "Will generate piped computations, but their results will not be used in the larger scope.")
  [(|> +5
       (exec [.nat %n log!])
       (i.* +10))])

(documentation: /.tuple
  (format "Parallel branching for pipes."
          \n "Allows to run multiple pipelines for a value and gives you a tuple of the outputs.")
  [(|> +5
       (tuple [(i.* +10)]
              [-- (i./ +2)]
              [i#encoded]))
   "=>"
   [+50 +2 "+5"]])

(documentation: /.case
  (format "Pattern-matching for pipes."
          \n "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"
         _ "???"))])

(.def .public documentation
  (.List $.Module)
  ($.module /._
            "Composable extensions to the piping macros (|> and <|) that enhance them with various abilities."
            [..new
             ..let
             ..cond
             ..if
             ..when
             ..while
             ..do
             ..exec
             ..tuple
             ..case]
            []))