diff options
author | Louis Pilfold <louis@lpil.uk> | 2018-09-22 13:05:34 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2018-09-22 13:05:34 +0100 |
commit | 2d74cf4d9b1e56159650d427a265d3e96b3aff9e (patch) | |
tree | 9f90ed8aec32a1867c9af3310de0015af583288a | |
parent | d2bd45e76bf2138ce36757476e7ee42a5048a545 (diff) | |
download | gleam_stdlib-2d74cf4d9b1e56159650d427a265d3e96b3aff9e.tar.gz gleam_stdlib-2d74cf4d9b1e56159650d427a265d3e96b3aff9e.zip |
Remove `exposing` syntax
-rw-r--r-- | src/Bool.gleam | 86 | ||||
-rw-r--r-- | src/Foreign.gleam | 12 | ||||
-rw-r--r-- | src/List.gleam | 83 | ||||
-rw-r--r-- | src/Maybe.gleam | 83 | ||||
-rw-r--r-- | src/Order.gleam | 14 | ||||
-rw-r--r-- | src/Result.gleam | 47 |
6 files changed, 128 insertions, 197 deletions
diff --git a/src/Bool.gleam b/src/Bool.gleam index 6976042..ede88d4 100644 --- a/src/Bool.gleam +++ b/src/Bool.gleam @@ -1,14 +1,15 @@ -module Bool exposing Bool(..), not/1, compare/2, max/2, min/2 +module Bool -import Order exposing Order(_) +import Order:Order -type Bool = +pub type Bool = | True | False +; -; // Fix GitHub syntax highlighting +import Bool:* -fn not(bool) { +pub fn not(bool) { case bool { | True => False | False => True @@ -16,27 +17,37 @@ fn not(bool) { } test not { - not(True) |> Assert.false - not(False) |> Assert.true + not(True) + |> Assert.false + + not(False) + |> Assert.true } -fn compare(a, b) { +pub fn compare(a, b) { case (a, b) { - | (True, True) => EQ - | (True, False) => GT - | (False, False) => EQ - | (False, True) => GT + | (True, True) => Order:EQ + | (True, False) => Order:GT + | (False, False) => Order:EQ + | (False, True) => Order:GT } } 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) + compare(True, True) + |> Assert.equal(_, Order:EQ) + + compare(True, False) + |> Assert.equal(_, Order:GT) + + compare(False, False) + |> Assert.equal(_, Order:LT) + + compare(False, True) + |> Assert.equal(_, Order:GT) } -fn max(a, b) { +pub fn max(a, b) { case a { | True => True | False => b @@ -44,13 +55,20 @@ fn max(a, b) { } 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) + 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) { +pub fn min(a, b) { case a { | False => False | True => b @@ -58,13 +76,20 @@ fn min(a, b) { } 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) + 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) { +pub fn to_int(bool) { case bool { | False => 0 | True => 1 @@ -72,6 +97,9 @@ fn to_int(bool) { } test to_int { - to_int(True) |> Assert.equal(_, 1) - to_int(False) |> Assert.equal(_, 0) + to_int(True) + |> Assert.equal(_, 1) + + to_int(False) + |> Assert.equal(_, 0) } diff --git a/src/Foreign.gleam b/src/Foreign.gleam index 4c3cbe7..e2a40a2 100644 --- a/src/Foreign.gleam +++ b/src/Foreign.gleam @@ -1,18 +1,17 @@ -module Foreign exposing Foreign, new/1, unsafeCoerce/1, identity/1 +module Foreign doc """ Foreign data is data that we don't know the type of yet. We likely get data like this from interop with Erlang, or from IO with the outside world. """ -external type Foreign - -; // Fix GitHub syntax highlighting +pub external type Foreign +; doc """ Convert any Gleam data into Foreign data. """ -external new : fn(a) { Foreign } = :"Gleam.Foreign" :identity +pub external fn new(a) { Foreign } = :"Gleam.Foreign" :identity doc """ Unsafely cast any type into any other type.o @@ -20,9 +19,8 @@ Unsafely cast any type into any other type.o This is an escape hatch for the type system that may be useful when wrapping native Erlang APIs. It is to be used as a last measure only. """ -external unsafeCoerce : fn(a) { b } = :"Gleam.Foreign" :identity +pub external dangerouslyCoerce : fn(a) { b } = :"Gleam.Foreign" :identity -doc False fn identity(x) { x } diff --git a/src/List.gleam b/src/List.gleam index 5f49866..2388591 100644 --- a/src/List.gleam +++ b/src/List.gleam @@ -1,8 +1,9 @@ module List - exposing length/1, reverse/1, empty/1, member/2, head/1, tail/1, filter/2, - foldl/3, foldr/3, map/2, flatten/1, drop/2, take/2, of/1, new/0 -import Maybe exposing Maybe(..) +import Result:Result:* + +pub type Err = + | Empty // Using the Erlang C BIF implementation. // @@ -17,14 +18,14 @@ test length { // Using the Erlang C BIF implementation. // -external reverse : fn(List(a)) { List(a) } = :erlang :reverse +pub external fn reverse(List(a)) { List(a) } = :erlang :reverse test reverse { length([]) |> Assert.equal(_, []) length([1, 2, 3, 4, 5]) |> Assert.equal(_, [5, 4, 3, 2, 1]) } -fn is_empty(list) { +pub fn is_empty(list) { list == [] } @@ -33,7 +34,7 @@ test is_empty { is_empty([1]) |> Assert.false } -fn member(list, elem) { +pub fn member(list, elem) { case list { | [] => False | elem :: _ => True @@ -47,32 +48,40 @@ test is_member { is_member([], 1) |> Assert.false } -fn head(list) { +pub fn head(list) { case list { - | [] => Nothing - | elem :: _ => Just(elem) + | [] => Error(Err:Empty) + | elem :: _ => Ok(elem) } } test head { - head([0, 4, 5, 7]) |> Assert.equal(_, Just(0)) - head([]) |> Assert.equal(_, Nothing) + head([0, 4, 5, 7]) + |> Assert.equal(_, Ok(0)) + + head([]) + |> Assert.equal(_, Error(Err:Empty)) } -fn tail(list) { +pub fn tail(list) { case list { - | [] => Nothing - | _ :: rest => Just(rest) + | [] => Error(Err:Empty) + | _ :: rest => Ok(rest) } } test tail { - tail([0, 4, 5, 7]) |> Assert.equal(_, Just([4, 5, 7])) - tail([0]) |> Assert.equal(_, Just([])) - tail([]) |> Assert.equal(_, Nothing) + tail([0, 4, 5, 7]) + |> Assert.equal(_, Ok([4, 5, 7])) + + tail([0]) + |> Assert.equal(_, Ok([])) + + tail([]) + |> Assert.equal(_, Error(Err:Empty)) } -fn filter(list, fun) { +pub fn filter(list, fun) { filter(list, fun, []) } @@ -80,18 +89,21 @@ test filter { [] |> filter(_, fn(x) { True }) |> Assert.equal(_, []) + [0, 4, 5, 7, 3] |> filter(_, fn(x) { True }) |> Assert.equal(_, [0, 4, 5, 7, 3]) + [0, 4, 5, 7, 3] |> filter(_, fn(x) { x > 4 }) |> Assert.equal(_, [5, 7]) + [0, 4, 5, 7, 3] |> filter(_, fn(x) { x < 4 }) |> Assert.equal(_, [0, 3]) } -fn filter(list, fun, acc) { +fn do_filter(list, fun, acc) { case list { | [] => reverse(acc) | x :: xs => @@ -100,31 +112,32 @@ fn filter(list, fun, acc) { | True => x :: acc | False => acc } - filter(xs, fun, new_acc) + do_filter(xs, fun, new_acc) } } -fn map(list, fun) { - map(list, fun, []) +pub fn map(list, fun) { + do_map(list, fun, []) } test map { [] |> map(_, fn(x) { x * 2 }) |> Assert.equal(_, []) + [0, 4, 5, 7, 3] |> map(_, fn(x) { x * 2 }) |> Assert.equal(_, [0, 8, 10, 14, 6]) } -fn map(list, fun, acc) { +fn do_map(list, fun, acc) { case list { | [] => reverse(acc) - | x :: xs => map(xs, fun, fun(x) :: acc) + | x :: xs => do_map(xs, fun, fun(x) :: acc) } } -fn drop(list, n) { +pub fn drop(list, n) { case n <= 0 { | True => list | False => @@ -144,11 +157,11 @@ test drop/2 { |> Assert.equal(_, [6, 7, 8]) } -fn take(list, n) { - take(list, n, []) +pub fn take(list, n) { + do_take(list, n, []) } -fn take(list, n, acc) { +fn do_take(list, n, acc) { case n <= 0 { | True => reverse(acc) | False => @@ -168,7 +181,7 @@ test take { |> Assert.equal(_, [1, 2, 3, 4, 5]) } -fn of(x) { +pub fn of(x) { [x] } @@ -177,7 +190,7 @@ test of() { of(1) |> Assert.equal(_, [1]) } -fn new() { +pub fn new() { [] } @@ -185,8 +198,8 @@ test new() { new() |> Assert.equal(_, []) } -fn flatten(lists) { - flatten(lists, []) +pub fn flatten(lists) { + do_flatten(lists, []) } test flatten() { @@ -196,14 +209,14 @@ test flatten() { flatten([[1, 2], [], [3, 4]]) |> Assert.equal(_, [1, 2, 3, 4]) } -fn flatten(lists, acc) { +fn do_flatten(lists, acc) { case lists { | [] => acc | l :: rest => flatten(rest, acc ++ l) } } -fn foldl(list, acc, fun) { +pub fn foldl(list, acc, fun) { case list { | [] => acc | x :: rest => foldl(rest, fun(x, acc), fun) @@ -216,7 +229,7 @@ test foldl() { |> Assert.equal(_, [3, 2, 1]) } -fn foldr(list, acc, fun) { +pub fn foldr(list, acc, fun) { case list { | [] => acc | x :: rest => fun(x, foldl(rest, acc, fun)) diff --git a/src/Maybe.gleam b/src/Maybe.gleam deleted file mode 100644 index 8ae6e7c..0000000 --- a/src/Maybe.gleam +++ /dev/null @@ -1,83 +0,0 @@ -module Maybe - exposing Maybe(..), is_just/1, is_nothing/1, map/2, flatten/1, flat_map/2, - unwrap/2 - -type Maybe(x) = - | Just(x) - | Nothing - -; // Fix GitHub syntax highlighting - -fn is_just(maybe) { - case maybe { - | Just(_) => True - | Nothing => False - } -} - -test is_just() { - is_just(Just(1)) |> Assert.true - is_just(Nothing) |> Assert.false -} - -fn is_nothing(maybe) { - case maybe { - | Just(_) => False - | Nothing => True - } -} - -test is_nothing() { - is_nothing(Just(1)) |> Assert.false - is_nothing(Nothing) |> Assert.true -} - -fn map(maybe, fun) { - case maybe { - | Just(x) => fun(x) - | Nothing => Nothing - } -} - -test map() { - map(Just(1), fn(x) { x + 1 }) |> Assert.equal(_, Just(2)) - map(Nothing, fn(x) { x + 1 }) |> Assert.equal(Nothing) -} - -fn flatten(maybe) { - maybe - |> unwrap(_, Nothing) -} - -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) { - maybe - |> map(_, fun) - |> flatten -} - -test flat_map() { - flat_map(Nothing, fn(x) { Just(x + 1) }) - |> Assert.equal(_, Nothing) - flat_map(Just(1), fn(x) { Just(x + 1) }) - |> Assert.equal(_, Just(2)) - flat_map(Just(1), fn(_) { Nothing }) - |> Assert.equal(_, Nothing) -} - -fn unwrap(maybe, fallback) { - case maybe { - | Just(v) => v - | Nothing => fallback - } -} - -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 65e61f9..00f6750 100644 --- a/src/Order.gleam +++ b/src/Order.gleam @@ -1,13 +1,15 @@ -module Order exposing Order(..), reverse/1, to_int/1, compare/2, max/2, min/2 +module Order type Order = | LT | EQ | GT +import Order:* + ; // Fix GitHub syntax highlighting -fn reverse(order) { +pub fn reverse(order) { case order { | LT => GT | EQ => EQ @@ -21,7 +23,7 @@ test reverse { reverse(GT) |> Assert.equal(_, LT) } -fn to_int(order) { +pub fn to_int(order) { case order { | LT => -1 | EQ => 0 @@ -35,7 +37,7 @@ test to_int { to_int(GT) |> Assert.equal(_, 1) } -fn compare(a, b) { +pub fn compare(a, b) { case {a, b} { | {LT, LT} => EQ | {LT, _} => LT @@ -58,7 +60,7 @@ test compare { compare(GT, GT) |> Assert.equal(_, EQ) } -fn max(a, b) { +pub fn max(a, b) { case {a, b} { | {GT, _} => GT | {EQ, LT} => EQ @@ -78,7 +80,7 @@ test max { max(GT, GT) |> Assert.equal(_, GT) } -fn min(a, b) { +pub fn min(a, b) { case {a, b} { | {LT, _} => LT | {EQ, GT} => EQ diff --git a/src/Result.gleam b/src/Result.gleam index 3650e61..6b9c413 100644 --- a/src/Result.gleam +++ b/src/Result.gleam @@ -1,20 +1,17 @@ module Result - exposing Result(..), is_ok/1, is_none/1, map/2, map_error/2, flatten/1, - flat_map/2, unwrap/2, to_maybe/1, from_maybe/1 - -import Maybe exposing Maybe(..) 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) { +pub type Result(error, value) { | Ok(value) | Error(error) +; -; // Fix GitHub syntax highlighting +import Result:* -fn is_ok(result) { +pub fn is_ok(result) { case result { | Error(_) => False | Ok(_) => True @@ -26,7 +23,7 @@ test is_ok { is_ok(Error(1)) |> Assert.false } -fn is_error(result) { +pub fn is_error(result) { case result { | Ok(_) => False | Error(_) => True @@ -38,7 +35,7 @@ test is_error { is_error(Error(1)) |> Assert.true } -fn map(result, fun) { +pub fn map(result, fun) { case result { | Ok(x) => fun(x) | Error(_) => result @@ -54,7 +51,7 @@ test map { |> Assert.equal(Error(1)) } -fn map_error(result, fun) { +pub fn map_error(result, fun) { case result { | Ok(_) => result | Error(error) => Error(fun(error)) @@ -70,7 +67,7 @@ test map_error { |> Assert.equal(_, Error(2)) } -fn flatten(result) { +pub fn flatten(result) { case result { | Ok(x) => x | Error(_) => result @@ -86,7 +83,7 @@ test flatten { |> Assert.equal(_, Error(1)) } -fn flat_map(result, fun) { +pub fn flat_map(result, fun) { result |> unwrap(_, fun) |> flatten @@ -104,7 +101,7 @@ test flat_map { |> Assert.equal(_, Error(1)) } -fn unwrap(result, default) { +pub fn unwrap(result, default) { case result { | Ok(v) => v | Error(_) => default @@ -115,27 +112,3 @@ test unwrap { unwrap(Ok(1), 50) |> Assert.equal(_, 1) unwrap(Error("nope"), 50) |> Assert.equal(_, 50) } - -fn to_maybe(result) { - case result { - | Ok(v) => Just(v) - | Error(_) => Nothing - } -} - -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 { - | Just(v) => Ok(v) - | Nothing => Error(error_reason) - } -} - -test from_maybe { - to_maybe(Just(1), :ok) |> Assert.equal(_, Ok(1)) - to_maybe(Nothing, :ok) |> Assert.equal(_, Error(:ok)) -} |