diff options
author | Alice Dee <alice.dee@guardian.co.uk> | 2019-09-17 12:37:06 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2019-09-17 12:59:32 +0100 |
commit | 198beba763f8bc0fd35b5b6ba36e42c5926ecc8c (patch) | |
tree | 5c2262db87b91b8a804f7c0fc97fb365022bcb9b | |
parent | 9d282d68b5a2764ab73c2dead1df0b0ab8b8edaa (diff) | |
download | gleam_stdlib-198beba763f8bc0fd35b5b6ba36e42c5926ecc8c.tar.gz gleam_stdlib-198beba763f8bc0fd35b5b6ba36e42c5926ecc8c.zip |
Create Pair datatype (remove Tuple)
-rw-r--r-- | gen/src/gleam@any.erl | 6 | ||||
-rw-r--r-- | gen/src/gleam@pair.erl | 16 | ||||
-rw-r--r-- | gen/test/gleam@any_test.erl | 24 | ||||
-rw-r--r-- | gen/test/gleam@pair_test.erl | 13 | ||||
-rw-r--r-- | src/gleam/any.gleam | 6 | ||||
-rw-r--r-- | src/gleam/bool.gleam | 12 | ||||
-rw-r--r-- | src/gleam/list.gleam | 26 | ||||
-rw-r--r-- | src/gleam/map.gleam | 8 | ||||
-rw-r--r-- | src/gleam/order.gleam | 27 | ||||
-rw-r--r-- | src/gleam/pair.gleam | 30 | ||||
-rw-r--r-- | src/gleam/tuple.gleam | 31 | ||||
-rw-r--r-- | src/gleam__stdlib.erl | 6 | ||||
-rw-r--r-- | test/gleam/any_test.gleam | 45 | ||||
-rw-r--r-- | test/gleam/list_test.gleam | 36 | ||||
-rw-r--r-- | test/gleam/map_test.gleam | 136 | ||||
-rw-r--r-- | test/gleam/pair_test.gleam | 36 | ||||
-rw-r--r-- | test/gleam/result_test.gleam | 6 | ||||
-rw-r--r-- | test/gleam/tuple_test.gleam | 46 |
18 files changed, 258 insertions, 252 deletions
diff --git a/gen/src/gleam@any.erl b/gen/src/gleam@any.erl index 5bd9157..4b2d17d 100644 --- a/gen/src/gleam@any.erl +++ b/gen/src/gleam@any.erl @@ -1,7 +1,7 @@ -module(gleam@any). -compile(no_auto_import). --export([from/1, unsafe_coerce/1, string/1, int/1, float/1, atom/1, bool/1, thunk/1, list/2, struct2/1, field/2]). +-export([from/1, unsafe_coerce/1, string/1, int/1, float/1, atom/1, bool/1, thunk/1, list/2, pair/1, field/2]). from(A) -> gleam__stdlib:identity(A). @@ -36,8 +36,8 @@ list(Any, Decode) -> fun(Capture1) -> gleam@list:traverse(Capture1, Decode) end ). -struct2(A) -> - gleam__stdlib:decode_struct2(A). +pair(A) -> + gleam__stdlib:decode_pair(A). field(A, B) -> gleam__stdlib:decode_field(A, B). diff --git a/gen/src/gleam@pair.erl b/gen/src/gleam@pair.erl new file mode 100644 index 0000000..887adb9 --- /dev/null +++ b/gen/src/gleam@pair.erl @@ -0,0 +1,16 @@ +-module(gleam@pair). +-compile(no_auto_import). + +-export([first/1, second/1, swap/1]). + +first(Tup) -> + {A, _} = Tup, + A. + +second(Tup) -> + {_, A} = Tup, + A. + +swap(Tup) -> + {A, B} = Tup, + {B, A}. diff --git a/gen/test/gleam@any_test.erl b/gen/test/gleam@any_test.erl index e915fe7..59a5067 100644 --- a/gen/test/gleam@any_test.erl +++ b/gen/test/gleam@any_test.erl @@ -1,7 +1,7 @@ -module(gleam@any_test). -compile(no_auto_import). --export([string_test/0, int_test/0, float_test/0, thunk_test/0, bool_test/0, atom_test/0, list_test/0, struct2_test/0, field_test/0]). +-export([string_test/0, int_test/0, float_test/0, thunk_test/0, bool_test/0, atom_test/0, list_test/0, pair_test/0, field_test/0]). string_test() -> gleam@expect:equal(gleam@any:string(gleam@any:from(<<"">>)), {ok, <<"">>}), @@ -117,32 +117,30 @@ list_test() -> ) ). -struct2_test() -> +pair_test() -> gleam@expect:equal( - gleam@any:struct2(gleam@any:from({1, []})), + gleam@any:pair(gleam@any:from({1, []})), {ok, {gleam@any:from(1), gleam@any:from([])}} ), gleam@expect:equal( - gleam@any:struct2(gleam@any:from({<<"ok">>, <<"ok">>})), + gleam@any:pair(gleam@any:from({<<"ok">>, <<"ok">>})), {ok, {gleam@any:from(<<"ok">>), gleam@any:from(<<"ok">>)}} ), - gleam@expect:is_error(gleam@any:struct2(gleam@any:from({1}))), - gleam@expect:is_error(gleam@any:struct2(gleam@any:from({1, 2, 3}))), gleam@expect:equal( gleam@result:then( gleam@result:then( - gleam@any:struct2(gleam@any:from({1, 2.0})), + gleam@any:pair(gleam@any:from({1, 2.0})), fun(X) -> gleam@result:map( - gleam@any:int(gleam@tuple:first(X)), - fun(F) -> {F, gleam@tuple:second(X)} end + gleam@any:int(gleam@pair:first(X)), + fun(F) -> {F, gleam@pair:second(X)} end ) end ), fun(X) -> gleam@result:map( - gleam@any:float(gleam@tuple:second(X)), - fun(F) -> {gleam@tuple:first(X), F} end + gleam@any:float(gleam@pair:second(X)), + fun(F) -> {gleam@pair:first(X), F} end ) end ), @@ -151,7 +149,7 @@ struct2_test() -> field_test() -> {ok, OkAtom} = gleam@atom:from_string(<<"ok">>), - {ok, EarlierAtom} = gleam@atom:from_string(<<"earlier">>), + {ok, ErrorAtom} = gleam@atom:from_string(<<"error">>), gleam@expect:equal( gleam@any:field( gleam@any:from(gleam@map:put(gleam@map:new(), OkAtom, 1)), @@ -164,7 +162,7 @@ field_test() -> gleam@any:from( gleam@map:put( gleam@map:put(gleam@map:new(), OkAtom, 3), - EarlierAtom, + ErrorAtom, 1 ) ), diff --git a/gen/test/gleam@pair_test.erl b/gen/test/gleam@pair_test.erl new file mode 100644 index 0000000..63a329a --- /dev/null +++ b/gen/test/gleam@pair_test.erl @@ -0,0 +1,13 @@ +-module(gleam@pair_test). +-compile(no_auto_import). + +-export([first_test/0, second_test/0, swap_test/0]). + +first_test() -> + gleam@expect:equal(gleam@pair:first({1, 2}), 1). + +second_test() -> + gleam@expect:equal(gleam@pair:second({1, 2}), 2). + +swap_test() -> + gleam@expect:equal(gleam@pair:swap({1, <<"2">>}), {<<"2">>, 1}). diff --git a/src/gleam/any.gleam b/src/gleam/any.gleam index 611d4fd..902fb5c 100644 --- a/src/gleam/any.gleam +++ b/src/gleam/any.gleam @@ -1,6 +1,7 @@ import gleam/list as list_mod import gleam/atom import gleam/result +import gleam/pair // `Any` data is data that we don"t know the type of yet. // We likely get data like this from interop with Erlang, or from @@ -46,9 +47,8 @@ pub fn list(any, decode) { |> result.then(_, list_mod.traverse(_, decode)) } -// TODO: replace struct with Pair -pub external fn struct2(Any) -> Result(struct(Any, Any), String) - = "gleam__stdlib" "decode_struct2" +pub external fn pair(Any) -> Result(pair.Pair(Any, Any), String) + = "gleam__stdlib" "decode_pair" pub external fn field(Any, a) -> Result(Any, String) = "gleam__stdlib" "decode_field" diff --git a/src/gleam/bool.gleam b/src/gleam/bool.gleam index 81e0cea..ff26a9b 100644 --- a/src/gleam/bool.gleam +++ b/src/gleam/bool.gleam @@ -1,4 +1,5 @@ import gleam/order +import gleam/pair pub fn negate(bool) { case bool { @@ -7,13 +8,12 @@ pub fn negate(bool) { } } -// TODO: replace struct with Pair pub fn compare(a, b) { - case struct(a, b) { - | struct(True, True) -> order.Eq - | struct(True, False) -> order.Gt - | struct(False, False) -> order.Eq - | struct(False, True) -> order.Lt + case pair.Pair(a, b) { + | pair.Pair(True, True) -> order.Eq + | pair.Pair(True, False) -> order.Gt + | pair.Pair(False, False) -> order.Eq + | pair.Pair(False, True) -> order.Lt } } diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index 195e1b5..e0e7016 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -1,5 +1,6 @@ import gleam/int import gleam/order +import gleam/pair pub enum Empty = | Empty @@ -187,12 +188,11 @@ pub fn any(list, f) { } } -// TODO: replace struct with Pair pub fn zip(l1, l2) { - case struct(l1, l2) { - | struct([], _) -> [] - | struct(_, []) -> [] - | struct([x1 | rest1], [x2 | rest2]) -> [ struct(x1, x2) | zip(rest1, rest2) ] + case pair.Pair(l1, l2) { + | pair.Pair([], _) -> [] + | pair.Pair(_, []) -> [] + | pair.Pair([x1 | rest1], [x2 | rest2]) -> [ pair.Pair(x1, x2) | zip(rest1, rest2) ] } } @@ -234,10 +234,10 @@ pub fn unique(list) { } fn merge_sort(a, b, compare) { - case struct(a, b) { - | struct([], _) -> b - | struct(_, []) -> a - | struct([ax | ar], [bx | br]) -> + case pair.Pair(a, b) { + | pair.Pair([], _) -> b + | pair.Pair(_, []) -> a + | pair.Pair([ax | ar], [bx | br]) -> case compare(ax, bx) { | order.Lt -> [ax | merge_sort(ar, b, compare)] | _ -> [bx | merge_sort(a, br, compare)] @@ -285,10 +285,10 @@ pub fn repeat(a, times) { fn do_split(list, n, taken) { case n <= 0 { - | True -> struct(reverse(taken), list) + | True -> pair.Pair(reverse(taken), list) | False -> case list { - | [] -> struct(reverse(taken), []) + | [] -> pair.Pair(reverse(taken), []) | [x | xs] -> do_split(xs, n - 1, [x | taken]) } } @@ -300,10 +300,10 @@ pub fn split(list, n) { fn do_split_while(list, f, acc) { case list { - | [] -> struct(reverse(acc), []) + | [] -> pair.Pair(reverse(acc), []) | [x | xs] -> case f(x) { - | False -> struct(reverse(acc), list) + | False -> pair.Pair(reverse(acc), list) | _ -> do_split_while(xs, f, [x | acc]) } } diff --git a/src/gleam/map.gleam b/src/gleam/map.gleam index ba03b78..935f3cb 100644 --- a/src/gleam/map.gleam +++ b/src/gleam/map.gleam @@ -1,6 +1,7 @@ import gleam/any import gleam/result import gleam/list +import gleam/pair pub external type MapDict(key, value); @@ -10,10 +11,10 @@ pub enum NotFound = pub external fn size(MapDict(k, v)) -> Int = "maps" "size" -pub external fn to_list(MapDict(key, value)) -> List(struct(key, value)) +pub external fn to_list(MapDict(key, value)) -> List(pair.Pair(key, value)) = "maps" "to_list" -pub external fn from_list(List(struct(key, value))) -> MapDict(key, value) +pub external fn from_list(List(pair.Pair(key, value))) -> MapDict(key, value) = "maps" "from_list" external fn is_key(key, MapDict(key, v)) -> Bool @@ -88,10 +89,9 @@ pub fn update(dict, key, f) { } fn do_fold(list, acc, f) { - // TODO: replace struct with Pair case list { | [] -> acc - | [struct(k, v) | tail] -> do_fold(tail, f(k, v, acc), f) + | [pair.Pair(k, v) | tail] -> do_fold(tail, f(k, v, acc), f) } } diff --git a/src/gleam/order.gleam b/src/gleam/order.gleam index 5913479..6886991 100644 --- a/src/gleam/order.gleam +++ b/src/gleam/order.gleam @@ -1,3 +1,5 @@ +import gleam/pair + pub enum Order = | Lt | Eq @@ -20,30 +22,29 @@ pub fn to_int(order) { } } -// TODO: replace struct with Pair pub fn compare(a, b) { - case struct(a, b) { - | struct(Lt, Lt) -> Eq - | struct(Lt, _) -> Lt - | struct(Eq, Eq) -> Eq - | struct(Gt, Gt) -> Eq - | struct(Eq, Gt) -> Lt + case pair.Pair(a, b) { + | pair.Pair(Lt, Lt) -> Eq + | pair.Pair(Lt, _) -> Lt + | pair.Pair(Eq, Eq) -> Eq + | pair.Pair(Gt, Gt) -> Eq + | pair.Pair(Eq, Gt) -> Lt | _ -> Gt } } pub fn max(a, b) { - case struct(a, b) { - | struct(Gt, _) -> Gt - | struct(Eq, Lt) -> Eq + case pair.Pair(a, b) { + | pair.Pair(Gt, _) -> Gt + | pair.Pair(Eq, Lt) -> Eq | _ -> b } } pub fn min(a, b) { - case struct(a, b) { - | struct(Lt, _) -> Lt - | struct(Eq, Gt) -> Eq + case pair.Pair(a, b) { + | pair.Pair(Lt, _) -> Lt + | pair.Pair(Eq, Gt) -> Eq | _ -> b } } diff --git a/src/gleam/pair.gleam b/src/gleam/pair.gleam new file mode 100644 index 0000000..7df33ca --- /dev/null +++ b/src/gleam/pair.gleam @@ -0,0 +1,30 @@ +// import gleam/list + +pub struct Pair(a, b) { + first: a + second: b +} + +pub fn first(tup) { + let Pair(a, _) = tup + a +} + +pub fn second(tup) { + let Pair(_, a) = tup + a +} + +pub fn swap(tup) { + let Pair(a, b) = tup + Pair(b, a) +} + +// pub fn fetch(haystack, needle) { +// list.find(haystack, fn(tuple) { +// case first(tuple) == needle { +// | True -> tuple |> second |> Ok +// | False -> Error([]) +// } +// }) +// } diff --git a/src/gleam/tuple.gleam b/src/gleam/tuple.gleam deleted file mode 100644 index 88c2a6a..0000000 --- a/src/gleam/tuple.gleam +++ /dev/null @@ -1,31 +0,0 @@ -import gleam/list - -// TODO: replace struct with Pair - -pub fn new(a, b) { - struct(a, b) -} - -pub fn first(tup) { - let struct(a, _) = tup - a -} - -pub fn second(tup) { - let struct(_, a) = tup - a -} - -pub fn swap(tup) { - let struct(a, b) = tup - struct(b, a) -} - -pub fn fetch(haystack, needle) { - list.find(haystack, fn(tuple) { - case first(tuple) == needle { - | True -> tuple |> second |> Ok - | False -> Error([]) - } - }) -} diff --git a/src/gleam__stdlib.erl b/src/gleam__stdlib.erl index 8434082..55e847d 100644 --- a/src/gleam__stdlib.erl +++ b/src/gleam__stdlib.erl @@ -6,7 +6,7 @@ atom_create_from_string/1, atom_to_string/1, map_fetch/2, iodata_append/2, iodata_prepend/2, identity/1, decode_int/1, decode_string/1, decode_bool/1, decode_float/1, decode_thunk/1, decode_atom/1, - decode_struct2/1, decode_list/1, decode_field/2, parse_int/1, parse_float/1]). + decode_pair/1, decode_list/1, decode_field/2, parse_int/1, parse_float/1]). expect_equal(Actual, Expected) -> ?assertEqual(Expected, Actual). expect_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual). @@ -58,8 +58,8 @@ decode_bool(Data) -> decode_error_msg("a Bool", Data). decode_thunk(Data) when is_function(Data, 0) -> {ok, Data}; decode_thunk(Data) -> decode_error_msg("a zero arity function", Data). -decode_struct2(Data = {_, _}) -> {ok, Data}; -decode_struct2(Data) -> decode_error_msg("a 2 element struct", Data). +decode_pair(Data = {_, _}) -> {ok, Data}; +decode_pair(Data) -> decode_error_msg("a 2 element tuple", Data). decode_list(Data) when is_list(Data) -> {ok, Data}; decode_list(Data) -> decode_error_msg("a List", Data). diff --git a/test/gleam/any_test.gleam b/test/gleam/any_test.gleam index fc9640d..5fe0476 100644 --- a/test/gleam/any_test.gleam +++ b/test/gleam/any_test.gleam @@ -1,7 +1,7 @@ import gleam/any import gleam/atom import gleam/list -import gleam/tuple +import gleam/pair import gleam/expect import gleam/result import gleam/map @@ -210,49 +210,38 @@ pub fn list_test() { |> expect.is_error } -// TODO: replace struct with Pair -pub fn struct2_test() { - struct(1, []) +pub fn pair_test() { + pair.Pair(1, []) |> any.from - |> any.struct2 - |> expect.equal(_, Ok(struct(any.from(1), any.from([])))) + |> any.pair + |> expect.equal(_, Ok(pair.Pair(any.from(1), any.from([])))) - struct("ok", "ok") + pair.Pair("ok", "ok") |> any.from - |> any.struct2 - |> expect.equal(_, Ok(struct(any.from("ok"), any.from("ok")))) + |> any.pair + |> expect.equal(_, Ok(pair.Pair(any.from("ok"), any.from("ok")))) - struct(1) + pair.Pair(1, 2.0) |> any.from - |> any.struct2 - |> expect.is_error - - struct(1, 2, 3) - |> any.from - |> any.struct2 - |> expect.is_error - - struct(1, 2.0) - |> any.from - |> any.struct2 + |> any.pair |> result.then(_, fn(x) { x - |> tuple.first + |> pair.first |> any.int - |> result.map(_, fn(f) { struct(f, tuple.second(x)) }) + |> result.map(_, fn(f) { pair.Pair(f, pair.second(x)) }) }) |> result.then(_, fn(x) { x - |> tuple.second + |> pair.second |> any.float - |> result.map(_, fn(f) { struct(tuple.first(x), f) }) + |> result.map(_, fn(f) { pair.Pair(pair.first(x), f) }) }) - |> expect.equal(_, Ok(struct(1, 2.0))) + |> expect.equal(_, Ok(pair.Pair(1, 2.0))) } pub fn field_test() { let Ok(ok_atom) = atom.from_string("ok") - let Ok(earlier_atom) = atom.from_string("earlier") + let Ok(error_atom) = atom.from_string("error") map.new() |> map.put(_, ok_atom, 1) @@ -262,7 +251,7 @@ pub fn field_test() { map.new() |> map.put(_, ok_atom, 3) - |> map.put(_, earlier_atom, 1) + |> map.put(_, error_atom, 1) |> any.from |> any.field(_, ok_atom) |> expect.equal(_, Ok(any.from(3))) diff --git a/test/gleam/list_test.gleam b/test/gleam/list_test.gleam index 1270ff3..e211947 100644 --- a/test/gleam/list_test.gleam +++ b/test/gleam/list_test.gleam @@ -3,6 +3,7 @@ import gleam/list import gleam/int import gleam/float import gleam/string +import gleam/pair pub fn length_test() { list.length([]) |> expect.equal(_, 0) @@ -195,15 +196,14 @@ pub fn zip_test() { list.zip([1, 2], []) |> expect.equal(_, []) - // TODO: replace struct with Pair list.zip([1, 2, 3], [4, 5, 6]) - |> expect.equal(_, [struct(1, 4), struct(2, 5), struct(3, 6)]) + |> expect.equal(_, [pair.Pair(1, 4), pair.Pair(2, 5), pair.Pair(3, 6)]) list.zip([5, 6], [1, 2, 3]) - |> expect.equal(_, [struct(5, 1), struct(6, 2)]) + |> expect.equal(_, [pair.Pair(5, 1), pair.Pair(6, 2)]) list.zip([5, 6, 7], [1, 2]) - |> expect.equal(_, [struct(5, 1), struct(6, 2)]) + |> expect.equal(_, [pair.Pair(5, 1), pair.Pair(6, 2)]) } pub fn strict_zip_test() { @@ -214,7 +214,7 @@ pub fn strict_zip_test() { |> expect.is_error list.strict_zip([1, 2, 3], [4, 5, 6]) - |> expect.equal(_, Ok([struct(1, 4), struct(2, 5), struct(3, 6)])) + |> expect.equal(_, Ok([pair.Pair(1, 4), pair.Pair(2, 5), pair.Pair(3, 6)])) list.strict_zip([5, 6], [1, 2, 3]) |> expect.is_error @@ -278,8 +278,8 @@ pub fn sort_test() { } pub fn index_map_test() { - list.index_map([3, 4, 5], fn(i, x) { struct(i, x) }) - |> expect.equal(_, [struct(0, 3), struct(1, 4), struct(2, 5)]) + list.index_map([3, 4, 5], fn(i, x) { pair.Pair(i, x) }) + |> expect.equal(_, [pair.Pair(0, 3), pair.Pair(1, 4), pair.Pair(2, 5)]) let f = fn(i, x) { string.append(x, int.to_string(i)) @@ -324,37 +324,37 @@ pub fn repeat_test() { pub fn split_test() { list.split([], 0) - |> expect.equal(_, struct([], [])) + |> expect.equal(_, pair.Pair([], [])) list.split([0, 1, 2, 3, 4], 0) - |> expect.equal(_, struct([], [0, 1, 2, 3, 4])) + |> expect.equal(_, pair.Pair([], [0, 1, 2, 3, 4])) list.split([0, 1, 2, 3, 4], -2) - |> expect.equal(_, struct([], [0, 1, 2, 3, 4])) + |> expect.equal(_, pair.Pair([], [0, 1, 2, 3, 4])) list.split([0, 1, 2, 3, 4], 1) - |> expect.equal(_, struct([0], [1, 2, 3, 4])) + |> expect.equal(_, pair.Pair([0], [1, 2, 3, 4])) list.split([0, 1, 2, 3, 4], 3) - |> expect.equal(_, struct([0, 1, 2], [3, 4])) + |> expect.equal(_, pair.Pair([0, 1, 2], [3, 4])) list.split([0, 1, 2, 3, 4], 9) - |> expect.equal(_, struct([0, 1, 2, 3, 4], [])) + |> expect.equal(_, pair.Pair([0, 1, 2, 3, 4], [])) } pub fn split_while_test() { list.split_while([], fn(x) { x <= 5 }) - |> expect.equal(_, struct([], [])) + |> expect.equal(_, pair.Pair([], [])) list.split_while([1, 2, 3, 4, 5], fn(x) { x <= 5 }) - |> expect.equal(_, struct([1, 2, 3, 4, 5], [])) + |> expect.equal(_, pair.Pair([1, 2, 3, 4, 5], [])) list.split_while([1, 2, 3, 4, 5], fn(x) { x == 2 }) - |> expect.equal(_, struct([], [1, 2, 3, 4, 5])) + |> expect.equal(_, pair.Pair([], [1, 2, 3, 4, 5])) list.split_while([1, 2, 3, 4, 5], fn(x) { x <= 3 }) - |> expect.equal(_, struct([1, 2, 3], [4, 5])) + |> expect.equal(_, pair.Pair([1, 2, 3], [4, 5])) list.split_while([1, 2, 3, 4, 5], fn(x) { x <= -3 }) - |> expect.equal(_, struct([], [1, 2, 3, 4, 5])) + |> expect.equal(_, pair.Pair([], [1, 2, 3, 4, 5])) } diff --git a/test/gleam/map_test.gleam b/test/gleam/map_test.gleam index bef0e76..96d648f 100644 --- a/test/gleam/map_test.gleam +++ b/test/gleam/map_test.gleam @@ -1,12 +1,12 @@ import gleam/string import gleam/expect import gleam/map +import gleam/pair -// TODO: replace struct with Pair pub fn from_list_test() { [ - struct(4, 0), - struct(1, 0), + pair.Pair(4, 0), + pair.Pair(1, 0), ] |> map.from_list |> map.size @@ -20,23 +20,23 @@ pub fn has_key_test() { |> expect.false [ - struct(1, 0), + pair.Pair(1, 0), ] |> map.from_list |> map.has_key(_, 1) |> expect.true [ - struct(4, 0), - struct(1, 0), + pair.Pair(4, 0), + pair.Pair(1, 0), ] |> map.from_list |> map.has_key(_, 1) |> expect.true [ - struct(4, 0), - struct(1, 0), + pair.Pair(4, 0), + pair.Pair(1, 0), ] |> map.from_list |> map.has_key(_, 0) @@ -55,8 +55,8 @@ pub fn new_test() { pub fn fetch_test() { let proplist = [ - struct(4, 0), - struct(1, 1), + pair.Pair(4, 0), + pair.Pair(1, 1), ] let m = map.from_list(proplist) @@ -79,32 +79,32 @@ pub fn put_test() { |> map.put(_, "b", 1) |> map.put(_, "c", 2) |> expect.equal(_, map.from_list([ - struct("a", 0), - struct("b", 1), - struct("c", 2), + pair.Pair("a", 0), + pair.Pair("b", 1), + pair.Pair("c", 2), ])) } pub fn map_values_test() { [ - struct(1, 0), - struct(2, 1), - struct(3, 2), + pair.Pair(1, 0), + pair.Pair(2, 1), + pair.Pair(3, 2), ] |> map.from_list |> map.map_values(_, fn(k, v) { k + v }) |> expect.equal(_, map.from_list([ - struct(1, 1), - struct(2, 3), - struct(3, 5), + pair.Pair(1, 1), + pair.Pair(2, 3), + pair.Pair(3, 5), ])) } pub fn keys_test() { [ - struct("a", 0), - struct("b", 1), - struct("c", 2), + pair.Pair("a", 0), + pair.Pair("b", 1), + pair.Pair("c", 2), ] |> map.from_list |> map.keys @@ -113,9 +113,9 @@ pub fn keys_test() { pub fn values_test() { [ - struct("a", 0), - struct("b", 1), - struct("c", 2), + pair.Pair("a", 0), + pair.Pair("b", 1), + pair.Pair("c", 2), ] |> map.from_list |> map.values @@ -124,72 +124,72 @@ pub fn values_test() { pub fn take_test() { [ - struct("a", 0), - struct("b", 1), - struct("c", 2), + pair.Pair("a", 0), + pair.Pair("b", 1), + pair.Pair("c", 2), ] |> map.from_list |> map.take(_, ["a", "b", "d"]) - |> expect.equal(_, map.from_list([struct("a", 0), struct("b", 1)])) + |> expect.equal(_, map.from_list([pair.Pair("a", 0), pair.Pair("b", 1)])) } pub fn drop_test() { [ - struct("a", 0), - struct("b", 1), - struct("c", 2), + pair.Pair("a", 0), + pair.Pair("b", 1), + pair.Pair("c", 2), ] |> map.from_list |> map.drop(_, ["a", "b", "d"]) - |> expect.equal(_, map.from_list([struct("c", 2)])) + |> expect.equal(_, map.from_list([pair.Pair("c", 2)])) } pub fn merge_test() { let a = map.from_list([ - struct("a", 2), - struct("c", 4), - struct("d", 3), + pair.Pair("a", 2), + pair.Pair("c", 4), + pair.Pair("d", 3), ]) let b = map.from_list([ - struct("a", 0), - struct("b", 1), - struct("c", 2), + pair.Pair("a", 0), + pair.Pair("b", 1), + pair.Pair("c", 2), ]) map.merge(a, b) |> expect.equal(_, map.from_list([ - struct("a", 0), - struct("b", 1), - struct("c", 2), - struct("d", 3), + pair.Pair("a", 0), + pair.Pair("b", 1), + pair.Pair("c", 2), + pair.Pair("d", 3), ])) map.merge(b, a) |> expect.equal(_, map.from_list([ - struct("a", 2), - struct("b", 1), - struct("c", 4), - struct("d", 3), + pair.Pair("a", 2), + pair.Pair("b", 1), + pair.Pair("c", 4), + pair.Pair("d", 3), ])) } pub fn delete_test() { [ - struct("a", 0), - struct("b", 1), - struct("c", 2), + pair.Pair("a", 0), + pair.Pair("b", 1), + pair.Pair("c", 2), ] |> map.from_list |> map.delete(_, "a") |> map.delete(_, "d") - |> expect.equal(_, map.from_list([struct("b", 1), struct("c", 2)])) + |> expect.equal(_, map.from_list([pair.Pair("b", 1), pair.Pair("c", 2)])) } pub fn update_test() { let dict = map.from_list([ - struct("a", 0), - struct("b", 1), - struct("c", 2), + pair.Pair("a", 0), + pair.Pair("b", 1), + pair.Pair("c", 2), ]) let inc_or_zero = fn(x) { @@ -202,35 +202,35 @@ pub fn update_test() { dict |> map.update(_, "a", inc_or_zero) |> expect.equal(_, map.from_list([ - struct("a", 1), - struct("b", 1), - struct("c", 2), + pair.Pair("a", 1), + pair.Pair("b", 1), + pair.Pair("c", 2), ])) dict |> map.update(_, "b", inc_or_zero) |> expect.equal(_, map.from_list([ - struct("a", 0), - struct("b", 2), - struct("c", 2), + pair.Pair("a", 0), + pair.Pair("b", 2), + pair.Pair("c", 2), ])) dict |> map.update(_, "z", inc_or_zero) |> expect.equal(_, map.from_list([ - struct("a", 0), - struct("b", 1), - struct("c", 2), - struct("z", 0), + pair.Pair("a", 0), + pair.Pair("b", 1), + pair.Pair("c", 2), + pair.Pair("z", 0), ])) } pub fn fold_test() { let dict = map.from_list([ - struct("a", 0), - struct("b", 1), - struct("c", 2), - struct("d", 3), + pair.Pair("a", 0), + pair.Pair("b", 1), + pair.Pair("c", 2), + pair.Pair("d", 3), ]) let add = fn(_, v, acc) { diff --git a/test/gleam/pair_test.gleam b/test/gleam/pair_test.gleam new file mode 100644 index 0000000..86a9e1e --- /dev/null +++ b/test/gleam/pair_test.gleam @@ -0,0 +1,36 @@ +import gleam/expect +import gleam/pair + +pub fn first_test() { + pair.Pair(1, 2) + |> pair.first + |> expect.equal(_, 1) +} + +pub fn second_test() { + pair.Pair(1, 2) + |> pair.second + |> expect.equal(_, 2) +} + +pub fn swap_test() { + pair.Pair(1, "2") + |> pair.swap + |> expect.equal(_, pair.Pair("2", 1)) +} + +// pub fn fetch_test() { +// let proplist = [pair.Pair(0, "1"), pair.Pair(1, "2")] + +// proplist +// |> pair.fetch(_, 0) +// |> expect.equal(_, Ok("1")) + +// proplist +// |> pair.fetch(_, 1) +// |> expect.equal(_, Ok("2")) + +// proplist +// |> pair.fetch(_, 2) +// |> expect.is_error +// } diff --git a/test/gleam/result_test.gleam b/test/gleam/result_test.gleam index 9113e6c..644a5fc 100644 --- a/test/gleam/result_test.gleam +++ b/test/gleam/result_test.gleam @@ -1,5 +1,6 @@ import gleam/expect import gleam/result +import gleam/pair pub fn is_ok_test() { result.is_ok(Ok(1)) @@ -36,10 +37,9 @@ pub fn map_error_test() { |> result.map_error(_, fn(x) { x + 1 }) |> expect.equal(_, Ok(1)) - // TODO: replace struct with Pair Error(1) - |> result.map_error(_, fn(x) { struct("ok", x + 1) }) - |> expect.equal(_, Error(struct("ok", 2))) + |> result.map_error(_, fn(x) { pair.Pair("ok", x + 1) }) + |> expect.equal(_, Error(pair.Pair("ok", 2))) } pub fn flatten_test() { diff --git a/test/gleam/tuple_test.gleam b/test/gleam/tuple_test.gleam deleted file mode 100644 index a1693b0..0000000 --- a/test/gleam/tuple_test.gleam +++ /dev/null @@ -1,46 +0,0 @@ -import gleam/expect -import gleam/tuple - -// TODO: replace struct with Pair - -pub fn new_test() { - tuple.new(1, 2) - |> expect.equal(_, struct(1, 2)) - - tuple.new(2, "3") - |> expect.equal(_, struct(2, "3")) -} - -pub fn first_test() { - struct(1, 2) - |> tuple.first - |> expect.equal(_, 1) -} - -pub fn second_test() { - struct(1, 2) - |> tuple.second - |> expect.equal(_, 2) -} - -pub fn swap_test() { - struct(1, "2") - |> tuple.swap - |> expect.equal(_, struct("2", 1)) -} - -pub fn fetch_test() { - let proplist = [struct(0, "1"), struct(1, "2")] - - proplist - |> tuple.fetch(_, 0) - |> expect.equal(_, Ok("1")) - - proplist - |> tuple.fetch(_, 1) - |> expect.equal(_, Ok("2")) - - proplist - |> tuple.fetch(_, 2) - |> expect.is_error -} |