Skip to content

Commit 3482f30

Browse files
committed
Remaining map docs
1 parent 8a8cfc6 commit 3482f30

File tree

3 files changed

+195
-31
lines changed

3 files changed

+195
-31
lines changed

src/gleam/list.gleam

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ pub type LengthMismatch {
5151
/// > length([1, 2])
5252
/// 2
5353
///
54-
///
5554
pub external fn length(of: List(a)) -> Int = "erlang" "length"
5655

5756
/// 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"
7473
/// > reverse([1, 2])
7574
/// [2, 1]
7675
///
77-
///
7876
pub external fn reverse(List(a)) -> List(a) = "lists" "reverse"
7977

8078
/// Determine whether or not the list is empty.
@@ -92,7 +90,6 @@ pub external fn reverse(List(a)) -> List(a) = "lists" "reverse"
9290
/// > is_empty([1, 1])
9391
/// False
9492
///
95-
///
9693
pub fn is_empty(list: List(a)) -> Bool {
9794
list == []
9895
}
@@ -119,7 +116,6 @@ pub fn is_empty(list: List(a)) -> Bool {
119116
/// > contains([1, 0], 0)
120117
/// True
121118
///
122-
///
123119
pub fn contains(list: List(a), has elem: a) -> Bool {
124120
case list {
125121
[] -> False
@@ -140,7 +136,6 @@ pub fn contains(list: List(a), has elem: a) -> Bool {
140136
/// > head([1, 2])
141137
/// Ok(1)
142138
///
143-
///
144139
pub fn head(list: List(a)) -> Option(a) {
145140
case list {
146141
[] -> result.none()
@@ -164,7 +159,6 @@ pub fn head(list: List(a)) -> Option(a) {
164159
/// > tail([1, 2])
165160
/// Ok([2])
166161
///
167-
///
168162
pub fn tail(list: List(a)) -> Option(List(a)) {
169163
case list {
170164
[] -> result.none()
@@ -196,7 +190,6 @@ fn do_filter(list: List(a), fun: fn(a) -> Bool, acc: List(a)) -> List(a) {
196190
/// > filter([2, 4, 6, 1], fn(x) { x > 6 })
197191
/// []
198192
///
199-
///
200193
pub fn filter(list: List(a), for predicate: fn(a) -> Bool) -> List(a) {
201194
do_filter(list, predicate, [])
202195
}
@@ -216,7 +209,6 @@ fn do_map(list: List(a), fun: fn(a) -> b, acc: List(b)) -> List(b) {
216209
/// > map([2, 4, 6], fn(x) { x * 2 })
217210
/// [4, 8, 12]
218211
///
219-
///
220212
pub fn map(list: List(a), with fun: fn(a) -> b) -> List(b) {
221213
do_map(list, fun, [])
222214
}
@@ -244,7 +236,6 @@ fn do_index_map(
244236
/// > index_map(["a", "b"], fn(i, x) { tuple(i, x) })
245237
/// [tuple(0, "a"), tuple(1, "b")]
246238
///
247-
///
248239
pub fn index_map(list: List(a), with fun: fn(Int, a) -> b) -> List(b) {
249240
do_index_map(list, fun, 0, [])
250241
}
@@ -289,7 +280,6 @@ fn do_traverse(
289280
/// > traverse([[1], [], [2]], head)
290281
/// Error(Nil)
291282
///
292-
///
293283
pub fn traverse(
294284
list: List(a),
295285
with fun: fn(a) -> Result(b, e),
@@ -313,7 +303,6 @@ pub fn traverse(
313303
/// > drop([1, 2, 3, 4], 9)
314304
/// []
315305
///
316-
///
317306
pub fn drop(from list: List(a), up_to n: Int) -> List(a) {
318307
case n <= 0 {
319308
True -> list
@@ -352,7 +341,6 @@ fn do_take(list: List(a), n: Int, acc: List(a)) -> List(a) {
352341
/// > take([1, 2, 3, 4], 9)
353342
/// [1, 2, 3, 4]
354343
///
355-
///
356344
pub fn take(from list: List(a), up_to n: Int) -> List(a) {
357345
do_take(list, n, [])
358346
}
@@ -364,7 +352,6 @@ pub fn take(from list: List(a), up_to n: Int) -> List(a) {
364352
/// > new()
365353
/// []
366354
///
367-
///
368355
pub fn new() -> List(a) {
369356
[]
370357
}
@@ -379,7 +366,6 @@ pub fn new() -> List(a) {
379366
/// > append([1, 2], [3])
380367
/// [1, 2, 3]
381368
///
382-
///
383369
pub external fn append(List(a), List(a)) -> List(a)
384370
= "lists" "append";
385371

@@ -400,7 +386,6 @@ fn do_flatten(lists: List(List(a)), acc: List(a)) -> List(a) {
400386
/// > flatten([[1], [2, 3], []])
401387
/// [1, 2, 3]
402388
///
403-
///
404389
pub fn flatten(lists: List(List(a))) -> List(a) {
405390
do_flatten(lists, [])
406391
}
@@ -458,7 +443,6 @@ pub fn fold_right(
458443
/// > find([], fn(x) { True })
459444
/// Error(Nil)
460445
///
461-
///
462446
pub fn find(
463447
in haystack: List(a),
464448
one_that is_desired: fn(a) -> Bool,
@@ -490,7 +474,6 @@ pub fn find(
490474
/// > find_map([], head)
491475
/// Error(Nil)
492476
///
493-
///
494477
pub fn find_map(
495478
in haystack: List(a),
496479
with fun: fn(a) -> Option(b),
@@ -520,7 +503,6 @@ pub fn find_map(
520503
/// > all([4, 3], fn(x) { x > 3 })
521504
/// False
522505
///
523-
///
524506
pub fn all(in list: List(a), satisfying predicate: fn(a) -> Bool) -> Bool {
525507
case list {
526508
[] -> True
@@ -550,7 +532,6 @@ pub fn all(in list: List(a), satisfying predicate: fn(a) -> Bool) -> Bool {
550532
/// > any([3, 4], fn(x) { x > 3 })
551533
/// True
552534
///
553-
///
554535
pub fn any(in list: List(a), satisfying predicate: fn(a) -> Bool) -> Bool {
555536
case list {
556537
[] -> False
@@ -581,7 +562,6 @@ pub fn any(in list: List(a), satisfying predicate: fn(a) -> Bool) -> Bool {
581562
/// > zip([1, 2], [3, 4])
582563
/// [tuple(1, 3), tuple(2, 4)]
583564
///
584-
///
585565
pub fn zip(xs: List(a), ys: List(b)) -> List(tuple(a, b)) {
586566
case xs, ys {
587567
[], _ -> []
@@ -608,7 +588,6 @@ pub fn zip(xs: List(a), ys: List(b)) -> List(tuple(a, b)) {
608588
/// > strict_zip([1, 2], [3, 4])
609589
/// Ok([tuple(1, 3), tuple(2, 4)])
610590
///
611-
///
612591
pub fn strict_zip(l1: List(a), l2: List(b)) -> Result(List(tuple(a, b)), LengthMismatch) {
613592
case length(of: l1) == length(of: l2) {
614593
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
628607
/// > intersperse([], 2)
629608
/// []
630609
///
631-
///
632610
pub fn intersperse(list: List(a), with elem: a) -> List(a) {
633611
case list {
634612
[] | [_] -> list
@@ -649,7 +627,6 @@ pub fn intersperse(list: List(a), with elem: a) -> List(a) {
649627
/// > at([1, 2, 3], 5)
650628
/// Error(Nil)
651629
///
652-
///
653630
pub fn at(in list: List(a), get index: Int) -> Option(a) {
654631
case index < 0 {
655632
True -> result.none()
@@ -674,7 +651,6 @@ pub fn at(in list: List(a), get index: Int) -> Option(a) {
674651
/// > unique([1, 1, 1, 4, 7, 3, 3, 4])
675652
/// [1, 4, 7, 3]
676653
///
677-
///
678654
pub fn unique(list: List(a)) -> List(a) {
679655
case list {
680656
[] -> []
@@ -713,6 +689,12 @@ fn do_sort(list: List(a), compare: fn(a, a) -> Order, list_length: Int) -> List(
713689
/// Sort from smallest to largest based upon the ordering specified by a given
714690
/// function.
715691
///
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+
///
716698
pub fn sort(list: List(a), sort_by compare: fn(a, a) -> Order) -> List(a) {
717699
do_sort(list, compare, length(list))
718700
}
@@ -730,7 +712,6 @@ pub fn sort(list: List(a), sort_by compare: fn(a, a) -> Order) -> List(a) {
730712
/// > range(1, -5)
731713
/// [1, 0, -1, -2, -3, -4]
732714
///
733-
///
734715
pub fn range(from start: Int, to stop: Int) -> List(Int) {
735716
case int.compare(start, stop) {
736717
order.Eq -> []
@@ -771,6 +752,22 @@ fn do_split(list: List(a), n: Int, taken: List(a)) -> tuple(List(a), List(a)) {
771752
}
772753
}
773754

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+
///
774771
pub fn split(list list: List(a), at index: Int) -> tuple(List(a), List(a)) {
775772
do_split(list, index, [])
776773
}
@@ -790,13 +787,47 @@ fn do_split_while(
790787
}
791788
}
792789

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+
///
793804
pub fn split_while(
794805
list list: List(a),
795806
while predicate: fn(a) -> Bool,
796807
) -> tuple(List(a), List(a)) {
797808
do_split_while(list, predicate, [])
798809
}
799810

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+
///
800831
pub fn key_find(
801832
in keyword_list: List(tuple(k, v)),
802833
find desired_key: k,

0 commit comments

Comments
 (0)