From 198beba763f8bc0fd35b5b6ba36e42c5926ecc8c Mon Sep 17 00:00:00 2001 From: Alice Dee Date: Tue, 17 Sep 2019 12:37:06 +0100 Subject: Create Pair datatype (remove Tuple) --- src/gleam/any.gleam | 6 +++--- src/gleam/bool.gleam | 12 ++++++------ src/gleam/list.gleam | 26 +++++++++++++------------- src/gleam/map.gleam | 8 ++++---- src/gleam/order.gleam | 27 ++++++++++++++------------- src/gleam/pair.gleam | 30 ++++++++++++++++++++++++++++++ src/gleam/tuple.gleam | 31 ------------------------------- src/gleam__stdlib.erl | 6 +++--- 8 files changed, 73 insertions(+), 73 deletions(-) create mode 100644 src/gleam/pair.gleam delete mode 100644 src/gleam/tuple.gleam (limited to 'src') 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). -- cgit v1.2.3