diff options
author | Louis Pilfold <louis@lpil.uk> | 2018-06-29 16:07:23 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2018-06-29 16:26:17 +0100 |
commit | 31bea4c96bc2c412ad415c2ea16537364f920c1d (patch) | |
tree | 4f47c5d1206cf45e2d44638d74532cf1599fd141 | |
parent | 089e8498907739c198166d7d7f473784e4d48bc3 (diff) | |
download | gleam_stdlib-31bea4c96bc2c412ad415c2ea16537364f920c1d.tar.gz gleam_stdlib-31bea4c96bc2c412ad415c2ea16537364f920c1d.zip |
Brace based syntax
-rw-r--r-- | src/Bool.gleam | 49 | ||||
-rw-r--r-- | src/List.gleam | 145 | ||||
-rw-r--r-- | src/Maybe.gleam | 52 | ||||
-rw-r--r-- | src/Order.gleam | 49 | ||||
-rw-r--r-- | src/Result.gleam | 82 |
5 files changed, 246 insertions, 131 deletions
diff --git a/src/Bool.gleam b/src/Bool.gleam index 563b03e..b5caacc 100644 --- a/src/Bool.gleam +++ b/src/Bool.gleam @@ -2,59 +2,74 @@ module Bool exposing Bool(..), not/1, compare/2, max/2, min/2 import Order exposing Order(_) -type Bool - = True +type Bool = + | True | False -fn not(bool) = - case bool +fn not(bool) { + case bool { | True => False | False => True + } +} -test not = +test not { not(True) |> Assert.false not(False) |> Assert.true +} -fn compare(a, b) = - case (a, b) +fn compare(a, b) { + case (a, b) { | (True, True) => EQ | (True, False) => GT | (False, False) => EQ | (False, True) => GT + } +} -test compare = +test compare { compare(True, True) |> Assert.equal(_, EQ) compare(True, False) |> Assert.equal(_, GT) compare(False, False) |> Assert.equal(_, LT) compare(False, True) |> Assert.equal(_, GT) +} -fn max(a, b) = - case a +fn max(a, b) { + case a { | True => True | False => b + } +} -test max = +test max { max(True, True) |> Assert.equal(_, True) max(True, False) |> Assert.equal(_, True) max(False, False) |> Assert.equal(_, False) max(False, True) |> Assert.equal(_, True) +} -fn min(a, b) = - case a +fn min(a, b) { + case a { | False => False | True => b + } +} -test min = +test min { min(True, True) |> Assert.equal(_, True) min(True, False) |> Assert.equal(_, False) min(False, False) |> Assert.equal(_, False) min(False, True) |> Assert.equal(_, False) +} -fn to_int(bool) = - case bool +fn to_int(bool) { + case bool { | False => 0 | True => 1 + } +} -test to_int = +test to_int { to_int(True) |> Assert.equal(_, 1) to_int(False) |> Assert.equal(_, 0) +} diff --git a/src/List.gleam b/src/List.gleam index 4505a74..1ca291b 100644 --- a/src/List.gleam +++ b/src/List.gleam @@ -8,158 +8,201 @@ import Maybe exposing Maybe(..) // external length : |List(a)| -> Int = :erlang.length -test length = +test length { length([]) |> Assert.equal(_, 0) length([1]) |> Assert.equal(_, 1) length([1, 1]) |> Assert.equal(_, 2) length([1, 1, 1]) |> Assert.equal(_, 3) +} // Using the Erlang C BIF implementation. // external reverse : |List(a)| -> List(a) = :erlang.reverse -test reverse = +test reverse { length([]) |> Assert.equal(_, []) length([1, 2, 3, 4, 5]) |> Assert.equal(_, [5, 4, 3, 2, 1]) +} -fn is_empty(list) = +fn is_empty(list) { list == [] +} -test is_empty = +test is_empty { is_empty([]) |> Assert.true is_empty([1]) |> Assert.false +} -fn member(list, elem) = - case list +fn member(list, elem) { + case list { | [] => False | elem :: _ => True | _ :: rest => member(rest, elem) + } +} -test is_member = +test is_member { is_member([0, 4, 5, 1], 1) |> Assert.true is_member([0, 4, 5, 7], 1) |> Assert.false is_member([], 1) |> Assert.false +} -fn head(list) = - case list +fn head(list) { + case list { | [] => Nothing | elem :: _ => Just(elem) + } +} -test head = +test head { head([0, 4, 5, 7]) |> Assert.equal(_, Just(0)) head([]) |> Assert.equal(_, Nothing) +} -fn tail(list) = - case list +fn tail(list) { + case list { | [] => Nothing | _ :: rest => Just(rest) + } +} -test tail = +test tail { tail([0, 4, 5, 7]) |> Assert.equal(_, Just([4, 5, 7])) tail([0]) |> Assert.equal(_, Just([])) tail([]) |> Assert.equal(_, Nothing) +} -fn filter(list, fun) = +fn filter(list, fun) { filter(list, fun, []) +} -test filter = +test filter { filter([], |x| True) |> Assert.equal(_, []) filter([0, 4, 5, 7, 3], |x| True) |> Assert.equal(_, [0, 4, 5, 7, 3]) filter([0, 4, 5, 7, 3], |x| x > 4) |> Assert.equal(_, [5, 7]) filter([0, 4, 5, 7, 3], |x| x < 4) |> Assert.equal(_, [0, 3]) +} -fn filter(list, fun, acc) = - case list +fn filter(list, fun, acc) { + case list { | [] => reverse(acc) - | x :: xs => ( - new_acc = case fun(x) + | x :: xs => + new_acc = + case fun(x) { | True => x :: acc | False => acc + } filter(xs, fun, new_acc) - ) + } +} -fn map(list, fun) = +fn map(list, fun) { map(list, fun, []) +} -test map = +test map { map([], |x| * 2) |> Assert.equal(_, []) map([0, 4, 5, 7, 3], |x| x * 2) |> Assert.equal(_, [0, 8, 10, 14, 6]) +} -fn map(list, fun, acc) = - case list +fn map(list, fun, acc) { + case list { | [] => reverse(acc) | x :: xs => map(xs, fun, fun(x) :: acc) + } +} -fn drop(list, n) = - case n <= 0 +fn drop(list, n) { + case n <= 0 { | True => list - | False => ( - case list + | False => + case list { | [] => [] | _ :: xs => drop(xs, n - 1) - ) + } + } +} -test drop/2 = +test drop/2 { drop([], 5) |> Assert.equal(_, []) drop([1, 2, 3, 4, 5, 6, 7, 8], 5) |> Assert.equal(_, [6, 7, 8]) +} -fn take(list, n) = +fn take(list, n) { take(list, n, []) +} -fn take(list, n, acc) = - case n <= 0 +fn take(list, n, acc) { + case n <= 0 { | True => reverse(acc) - | False => ( - case list + | False => + case list { | [] => reverse(acc) | x :: xs => take(xs, n - 1, x :: acc) - ) + } + } +} -test take = +test take { take([], 5) |> Assert.equal(_, []) take([1, 2, 3, 4, 5, 6, 7, 8], 5) |> Assert.equal(_, [1, 2, 3, 4, 5]) +} -fn of(x) = +fn of(x) { [x] +} -test of() = +test of() { of([]) |> Assert.equal(_, [[]]) of(1) |> Assert.equal(_, [1]) +} -fn new() = +fn new() { [] +} -test new() = +test new() { new() |> Assert.equal(_, []) +} -fn flatten(lists) = +fn flatten(lists) { flatten(lists, []) +} -test flatten() = +test flatten() { flatten([]) |> Assert.equal(_, []) flatten([[]]) |> Assert.equal(_, []) flatten([[], [], []]) |> Assert.equal(_, []) flatten([[1, 2], [], [3, 4]]) |> Assert.equal(_, [1, 2, 3, 4]) +} -fn flatten(lists, acc) = - case lists +fn flatten(lists, acc) { + case lists { | [] => acc | l :: rest => flatten(rest, acc ++ l) + } +} -fn foldl(list, acc, fun) = - case list +fn foldl(list, acc, fun) { + case list { | [] => acc | x :: rest => foldl(rest, fun(x, acc), fun) + } +} -test foldl() = +test foldl() { foldl([1, 2, 3], [], |x, acc| x :: acc) |> Assert.equal(_, [3, 2, 1]) +} -fn foldr(list, acc, fun) = - case list +fn foldr(list, acc, fun) { + case list { | [] => acc | x :: rest => fun(x, foldl(rest, acc, fun)) + } +} -test foldr() = +test foldr() { foldr([1, 2, 3], [], |x, acc| x :: acc) |> Assert.equal(_, [1, 2, 3]) +} diff --git a/src/Maybe.gleam b/src/Maybe.gleam index a1790e1..57ba9af 100644 --- a/src/Maybe.gleam +++ b/src/Maybe.gleam @@ -2,61 +2,77 @@ module Maybe exposing Maybe(..), is_just/1, is_nothing/1, map/2, flatten/1, flat_map/2, unwrap/2 -type Maybe(x) - = Just(x) +type Maybe(x) = + | Just(x) | Nothing -fn is_just(maybe) = - case maybe +fn is_just(maybe) { + case maybe { | Just(_) => True | Nothing => False + } +} -test is_just() = +test is_just() { is_just(Just(1)) |> Assert.true is_just(Nothing) |> Assert.false +} -fn is_nothing(maybe) = - case maybe +fn is_nothing(maybe) { + case maybe { | Just(_) => False | Nothing => True + } +} -test is_nothing() = +test is_nothing() { is_nothing(Just(1)) |> Assert.false is_nothing(Nothing) |> Assert.true +} -fn map(maybe, fun) = - case maybe +fn map(maybe, fun) { + case maybe { | Just(x) => fun(x) | Nothing => Nothing + } +} -test map() = +test map() { map(Just(1), |x| x + 1) |> Assert.equal(_, Just(2)) map(Nothing, |x| x + 1) |> Assert.equal(Nothing) +} -fn flatten(maybe) = +fn flatten(maybe) { maybe |> unwrap(_, Nothing) +} -test flatten() = +test flatten() { flatten(Just(Just(1))) |> Assert.equal(Just(1)) flatten(Just(Nothing)) |> Assert.equal(Nothing) flatten(Nothing) |> Assert.equal(Nothing) +} -fn flat_map(maybe, fun) = +fn flat_map(maybe, fun) { maybe |> map(_, fun) |> flatten +} -test flat_map() = +test flat_map() { flat_map(Nothing, |x| Just(x + 1)) |> Assert.equal(Nothing) flat_map(Just(1), |x| Just(x + 1)) |> Assert.equal(Just(2)) flat_map(Just(1), |_| Nothing) |> Assert.equal(Nothing) +} -fn unwrap(maybe, fallback) = - case maybe +fn unwrap(maybe, fallback) { + case maybe { | Just(v) => v | Nothing => fallback + } +} -test unwrap() = +test unwrap() { unwrap(Just(1), 50) |> Assert.equal(1) unwrap(Nothing, 50) |> Assert.equal(50) +} diff --git a/src/Order.gleam b/src/Order.gleam index 24b1a3b..a5faeb9 100644 --- a/src/Order.gleam +++ b/src/Order.gleam @@ -1,42 +1,50 @@ module Order exposing Order(..), reverse/1, to_int/1, compare/2, max/2, min/2 -type Order - = LT +type Order = + | LT | EQ | GT -fn reverse(order) = - case order +fn reverse(order) { + case order { | LT => GT | EQ => EQ | GT => LT + } +} -test reverse = +test reverse { reverse(LT) |> Assert.equal(_, GT) reverse(EQ) |> Assert.equal(_, EQ) reverse(GT) |> Assert.equal(_, LT) +} -fn to_int(order) = - case order +fn to_int(order) { + case order { | LT => -1 | EQ => 0 | GT => 1 + } +} -test to_int = +test to_int { to_int(LT) |> Assert.equal(_, -1) to_int(EQ) |> Assert.equal(_, 0) to_int(GT) |> Assert.equal(_, 1) +} -fn compare(a, b) = - case (a, b) +fn compare(a, b) { + case (a, b) { | (LT, LT) => EQ | (LT, _) => LT | (EQ, EQ) => EQ | (GT, GT) => EQ | (EQ, GT) => LT | _ => GT + } +} -test compare = +test compare { compare(LT, LT) |> Assert.equal(_, EQ) compare(LT, EQ) |> Assert.equal(_, LT) compare(LT, GT) |> Assert.equal(_, LT) @@ -46,14 +54,17 @@ test compare = compare(GT, LT) |> Assert.equal(_, GT) compare(GT, EQ) |> Assert.equal(_, GT) compare(GT, GT) |> Assert.equal(_, EQ) +} -fn max(a, b) = - case (a, b) +fn max(a, b) { + case (a, b) { | (GT, _) => GT | (EQ, LT) => EQ | _ => b + } +} -test max = +test max { max(LT, LT) |> Assert.equal(_, LT) max(LT, EQ) |> Assert.equal(_, EQ) max(LT, GT) |> Assert.equal(_, GT) @@ -63,14 +74,17 @@ test max = max(GT, LT) |> Assert.equal(_, GT) max(GT, EQ) |> Assert.equal(_, GT) max(GT, GT) |> Assert.equal(_, GT) +} -fn min(a, b) = - case (a, b) +fn min(a, b) { + case (a, b) { | (LT, _) => LT | (EQ, GT) => EQ | _ => b + } +} -test min = +test min { min(LT, LT) |> Assert.equal(_, LT) min(LT, EQ) |> Assert.equal(_, LT) min(LT, GT) |> Assert.equal(_, LT) @@ -80,3 +94,4 @@ test min = min(GT, LT) |> Assert.equal(_, LT) min(GT, EQ) |> Assert.equal(_, EQ) min(GT, GT) |> Assert.equal(_, GT) +} diff --git a/src/Result.gleam b/src/Result.gleam index b5b4185..68869f7 100644 --- a/src/Result.gleam +++ b/src/Result.gleam @@ -8,89 +8,115 @@ doc """ Result represents the result of something that may succeed or fail. `Ok` means it was successful, `Error` means it failed. """ -type Result(error, value) - = Ok(value) +type Result(error, value) { + | Ok(value) | Error(error) -fn is_ok(result) = - case result +fn is_ok(result) { + case result { | Error(_) => False | Ok(_) => True + } +} -test is_ok = +test is_ok { is_ok(Ok(1)) |> Assert.true is_ok(Error(1)) |> Assert.false +} -fn is_error(result) = - case result +fn is_error(result) { + case result { | Ok(_) => False | Error(_) => True + } +} -test is_error = +test is_error { is_error(Ok(1)) |> Assert.false is_error(Error(1)) |> Assert.true +} -fn map(result, fun) = - case result +fn map(result, fun) { + case result { | Ok(x) => fun(x) | Error(_) => result + } +} -test map = +test map { map(Ok(1), |x| x + 1) |> Assert.equal(_, Ok(2)) map(Error(1), |x| x + 1) |> Assert.equal(Error(1)) +} -fn map_error(result, fun) = - case result +fn map_error(result, fun) { + case result { | Ok(_) => result | Error(error) => error |> fun |> Error + } +} -test map_error = +test map_error { map_error(Ok(1), |x| x + 1) |> Assert.equal(_, Ok(1)) map_error(Error(1), |x| x + 1) |> Assert.equal(Error(2)) +} -fn flatten(result) = - case result +fn flatten(result) { + case result { | Ok(x) => x | Error(_) => result + } +} -test flatten = +test flatten { flatten(Ok(Ok(1))) |> Assert.equal(_, Ok(1)) flatten(Ok(Error(1))) |> Assert.equal(_, Error(1)) flatten(Error(1)) |> Assert.equal(_, Error(1)) +} -fn flat_map(result, fun) = +fn flat_map(result, fun) { result |> unwrap(_, fun) |> flatten +} -test flat_map = +test flat_map { flat_map(Error(1), |x| Ok(x + 1)) |> Assert.equal(_, Error(1)) flat_map(Ok(1), |x| Ok(x + 1)) |> Assert.equal(_, Ok(2)) flat_map(Ok(1), |_| Error(1)) |> Assert.equal(_, Error(1)) +} -fn unwrap(result, default) = - case result +fn unwrap(result, default) { + case result { | Ok(v) => v | Error(_) => default + } +} -test unwrap = +test unwrap { unwrap(Ok(1), 50) |> Assert.equal(_, 1) unwrap(Error("nope"), 50) |> Assert.equal(_, 50) +} -fn to_maybe(result) = - case result +fn to_maybe(result) { + case result { | Ok(v) => Just(v) | Error(_) => Nothing + } +} -test to_maybe = +test to_maybe { to_maybe(Ok(1)) |> Assert.equal(_, Just(_, 1)) to_maybe(Error(1)) |> Assert.equal(_, Nothing) +} -fn from_maybe(maybe, error_reason) = - case maybe +fn from_maybe(maybe, error_reason) { + case maybe { | Just(v) => Ok(v) | Nothing => Error(error_reason) + } +} -test from_maybe = +test from_maybe { to_maybe(Just(1), :ok) |> Assert.equal(_, Ok(1)) to_maybe(Nothing, :ok) |> Assert.equal(_, Error(:ok)) +} |