add examples to builtin List

This commit is contained in:
HajagosNorbert 2023-06-13 19:51:19 +02:00
parent 232e9030ae
commit fe2e175d0f
No known key found for this signature in database
GPG key ID: 807F4444870DB673

View file

@ -359,6 +359,10 @@ releaseExcessCapacity : List a -> List a
concat : List a, List a -> List a concat : List a, List a -> List a
## Returns the last element in the list, or `ListWasEmpty` if it was empty. ## Returns the last element in the list, or `ListWasEmpty` if it was empty.
## ```
## expect List.last [1, 2, 3] == Ok 3
## expect List.last [] == Err ListWasEmpty
## ```
last : List a -> Result a [ListWasEmpty] last : List a -> Result a [ListWasEmpty]
last = \list -> last = \list ->
when List.get list (Num.subSaturated (List.len list) 1) is when List.get list (Num.subSaturated (List.len list) 1) is
@ -390,7 +394,7 @@ repeatHelp = \value, count, accum ->
## Returns the list with its elements reversed. ## Returns the list with its elements reversed.
## ``` ## ```
## List.reverse [1, 2, 3] ## expect List.reverse [1, 2, 3] == [3, 2, 1]
## ``` ## ```
reverse : List a -> List a reverse : List a -> List a
reverse = \list -> reverse = \list ->
@ -404,9 +408,9 @@ reverseHelp = \list, left, right ->
## Join the given lists together into one list. ## Join the given lists together into one list.
## ``` ## ```
## List.join [[1, 2, 3], [4, 5], [], [6, 7]] ## expect List.join [[1], [2, 3], [], [4, 5]] == [1, 2, 3, 4, 5]
## List.join [[], []] ## expect List.join [[], []] == []
## List.join [] ## expect List.join [] == []
## ``` ## ```
join : List (List a) -> List a join : List (List a) -> List a
join = \lists -> join = \lists ->
@ -604,6 +608,10 @@ dropIf = \list, predicate ->
## Run the given function on each element of a list, and return the ## Run the given function on each element of a list, and return the
## number of elements for which the function returned `Bool.true`. ## number of elements for which the function returned `Bool.true`.
## ```
## expect List.countIf [1, -2, -3] Num.isNegative == 2
## expect List.countIf [1, 2, 3] (\num -> num > 1 ) == 2
## ```
countIf : List a, (a -> Bool) -> Nat countIf : List a, (a -> Bool) -> Nat
countIf = \list, predicate -> countIf = \list, predicate ->
walkState = \state, elem -> walkState = \state, elem ->
@ -617,11 +625,12 @@ countIf = \list, predicate ->
## This works like [List.map], except only the transformed values that are ## This works like [List.map], except only the transformed values that are
## wrapped in `Ok` are kept. Any that are wrapped in `Err` are dropped. ## wrapped in `Ok` are kept. Any that are wrapped in `Err` are dropped.
## ``` ## ```
## List.keepOks [["a", "b"], [], [], ["c", "d", "e"]] List.last ## expect List.keepOks ["1", "Two", "23", "Bird"] Str.toI32 == [1, 23]
## ##
## fn = \str -> if Str.isEmpty str then Err StrWasEmpty else Ok (Str.len str) ## expect List.keepOks [["a", "b"], [], ["c", "d", "e"], [] ] List.first == ["a", "c"]
## ##
## List.keepOks ["", "a", "bc", "", "d", "ef", ""] ## fn = \str -> if Str.isEmpty str then Err StrWasEmpty else Ok str
## expect List.keepOks ["", "a", "bc", "", "d", "ef", ""] fn == ["a", "bc", "d", "ef"]
## ``` ## ```
keepOks : List before, (before -> Result after *) -> List after keepOks : List before, (before -> Result after *) -> List after
keepOks = \list, toResult -> keepOks = \list, toResult ->
@ -653,9 +662,9 @@ keepErrs = \list, toResult ->
## Convert each element in the list to something new, by calling a conversion ## Convert each element in the list to something new, by calling a conversion
## function on each of them. Then return a new list of the converted values. ## function on each of them. Then return a new list of the converted values.
## ``` ## ```
## List.map [1, 2, 3] (\num -> num + 1) ## expect List.map [1, 2, 3] (\num -> num + 1) == [2, 3, 4]
## ##
## List.map ["", "a", "bc"] Str.isEmpty ## expect List.map ["", "a", "bc"] Str.isEmpty == [Bool.true, Bool.false, Bool.false]
## ``` ## ```
map : List a, (a -> b) -> List b map : List a, (a -> b) -> List b
@ -682,6 +691,9 @@ map4 : List a, List b, List c, List d, (a, b, c, d -> e) -> List e
## This works like [List.map], except it also passes the index ## This works like [List.map], except it also passes the index
## of the element to the conversion function. ## of the element to the conversion function.
## ```
## expect List.mapWithIndex [10, 20, 30] (\num, index -> num + index) == [10, 21, 32]
## ```
mapWithIndex : List a, (a, Nat -> b) -> List b mapWithIndex : List a, (a, Nat -> b) -> List b
mapWithIndex = \src, func -> mapWithIndex = \src, func ->
length = len src length = len src