@@ -51,7 +51,6 @@ pub type LengthMismatch {
51
51
/// > length([1, 2])
52
52
/// 2
53
53
///
54
- ///
55
54
pub external fn length ( of : List ( a) ) -> Int = "erlang" "length"
56
55
57
56
/// Create a new list from a given list containing the same elements but in the
@@ -74,7 +73,6 @@ pub external fn length(of: List(a)) -> Int = "erlang" "length"
74
73
/// > reverse([1, 2])
75
74
/// [2, 1]
76
75
///
77
- ///
78
76
pub external fn reverse ( List ( a) ) -> List ( a) = "lists" "reverse"
79
77
80
78
/// Determine whether or not the list is empty.
@@ -92,7 +90,6 @@ pub external fn reverse(List(a)) -> List(a) = "lists" "reverse"
92
90
/// > is_empty([1, 1])
93
91
/// False
94
92
///
95
- ///
96
93
pub fn is_empty ( list : List ( a) ) -> Bool {
97
94
list == [ ]
98
95
}
@@ -119,7 +116,6 @@ pub fn is_empty(list: List(a)) -> Bool {
119
116
/// > contains([1, 0], 0)
120
117
/// True
121
118
///
122
- ///
123
119
pub fn contains ( list : List ( a) , has elem : a) -> Bool {
124
120
case list {
125
121
[ ] -> False
@@ -140,7 +136,6 @@ pub fn contains(list: List(a), has elem: a) -> Bool {
140
136
/// > head([1, 2])
141
137
/// Ok(1)
142
138
///
143
- ///
144
139
pub fn head(list: List(a)) -> Option(a) {
145
140
case list {
146
141
[] -> result.none()
@@ -164,7 +159,6 @@ pub fn head(list: List(a)) -> Option(a) {
164
159
/// > tail([1, 2])
165
160
/// Ok([2])
166
161
///
167
- ///
168
162
pub fn tail(list: List(a)) -> Option(List(a)) {
169
163
case list {
170
164
[] -> result.none()
@@ -196,7 +190,6 @@ fn do_filter(list: List(a), fun: fn(a) -> Bool, acc: List(a)) -> List(a) {
196
190
/// > filter([2, 4, 6, 1], fn(x) { x > 6 })
197
191
/// []
198
192
///
199
- ///
200
193
pub fn filter(list: List(a), for predicate: fn(a) -> Bool) -> List(a) {
201
194
do_filter(list, predicate, [])
202
195
}
@@ -216,7 +209,6 @@ fn do_map(list: List(a), fun: fn(a) -> b, acc: List(b)) -> List(b) {
216
209
/// > map([2, 4, 6], fn(x) { x * 2 })
217
210
/// [4, 8, 12]
218
211
///
219
- ///
220
212
pub fn map(list: List(a), with fun: fn(a) -> b) -> List(b) {
221
213
do_map(list, fun, [])
222
214
}
@@ -244,7 +236,6 @@ fn do_index_map(
244
236
/// > index_map(["a", "b"], fn(i, x) { tuple(i, x) })
245
237
/// [tuple(0, "a"), tuple(1, "b")]
246
238
///
247
- ///
248
239
pub fn index_map(list: List(a), with fun: fn(Int, a) -> b) -> List(b) {
249
240
do_index_map(list, fun, 0, [])
250
241
}
@@ -289,7 +280,6 @@ fn do_traverse(
289
280
/// > traverse([[1], [], [2]], head)
290
281
/// Error(Nil)
291
282
///
292
- ///
293
283
pub fn traverse(
294
284
list: List(a),
295
285
with fun: fn(a) -> Result(b, e),
@@ -313,7 +303,6 @@ pub fn traverse(
313
303
/// > drop([1, 2, 3, 4], 9)
314
304
/// []
315
305
///
316
- ///
317
306
pub fn drop(from list: List(a), up_to n: Int) -> List(a) {
318
307
case n <= 0 {
319
308
True -> list
@@ -352,7 +341,6 @@ fn do_take(list: List(a), n: Int, acc: List(a)) -> List(a) {
352
341
/// > take([1, 2, 3, 4], 9)
353
342
/// [1, 2, 3, 4]
354
343
///
355
- ///
356
344
pub fn take(from list: List(a), up_to n: Int) -> List(a) {
357
345
do_take(list, n, [])
358
346
}
@@ -364,7 +352,6 @@ pub fn take(from list: List(a), up_to n: Int) -> List(a) {
364
352
/// > new()
365
353
/// []
366
354
///
367
- ///
368
355
pub fn new() -> List(a) {
369
356
[]
370
357
}
@@ -379,7 +366,6 @@ pub fn new() -> List(a) {
379
366
/// > append([1, 2], [3])
380
367
/// [1, 2, 3]
381
368
///
382
- ///
383
369
pub external fn append(List(a), List(a)) -> List(a)
384
370
= "lists" "append";
385
371
@@ -400,7 +386,6 @@ fn do_flatten(lists: List(List(a)), acc: List(a)) -> List(a) {
400
386
/// > flatten([[1], [2, 3], []])
401
387
/// [1, 2, 3]
402
388
///
403
- ///
404
389
pub fn flatten(lists: List(List(a))) -> List(a) {
405
390
do_flatten(lists, [])
406
391
}
@@ -458,7 +443,6 @@ pub fn fold_right(
458
443
/// > find([], fn(x) { True })
459
444
/// Error(Nil)
460
445
///
461
- ///
462
446
pub fn find(
463
447
in haystack: List(a),
464
448
one_that is_desired: fn(a) -> Bool,
@@ -490,7 +474,6 @@ pub fn find(
490
474
/// > find_map([], head)
491
475
/// Error(Nil)
492
476
///
493
- ///
494
477
pub fn find_map(
495
478
in haystack: List(a),
496
479
with fun: fn(a) -> Option(b),
@@ -520,7 +503,6 @@ pub fn find_map(
520
503
/// > all([4, 3], fn(x) { x > 3 })
521
504
/// False
522
505
///
523
- ///
524
506
pub fn all(in list: List(a), satisfying predicate: fn(a) -> Bool) -> Bool {
525
507
case list {
526
508
[] -> True
@@ -550,7 +532,6 @@ pub fn all(in list: List(a), satisfying predicate: fn(a) -> Bool) -> Bool {
550
532
/// > any([3, 4], fn(x) { x > 3 })
551
533
/// True
552
534
///
553
- ///
554
535
pub fn any(in list: List(a), satisfying predicate: fn(a) -> Bool) -> Bool {
555
536
case list {
556
537
[] -> False
@@ -581,7 +562,6 @@ pub fn any(in list: List(a), satisfying predicate: fn(a) -> Bool) -> Bool {
581
562
/// > zip([1, 2], [3, 4])
582
563
/// [tuple(1, 3), tuple(2, 4)]
583
564
///
584
- ///
585
565
pub fn zip(xs: List(a), ys: List(b)) -> List(tuple(a, b)) {
586
566
case xs, ys {
587
567
[], _ -> []
@@ -608,7 +588,6 @@ pub fn zip(xs: List(a), ys: List(b)) -> List(tuple(a, b)) {
608
588
/// > strict_zip([1, 2], [3, 4])
609
589
/// Ok([tuple(1, 3), tuple(2, 4)])
610
590
///
611
- ///
612
591
pub fn strict_zip(l1: List(a), l2: List(b)) -> Result(List(tuple(a, b)), LengthMismatch) {
613
592
case length(of: l1) == length(of: l2) {
614
593
True -> Ok(zip(l1, l2))
@@ -628,7 +607,6 @@ pub fn strict_zip(l1: List(a), l2: List(b)) -> Result(List(tuple(a, b)), LengthM
628
607
/// > intersperse([], 2)
629
608
/// []
630
609
///
631
- ///
632
610
pub fn intersperse(list: List(a), with elem: a) -> List(a) {
633
611
case list {
634
612
[] | [_] -> list
@@ -649,7 +627,6 @@ pub fn intersperse(list: List(a), with elem: a) -> List(a) {
649
627
/// > at([1, 2, 3], 5)
650
628
/// Error(Nil)
651
629
///
652
- ///
653
630
pub fn at(in list: List(a), get index: Int) -> Option(a) {
654
631
case index < 0 {
655
632
True -> result.none()
@@ -674,7 +651,6 @@ pub fn at(in list: List(a), get index: Int) -> Option(a) {
674
651
/// > unique([1, 1, 1, 4, 7, 3, 3, 4])
675
652
/// [1, 4, 7, 3]
676
653
///
677
- ///
678
654
pub fn unique(list: List(a)) -> List(a) {
679
655
case list {
680
656
[] -> []
@@ -713,6 +689,12 @@ fn do_sort(list: List(a), compare: fn(a, a) -> Order, list_length: Int) -> List(
713
689
/// Sort from smallest to largest based upon the ordering specified by a given
714
690
/// function.
715
691
///
692
+ /// ## Examples
693
+ ///
694
+ /// > import gleam/int
695
+ /// > list.sort([4, 3, 6, 5, 4, 1, 2], int.compare)
696
+ /// [1, 2, 3, 4, 4, 5, 6]
697
+ ///
716
698
pub fn sort(list: List(a), sort_by compare: fn(a, a) -> Order) -> List(a) {
717
699
do_sort(list, compare, length(list))
718
700
}
@@ -730,7 +712,6 @@ pub fn sort(list: List(a), sort_by compare: fn(a, a) -> Order) -> List(a) {
730
712
/// > range(1, -5)
731
713
/// [1, 0, -1, -2, -3, -4]
732
714
///
733
- ///
734
715
pub fn range(from start: Int, to stop: Int) -> List(Int) {
735
716
case int.compare(start, stop) {
736
717
order.Eq -> []
@@ -771,6 +752,22 @@ fn do_split(list: List(a), n: Int, taken: List(a)) -> tuple(List(a), List(a)) {
771
752
}
772
753
}
773
754
755
+ /// Split a list in two before the given index.
756
+ ///
757
+ /// If the list is not long enough to have the given index the before list will
758
+ /// be the input list, and the after list will be empty.
759
+ ///
760
+ /// ## Examples
761
+ ///
762
+ /// > split([6, 7, 8, 9], 0)
763
+ /// tuple([], [6, 7, 8, 9])
764
+ ///
765
+ /// > split([6, 7, 8, 9], 2)
766
+ /// tuple([6, 7], [8, 9])
767
+ ///
768
+ /// > split([6, 7, 8, 9], 4)
769
+ /// tuple([6, 7, 8, 9], [])
770
+ ///
774
771
pub fn split(list list: List(a), at index: Int) -> tuple(List(a), List(a)) {
775
772
do_split(list, index, [])
776
773
}
@@ -790,13 +787,47 @@ fn do_split_while(
790
787
}
791
788
}
792
789
790
+ /// Split a list in two before the first element that a given function returns
791
+ /// False for.
792
+ ///
793
+ /// If the function returns True for all elements the first list will be the
794
+ /// input list, and the second list will be empty.
795
+ ///
796
+ /// ## Examples
797
+ ///
798
+ /// > split_while([1, 2, 3, 4, 5], fn(x) { x <= 3 })
799
+ /// tuple([1, 2, 3], [4, 5])
800
+ ///
801
+ /// > split_while([1, 2, 3, 4, 5], fn(x) { x <= 5 })
802
+ /// tuple([1, 2, 3, 4, 5], [])
803
+ ///
793
804
pub fn split_while(
794
805
list list: List(a),
795
806
while predicate: fn(a) -> Bool,
796
807
) -> tuple(List(a), List(a)) {
797
808
do_split_while(list, predicate, [])
798
809
}
799
810
811
+
812
+ /// Given a list of 2 element tuples, find the first tuple that has a given
813
+ /// key as the first element and return the second element.
814
+ ///
815
+ /// If no tuple is found with the given key then `Error(Nil)` is returned.
816
+ ///
817
+ /// This function may be useful for interacting with Erlang code where lists of
818
+ /// tuples are common.
819
+ ///
820
+ /// ## Examples
821
+ ///
822
+ /// > key_find([tuple("a", 0), tuple("b", 1)], "a")
823
+ /// Ok(0)
824
+ ///
825
+ /// > key_find([tuple("a", 0), tuple("b", 1)], "b")
826
+ /// Ok(1)
827
+ ///
828
+ /// > key_find([tuple("a", 0), tuple("b", 1)], "c")
829
+ /// Error(Nil)
830
+ ///
800
831
pub fn key_find(
801
832
in keyword_list: List( tuple( k, v ) ) ,
802
833
find desired_key: k,
0 commit comments