aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/function.lux
blob: d0bc286ae86b03649d2581e6b9bb1efb16b55ab8 (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
(.module:
  [library
   [lux #*
    [abstract
     [monoid (#+ Monoid)]]]])

(def: .public identity
  {#.doc (example "Identity function."
                  "Does nothing to its argument and just returns it."
                  (same? (identity value)
                         value))}
  (All [a] (-> a a))
  (|>>))

(def: .public (compose f g)
  {#.doc (example "Function composition."
                  (= ((compose f g) "foo")
                     (f (g "foo"))))}
  (All [a b c]
    (-> (-> b c) (-> a b) (-> a c)))
  (|>> g f))

(def: .public (constant value)
  {#.doc (example "Create constant functions."
                  (= ((constant "foo") "bar")
                     "foo"))}
  (All [o] (-> o (All [i] (-> i o))))
  (function (_ _) value))

(def: .public (flipped f)
  {#.doc (example "Flips the order of the arguments of a function."
                  (= ((flipped f) "foo" "bar")
                     (f "bar" "foo")))}
  (All [a b c]
    (-> (-> a b c) (-> b a c)))
  (function (_ x y) (f y x)))

(def: .public (apply input function)
  {#.doc (example "Simple 1-argument function application.")}
  (All [i o]
    (-> i (-> i o) o))
  (function input))

(implementation: .public monoid
  (All [a] (Monoid (-> a a)))
  
  (def: identity ..identity)
  (def: compose ..compose))