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

(def: #export identity
  {#.doc (doc "Identity function."
              "Does nothing to it's argument and just returns it."
              (= (identity "foo")
                 "foo"))}
  (All [a] (-> a a))
  (|>>))

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

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

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

(structure: #export monoid (All [a] (Monoid (-> a a)))
  (def: identity ..identity)
  (def: compose ..compose))