/
Nonempty.elm
550 lines (375 loc) · 12.3 KB
/
Nonempty.elm
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
module List.Nonempty exposing
( Nonempty(..)
, singleton, fromList
, head, tail, toList, get, last, take, sample
, isSingleton, length, member, all, any
, cons, append, pop, reverse, concat
, replaceHead, replaceTail, dropTail
, map, indexedMap, map2, andMap, concatMap
, filter
, foldl, foldl1
, zip, unzip
, sort, sortBy, sortWith
, dedup, uniq
, fromElement
)
{-| A list that cannot be empty. The head and tail can be accessed without Maybes. Most other list functions are
available.
# Definition
@docs Nonempty
# Create
@docs singleton, fromList
# Access
@docs head, tail, toList, get, last, take, sample
# Inspect
Nonempty lists support equality with the usual `(==)` operator (provided their contents also support equality).
@docs isSingleton, length, member, all, any
# Convert
@docs cons, append, pop, reverse, concat
# Swap
@docs replaceHead, replaceTail, dropTail
# Map
@docs map, indexedMap, map2, andMap, concatMap
# Filter
@docs filter
# Fold
To fold or scan from the right, reverse the list first.
@docs foldl, foldl1
# Zipping
@docs zip, unzip
# Sort
@docs sort, sortBy, sortWith
# Deduplicate
The nonempty list's elements must support equality (e.g. not functions). Otherwise you will get a runtime error.
@docs dedup, uniq
# Deprecated
@docs fromElement
-}
import Random
{-| The Nonempty type. If you have both a head and tail, you can construct a
nonempty list directly. Otherwise use the helpers below instead.
-}
type Nonempty a
= Nonempty a (List a)
{-| Create a singleton list with the given element.
-}
singleton : a -> Nonempty a
singleton x =
Nonempty x []
{-| Create a singleton list with the given element. Deprecated in favor of `singleton`, which is what elm-lang/core uses.
-}
fromElement : a -> Nonempty a
fromElement =
singleton
{-| Create a nonempty list from an ordinary list, failing on the empty list.
-}
fromList : List a -> Maybe (Nonempty a)
fromList ys =
case ys of
x :: xs ->
Just (Nonempty x xs)
_ ->
Nothing
{-| Return the head of the list.
-}
head : Nonempty a -> a
head (Nonempty x xs) =
x
{-| Return the tail of the list.
-}
tail : Nonempty a -> List a
tail (Nonempty x xs) =
xs
{-| Convert to an ordinary list.
-}
toList : Nonempty a -> List a
toList (Nonempty x xs) =
x :: xs
{-| Get the item at the specified index. The head has index 0. Indices are modulused by the length so out-of-range
errors can't happen. This means that negative indices are supported, e.g. -1 to get the last element.
-}
get : Int -> Nonempty a -> a
get i ((Nonempty x xs) as ne) =
let
j =
modBy (length ne) i
find k ys =
case ys of
[] ->
{- This should never happen, but to avoid Debug.crash,
we return the head of the list.
-}
x
z :: zs ->
if k == 0 then
z
else
find (k - 1) zs
in
if j == 0 then
x
else
find (j - 1) xs
{-| Return the last element of the list.
-}
last : Nonempty a -> a
last (Nonempty x xs) =
case xs of
[] ->
x
[ y ] ->
y
_ :: rest ->
last (Nonempty x rest)
{-| Create a nonempty list consisting of the first n elements. If n < 1, returns a nonempty list consiting of just the first element
take 2 (Nonempty 1 [ 2, 3 ]) --> Nonempty 1 [ 2 ]
take 0 (Nonempty 1 [ 2, 3 ]) --> fromElement 1
take -3 (Nonempty 1 [ 2, 3 ]) --> fromElement 1
-}
take : Int -> Nonempty a -> Nonempty a
take n (Nonempty x xs) =
Nonempty x (List.take (n - 1) xs)
{-| Create a random generator that returns a value of the nonempty list chosen uniformly at random.
-}
sample : Nonempty a -> Random.Generator a
sample nonempty =
Random.int 0 (length nonempty - 1)
|> Random.map (\i -> get i nonempty)
{-| Add another element as the head of the list, pushing the previous head to the tail.
-}
cons : a -> Nonempty a -> Nonempty a
cons y (Nonempty x xs) =
Nonempty y (x :: xs)
{-| Append two nonempty lists together. `(++)` is _not_ supported.
-}
append : Nonempty a -> Nonempty a -> Nonempty a
append (Nonempty x xs) (Nonempty y ys) =
Nonempty x (xs ++ y :: ys)
{-| Pop and discard the head, or do nothing for a singleton list. Useful if you
want to exhaust a list but hang on to the last item indefinitely.
pop (Nonempty 3 [ 2, 1 ]) --> Nonempty 2 [1]
pop (Nonempty 1 []) --> Nonempty 1 []
-}
pop : Nonempty a -> Nonempty a
pop (Nonempty x xs) =
case xs of
[] ->
Nonempty x xs
y :: ys ->
Nonempty y ys
{-| Reverse a nonempty list.
-}
reverse : Nonempty a -> Nonempty a
reverse (Nonempty x xs) =
let
revapp : ( List a, a, List a ) -> Nonempty a
revapp ( ls, c, rs ) =
case rs of
[] ->
Nonempty c ls
r :: rss ->
revapp ( c :: ls, r, rss )
in
revapp ( [], x, xs )
{-| Flatten a nonempty list of nonempty lists into a single nonempty list.
-}
concat : Nonempty (Nonempty a) -> Nonempty a
concat (Nonempty xs xss) =
let
hd =
head xs
tl =
tail xs ++ List.concat (List.map toList xss)
in
Nonempty hd tl
{-| Exchange the head element while leaving the tail alone.
-}
replaceHead : a -> Nonempty a -> Nonempty a
replaceHead y (Nonempty x xs) =
Nonempty y xs
{-| Exchange the tail for another while leaving the head alone.
-}
replaceTail : List a -> Nonempty a -> Nonempty a
replaceTail ys (Nonempty x xs) =
Nonempty x ys
{-| Replace the tail with the empty list while leaving the head alone.
-}
dropTail : Nonempty a -> Nonempty a
dropTail (Nonempty x xs) =
Nonempty x []
{-| Map a function over a nonempty list.
-}
map : (a -> b) -> Nonempty a -> Nonempty b
map f (Nonempty x xs) =
Nonempty (f x) (List.map f xs)
{-| Map a function over two nonempty lists.
-}
map2 : (a -> b -> c) -> Nonempty a -> Nonempty b -> Nonempty c
map2 f (Nonempty x xs) (Nonempty y ys) =
Nonempty (f x y) (List.map2 f xs ys)
{-| Map over an arbitrary number of nonempty lists.
map2 (,) xs ys == map (,) xs |> andMap ys
head (map (,,) xs |> andMap ys |> andMap zs) == ( head xs, head ys, head zs )
-}
andMap : Nonempty a -> Nonempty (a -> b) -> Nonempty b
andMap =
map2 (|>)
{-| Map a given function onto a nonempty list and flatten the resulting nonempty lists. If you're chaining, you can
define `andThen = flip concatMap`.
-}
concatMap : (a -> Nonempty b) -> Nonempty a -> Nonempty b
concatMap f xs =
concat (map f xs)
{-| Same as `map` but the function is also applied to the index of each element (starting at zero).
-}
indexedMap : (Int -> a -> b) -> Nonempty a -> Nonempty b
indexedMap f (Nonempty x xs) =
let
wrapped i d =
f (i + 1) d
in
Nonempty (f 0 x) (List.indexedMap wrapped xs)
{-| Determine if the nonempty list has exactly one element.
-}
isSingleton : Nonempty a -> Bool
isSingleton (Nonempty x xs) =
List.isEmpty xs
{-| Find the length of the nonempty list.
-}
length : Nonempty a -> Int
length (Nonempty x xs) =
List.length xs + 1
{-| Determine if an element is present in the nonempty list.
-}
member : a -> Nonempty a -> Bool
member y (Nonempty x xs) =
x == y || List.member y xs
{-| Determine if all elements satisfy the predicate.
-}
all : (a -> Bool) -> Nonempty a -> Bool
all f (Nonempty x xs) =
f x && List.all f xs
{-| Determine if any elements satisfy the predicate.
-}
any : (a -> Bool) -> Nonempty a -> Bool
any f (Nonempty x xs) =
f x || List.any f xs
{-| Filter a nonempty list. If all values are filtered out, return the singleton list containing the default value
provided. If any value is retained, the default value is not used. If you want to deal with a Maybe instead, use
`toList >> List.filter yourPredicate >> fromList`.
isEven : Int -> Bool
isEven n = modBy 2 n == 0
filter isEven 0 (Nonempty 7 [2, 5]) --> fromElement 2
filter isEven 0 (Nonempty 7 []) --> fromElement 0
-}
filter : (a -> Bool) -> a -> Nonempty a -> Nonempty a
filter p d (Nonempty x xs) =
if p x then
Nonempty x (List.filter p xs)
else
case List.filter p xs of
[] ->
Nonempty d []
y :: ys ->
Nonempty y ys
insertWith : (a -> a -> Order) -> a -> List a -> Nonempty a
insertWith cmp hd aList =
case aList of
x :: xs ->
if cmp x hd == LT then
Nonempty x (List.sortWith cmp (hd :: xs))
else
Nonempty hd aList
[] ->
Nonempty hd []
{-| Sort a nonempty list of comparable things, lowest to highest.
-}
sort : Nonempty comparable -> Nonempty comparable
sort (Nonempty x xs) =
insertWith compare x (List.sort xs)
{-| Sort a nonempty list of things by a derived property.
-}
sortBy : (a -> comparable) -> Nonempty a -> Nonempty a
sortBy f (Nonempty x xs) =
insertWith (\a b -> compare (f a) (f b)) x (List.sortBy f xs)
{-| Sort a nonempty list of things by a custom comparison function.
-}
sortWith : (a -> a -> Order) -> Nonempty a -> Nonempty a
sortWith f (Nonempty x xs) =
insertWith f x <| List.sortWith f xs
{-| Remove _adjacent_ duplicate elements from the nonempty list.
dedup (Nonempty 1 [ 2, 2, 1 ]) --> Nonempty 1 [2, 1]
-}
dedup : Nonempty a -> Nonempty a
dedup (Nonempty x xs) =
let
dedupe : a -> List a -> List a -> Nonempty a
dedupe prev done next =
case next of
[] ->
Nonempty prev done
y :: ys ->
if y == prev then
dedupe prev done ys
else
dedupe y (prev :: done) ys
in
reverse <| dedupe x [] xs
{-| Remove _all_ duplicate elements from the nonempty list, with the remaining elements ordered by first occurrence.
uniq (Nonempty 1 [ 2, 2, 1 ]) --> Nonempty 1 [2]
-}
uniq : Nonempty a -> Nonempty a
uniq (Nonempty x xs) =
let
unique : List a -> Nonempty a -> List a -> Nonempty a
unique seen done next =
case next of
[] ->
done
y :: ys ->
if List.member y seen then
unique seen done ys
else
unique (y :: seen) (cons y done) ys
in
reverse <| unique [ x ] (Nonempty x []) xs
{-| Reduce a nonempty list from the left with a base case.
foldl (++) "" (Nonempty "a" [ "b", "c" ]) --> "cba"
-}
foldl : (a -> b -> b) -> b -> Nonempty a -> b
foldl f b (Nonempty x xs) =
List.foldl f b (x :: xs)
{-| Reduce a nonempty list from the left _without_ a base case. As per Elm convention, the first argument is the current
element and the second argument is the accumulated value. The function is first invoked on the _second_ element, using
the first element as the accumulated value, except for singleton lists in which has the head is returned.
foldl1 (++) (Nonempty "a" ["b", "c"]) --> "cba"
foldl1 (++) (fromElement "a") --> "a"
findMe : Int
findMe = 42
minimizeMe : Int -> Int
minimizeMe n = abs (n-findMe)
nearest : Int
nearest = foldl1 (\a b -> if minimizeMe a < minimizeMe b then a else b) (Nonempty 10 [20,30,40,50,60])
nearest --> 40
-}
foldl1 : (a -> a -> a) -> Nonempty a -> a
foldl1 f (Nonempty x xs) =
List.foldl f x xs
{-| Take two nonempty lists and compose them into a nonempty list of corresponding pairs.
The length of the new list equals the length of the smallest list given.
-}
zip : Nonempty a -> Nonempty b -> Nonempty ( a, b )
zip (Nonempty x xs) (Nonempty y ys) =
Nonempty ( x, y ) (List.map2 Tuple.pair xs ys)
{-| Decompose a nonempty list of tuples into a tuple of nonempty lists.
batters : Nonempty (String, Int)
batters = Nonempty ("Barry Bonds", 762) [("Hank Aaron", 755), ("Babe Ruth", 714)]
unzip batters --> (Nonempty "Barry Bonds" ["Hank Aaron", "Babe Ruth"], Nonempty 762 [755, 714])
-}
unzip : Nonempty ( a, b ) -> ( Nonempty a, Nonempty b )
unzip (Nonempty ( x, y ) rest) =
let
( xs, ys ) =
List.unzip rest
in
( Nonempty x xs, Nonempty y ys )