diff options
Diffstat (limited to 'src/list.gleam')
-rw-r--r-- | src/list.gleam | 179 |
1 files changed, 6 insertions, 173 deletions
diff --git a/src/list.gleam b/src/list.gleam index 5ccdda6..22da117 100644 --- a/src/list.gleam +++ b/src/list.gleam @@ -1,11 +1,8 @@ -import expect - // TODO: all // TODO: any // TODO: at // TODO: concat // TODO: index_map -// TODO: index_map // TODO: intersperse // TODO: sort // TODO: unique @@ -21,44 +18,21 @@ pub enum NotFound = // pub external fn length(List(a)) -> Int = "erlang" "length" -test length { - length([]) |> expect:equal(_, 0) - length([1]) |> expect:equal(_, 1) - length([1, 1]) |> expect:equal(_, 2) - length([1, 1, 1]) |> expect:equal(_, 3) -} - // Using the Erlang C BIF implementation. // pub external fn reverse(List(a)) -> List(a) = "lists" "reverse" -test reverse { - length([]) |> expect:equal(_, 0) - length([1, 2, 3, 4, 5]) |> expect:equal(_, 5) -} - pub fn is_empty(list) { list == [] } -test is_empty { - is_empty([]) |> expect:true - is_empty([1]) |> expect:false -} - -pub fn has_member(list, elem) { +pub fn contains(list, elem) { case list { | [] -> False - | [head | rest] -> head == elem || has_member(rest, elem) + | [head | rest] -> head == elem || contains(rest, elem) } } -test has_member { - has_member([0, 4, 5, 1], 1) |> expect:true - has_member([0, 4, 5, 7], 1) |> expect:false - has_member([], 1) |> expect:false -} - pub fn head(list) { case list { | [] -> Error(Empty) @@ -66,14 +40,6 @@ pub fn head(list) { } } -test head { - head([0, 4, 5, 7]) - |> expect:equal(_, Ok(0)) - - head([]) - |> expect:equal(_, Error(Empty)) -} - pub fn tail(list) { case list { | [] -> Error(Empty) @@ -81,17 +47,6 @@ pub fn tail(list) { } } -test tail { - tail([0, 4, 5, 7]) - |> expect:equal(_, Ok([4, 5, 7])) - - tail([0]) - |> expect:equal(_, Ok([])) - - tail([]) - |> expect:equal(_, Error(Empty)) -} - fn do_filter(list, fun, acc) { case list { | [] -> reverse(acc) @@ -109,24 +64,6 @@ pub fn filter(list, fun) { do_filter(list, fun, []) } -test filter { - [] - |> filter(_, fn(_) { True }) - |> expect:equal(_, []) - - [0, 4, 5, 7, 3] - |> filter(_, fn(_) { True }) - |> expect:equal(_, [0, 4, 5, 7, 3]) - - [0, 4, 5, 7, 3] - |> filter(_, fn(x) { x > 4 }) - |> expect:equal(_, [5, 7]) - - [0, 4, 5, 7, 3] - |> filter(_, fn(x) { x < 4 }) - |> expect:equal(_, [0, 3]) -} - fn do_map(list, fun, acc) { case list { | [] -> reverse(acc) @@ -138,16 +75,6 @@ pub fn map(list, fun) { do_map(list, fun, []) } -test map { - [] - |> map(_, fn(x) { x * 2 }) - |> expect:equal(_, []) - - [0, 4, 5, 7, 3] - |> map(_, fn(x) { x * 2 }) - |> expect:equal(_, [0, 8, 10, 14, 6]) -} - fn do_traverse(list, fun, acc) { case list { | [] -> Ok(reverse(acc)) @@ -163,24 +90,6 @@ pub fn traverse(list, fun) { do_traverse(list, fun, []) } -test traverse { - let fun = fn(x) { - case x == 6 || x == 5 || x == 4 { - | True -> Ok(x * 2) - | False -> Error(x) - } - } - - [5, 6, 5, 6] - |> traverse(_, fun) - |> expect:equal(_, Ok([10, 12, 10, 12])) - - [4, 6, 5, 7, 3] - |> traverse(_, fun) - |> expect:equal(_, Error(7)) -} - - pub fn drop(list, n) { case n <= 0 { | True -> list @@ -192,16 +101,6 @@ pub fn drop(list, n) { } } -test drop { - [] - |> drop(_, 5) - |> expect:equal(_, []) - - [1, 2, 3, 4, 5, 6, 7, 8] - |> drop(_, 5) - |> expect:equal(_, [6, 7, 8]) -} - fn do_take(list, n, acc) { case n <= 0 { | True -> reverse(acc) @@ -217,32 +116,12 @@ pub fn take(list, n) { do_take(list, n, []) } -test take { - [] - |> take(_, 5) - |> expect:equal(_, []) - [1, 2, 3, 4, 5, 6, 7, 8] - |> take(_, 5) - |> expect:equal(_, [1, 2, 3, 4, 5]) -} - pub fn new() { [] } -test new { - new() |> expect:equal(_, []) -} - pub external fn append(List(a), List(a)) -> List(a) = "lists" "append"; -test append { - expect:equal( - append([1], [2, 3]), - [1, 2, 3], - ) -} - fn do_flatten(lists, acc) { case lists { | [] -> acc @@ -254,46 +133,20 @@ pub fn flatten(lists) { do_flatten(lists, []) } -test flatten { - flatten([]) - |> expect:equal(_, []) - - flatten([[]]) - |> expect:equal(_, []) - - flatten([[], [], []]) - |> expect:equal(_, []) - - flatten([[1, 2], [], [3, 4]]) - |> expect:equal(_, [1, 2, 3, 4]) -} - -pub fn foldl(list, acc, fun) { +pub fn fold(list, acc, fun) { case list { | [] -> acc - | [x | rest] -> foldl(rest, fun(x, acc), fun) + | [x | rest] -> fold(rest, fun(x, acc), fun) } } -test foldl { - [1, 2, 3] - |> foldl(_, [], fn(x, acc) { [x | acc] }) - |> expect:equal(_, [3, 2, 1]) -} - -pub fn foldr(list, acc, fun) { +pub fn fold_right(list, acc, fun) { case list { | [] -> acc - | [x | rest] -> fun(x, foldr(rest, acc, fun)) + | [x | rest] -> fun(x, fold_right(rest, acc, fun)) } } -test foldr { - [1, 2, 3] - |> foldr(_, [], fn(x, acc) { [x | acc] }) - |> expect:equal(_, [1, 2, 3]) -} - pub fn find(haystack, f) { case haystack { | [] -> Error(NotFound) @@ -305,23 +158,3 @@ pub fn find(haystack, f) { } } -test find { - let f = fn(x) { - case x { - | 2 -> Ok(4) - | _ -> Error(NotFound) - } - } - - [1, 2, 3] - |> find(_, f) - |> expect:equal(_, Ok(4)) - - [1, 3, 2] - |> find(_, f) - |> expect:equal(_, Ok(4)) - - [1, 3] - |> find(_, f) - |> expect:equal(_, Error(NotFound)) -} |