aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/product.lux
blob: 6cf5bde39c4e66a1f987d0a32c9616dccf436916 (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
(.require
 [library
  [lux (.except left right)
   [abstract
    [equivalence (.only Equivalence)]
    [hash (.only Hash)]]]])

(with_template [<name>]
  [(def .public (<name> [left right])
     (All (_ left right)
       (-> [left right] <name>))
     <name>)]

  [left]
  [right]
  )

... https://en.wikipedia.org/wiki/Currying
(def .public (curried f)
  (All (_ a b c)
    (-> (-> [a b] c)
        (-> a b c)))
  (function (_ x y)
    (f [x y])))

(def .public (uncurried f)
  (All (_ a b c)
    (-> (-> a b c)
        (-> [a b] c)))
  (function (_ xy)
    (let [[x y] xy]
      (f x y))))

(def .public (swapped [left right])
  (All (_ left right) (-> [left right] [right left]))
  [right left])

(def .public (then f g)
  (All (_ a b c d)
    (-> (-> a c) (-> b d)
        (-> [a b] [c d])))
  (function (_ [x y])
    [(f x) (g y)]))

(def .public (forked f g)
  (All (_ a l r)
    (-> (-> a l) (-> a r)
        (-> a [l r])))
  (function (_ x)
    [(f x) (g x)]))

(def .public (equivalence left right)
  (All (_ l r) (-> (Equivalence l) (Equivalence r) (Equivalence [l r])))
  (implementation
   (def (= [rl rr] [sl sr])
     (and (of left = rl sl)
          (of right = rr sr)))))

(def .public (hash left right)
  (All (_ l r) (-> (Hash l) (Hash r) (Hash [l r])))
  (implementation
   (def equivalence
     (..equivalence (of left equivalence)
                    (of right equivalence)))
   (def (hash [leftV rightV])
     (.i64_+# (of left hash leftV)
              (of right hash rightV)))))