aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/product.lux
blob: 64b84cb3ee93ea6b43d922e73d1a21bbd46ea87c (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
(.module:
  {#.doc "Functionality for working with tuples (particularly 2-tuples)."}
  [lux #*
   [abstract
    [equivalence (#+ Equivalence)]]])

(template [<name> <type> <output>]
  [(def: #export (<name> xy)
     (All [a b] (-> [a b] <type>))
     (let [[x y] xy]
       <output>))]

  [left  a x]
  [right b y])

(def: #export (curry f)
  (All [a b c]
    (-> (-> [a b] c)
        (-> a b c)))
  (function (_ x y)
    (f [x y])))

(def: #export (uncurry f)
  (All [a b c]
    (-> (-> a b c) (-> [a b] c)))
  (function (_ xy)
    (let [[x y] xy]
      (f x y))))

(def: #export (swap xy)
  (All [a b] (-> [a b] [b a]))
  (let [[x y] xy]
    [y x]))

(def: #export (both f g)
  (All [a b c d]
    (-> (-> a c) (-> b d)
        (-> [a b] [c d])))
  (function (_ [x y])
    [(f x) (g y)]))

(def: #export (fork f g)
  (All [a l r]
    (-> (-> a l) (-> a r)
        (-> a [l r])))
  (function (_ x)
    [(f x) (g x)]))

(structure: #export (equivalence l@= r@=)
  (All [l r]
    (-> (Equivalence l) (Equivalence r)
        (Equivalence [l r])))
  (def: (= [lP rP] [lS rS])
    (and (l@= lP lS)
         (r@= rP rS))))