aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/collection/array.lux
blob: 69f701e82c9856a22a9d89472ec7e0103fd6b99d (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
(.using
 [library
  [lux {"-" list}
   [abstract
    [monoid (.only Monoid)]
    [functor (.only Functor)]
    [equivalence (.only Equivalence)]
    [mix (.only Mix)]
    [predicate (.only Predicate)]]
   [data
    [collection
     ["[0]" list]]]
   [type
    [variance (.only)]]]]
 ["!" \\unsafe])

(def: .public type_name
  Text
  !.type)

(type: .public Array'
  !.Array')

(type: .public Array
  !.Array)

(def: .public empty
  (All (_ a) (-> Nat (Array a)))
  (|>> !.empty))

(def: .public size
  (All (_ r w) (-> (Array' r w) Nat))
  (|>> !.size))

(def: .public (item index array)
  (All (_ r w)
    (-> Nat (Array' r w) (Maybe r)))
  (if (!.lacks? index array)
    {.#None}
    {.#Some (!.item index array)}))

(def: .public (has! index value array)
  (All (_ r w)
    (-> Nat w (Array' r w) (Array' r w)))
  (!.has! index value array))

(def: .public (lacks! index array)
  (All (_ r w)
    (-> Nat (Array' r w) (Array' r w)))
  (!.lacks! index array))

(def: .public (lacks? index array)
  (All (_ r w)
    (-> Nat (Array' r w) Bit))
  (!.lacks? index array))

(def: .public (has? index array)
  (All (_ r w)
    (-> Nat (Array' r w) Bit))
  (!.has? index array))

(def: .public (revised! index $ array)
  (All (_ r w)
    (-> Nat (-> r w) (Array' r w) (Array' r w)))
  (!.revised! index $ array))

(def: .public (upsert! index default transform array)
  (All (_ r w)
    (-> Nat r (-> r w) (Array' r w) (Array' r w)))
  (!.upsert! index default transform array))

(def: .public (copy! length src_start src_array dest_start dest_array)
  (All (_ r w)
    (-> Nat Nat (Array' w Nothing) Nat (Array' r w)
        (Array' r w)))
  (!.copy! length src_start src_array dest_start dest_array))

(def: .public occupancy
  (All (_ r w) (-> (Array' r w) Nat))
  (|>> !.occupancy))

(def: .public vacancy
  (All (_ r w) (-> (Array' r w) Nat))
  (|>> !.vacancy))

(def: .public (only! ? it)
  (All (_ r w)
    (-> (Predicate r) (Array' r w) (Array' r w)))
  (!.only! ? it))

(def: .public (example ? it)
  (All (_ r w)
    (-> (Predicate r) (Array' r w) (Maybe r)))
  (!.example ? it))

(def: .public (example' ? it)
  (All (_ r w)
    (-> (-> Nat r Bit) (Array' r w) (Maybe [Nat r])))
  (!.example' ? it))

(def: .public clone
  (All (_ a) (-> (Array a) (Array a)))
  (|>> !.clone))

(def: .public of_list
  (All (_ a) (-> (List a) (Array a)))
  (|>> !.of_list))

(def: .public (list default array)
  (All (_ r w) (-> (Maybe r) (Array' r w) (List r)))
  (!.list default array))

(implementation: .public (equivalence //)
  (All (_ r) (-> (Equivalence r) (Equivalence (Ex (_ w) (Array' r w)))))
  
  (def: (= left/* right/*)
    (!.= // left/* right/*)))

(implementation: .public monoid
  (All (_ a) (Monoid (Array a)))
  
  (def: identity (!.empty 0))

  (def: (composite left/* right/*)
    (!.composite left/* right/*)))

(implementation: .public mix
  (Mix (All (_ r) (Array' r Nothing)))
  
  (def: (mix $ init it)
    (!.mix (function (_ index partial total)
             ($ partial total))
           init
           it)))

(implementation: .public functor
  (Functor Array)
  
  (def: (each $ input)
    (!.each $ input)))

(template [<safe> <unsafe>]
  [(def: .public (<safe> ? it)
     (All (_ r w)
       (-> (Predicate r) (Predicate (Array' r w))))
     (<unsafe> ? it))]

  [every? !.every?]
  [any? !.any?]
  )

(def: .public (one ? it)
  (All (_ r r' w)
    (-> (-> r (Maybe r')) (Array' r w) (Maybe r')))
  (!.one ? it))