aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrett Snyder <bsnyder@digitalocean.com>2019-10-02 10:49:43 -0500
committerLouis Pilfold <louis@lpil.uk>2019-10-03 11:38:43 +0100
commitcf7831f278423bab72f5d9433771e5a16ce44cac (patch)
tree1dbbb96cd4f040c85a343dc7244365dcf87f1c30
parent7677e9e96d38dffcf230891b7c74991aa7f16df5 (diff)
downloadgleam_stdlib-cf7831f278423bab72f5d9433771e5a16ce44cac.tar.gz
gleam_stdlib-cf7831f278423bab72f5d9433771e5a16ce44cac.zip
add pair:map_first and pair:map_second
-rw-r--r--CHANGELOG.md1
-rw-r--r--gen/src/gleam@pair.erl10
-rw-r--r--gen/test/gleam@pair_test.erl16
-rw-r--r--src/gleam/pair.gleam10
-rw-r--r--test/gleam/pair_test.gleam34
5 files changed, 69 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2ff57af..d31575e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
## Unreleased
+- The `pair` module gains the `map_first`, and `map_second` functions.
- The `string` module gains the `compare` function.
## v0.4.0 - 2019-09-19
diff --git a/gen/src/gleam@pair.erl b/gen/src/gleam@pair.erl
index 887adb9..3361dc7 100644
--- a/gen/src/gleam@pair.erl
+++ b/gen/src/gleam@pair.erl
@@ -1,7 +1,7 @@
-module(gleam@pair).
-compile(no_auto_import).
--export([first/1, second/1, swap/1]).
+-export([first/1, second/1, swap/1, map_first/2, map_second/2]).
first(Tup) ->
{A, _} = Tup,
@@ -14,3 +14,11 @@ second(Tup) ->
swap(Tup) ->
{A, B} = Tup,
{B, A}.
+
+map_first(Tup, F) ->
+ {A, B} = Tup,
+ {F(A), B}.
+
+map_second(Tup, F) ->
+ {A, B} = Tup,
+ {A, F(B)}.
diff --git a/gen/test/gleam@pair_test.erl b/gen/test/gleam@pair_test.erl
index 775f161..6a2c856 100644
--- a/gen/test/gleam@pair_test.erl
+++ b/gen/test/gleam@pair_test.erl
@@ -1,7 +1,7 @@
-module(gleam@pair_test).
-compile(no_auto_import).
--export([first_test/0, second_test/0, swap_test/0]).
+-export([first_test/0, second_test/0, swap_test/0, map_first_test/0, map_second_test/0]).
first_test() ->
gleam@expect:equal(gleam@pair:first({1, 2}), 1),
@@ -13,3 +13,17 @@ second_test() ->
swap_test() ->
gleam@expect:equal(gleam@pair:swap({1, <<"2">>}), {<<"2">>, 1}).
+
+map_first_test() ->
+ Inc = fun(A) -> A + 1 end,
+ gleam@expect:equal(gleam@pair:map_first({1, 2}, Inc), {2, 2}),
+ gleam@expect:equal(gleam@pair:map_first({8, 2}, Inc), {9, 2}),
+ gleam@expect:equal(gleam@pair:map_first({0, -2}, Inc), {1, -2}),
+ gleam@expect:equal(gleam@pair:map_first({-10, 20}, Inc), {-9, 20}).
+
+map_second_test() ->
+ Dec = fun(A) -> A - 1 end,
+ gleam@expect:equal(gleam@pair:map_second({1, 2}, Dec), {1, 1}),
+ gleam@expect:equal(gleam@pair:map_second({8, 2}, Dec), {8, 1}),
+ gleam@expect:equal(gleam@pair:map_second({0, -2}, Dec), {0, -3}),
+ gleam@expect:equal(gleam@pair:map_second({-10, 20}, Dec), {-10, 19}).
diff --git a/src/gleam/pair.gleam b/src/gleam/pair.gleam
index 9074330..32fc6d1 100644
--- a/src/gleam/pair.gleam
+++ b/src/gleam/pair.gleam
@@ -17,3 +17,13 @@ pub fn swap(tup) {
let Pair(a, b) = tup
Pair(b, a)
}
+
+pub fn map_first(tup, f) {
+ let Pair(a, b) = tup
+ Pair(f(a), b)
+}
+
+pub fn map_second(tup, f) {
+ let Pair(a, b) = tup
+ Pair(a, f(b))
+}
diff --git a/test/gleam/pair_test.gleam b/test/gleam/pair_test.gleam
index 54a12af..5dd2690 100644
--- a/test/gleam/pair_test.gleam
+++ b/test/gleam/pair_test.gleam
@@ -26,3 +26,37 @@ pub fn swap_test() {
|> pair.swap
|> expect.equal(_, pair.Pair("2", 1))
}
+
+pub fn map_first_test() {
+ let inc = fn(a) {
+ a + 1
+ }
+ pair.map_first(pair.Pair(1, 2), inc)
+ |> expect.equal(_, pair.Pair(2, 2))
+
+ pair.map_first(pair.Pair(8,2), inc)
+ |> expect.equal(_, pair.Pair(9, 2))
+
+ pair.map_first(pair.Pair(0,-2), inc)
+ |> expect.equal(_, pair.Pair(1, -2))
+
+ pair.map_first(pair.Pair(-10, 20), inc)
+ |> expect.equal(_, pair.Pair(-9, 20))
+}
+
+pub fn map_second_test() {
+ let dec = fn(a) {
+ a - 1
+ }
+ pair.map_second(pair.Pair(1, 2), dec)
+ |> expect.equal(_, pair.Pair(1, 1))
+
+ pair.map_second(pair.Pair(8,2), dec)
+ |> expect.equal(_, pair.Pair(8, 1))
+
+ pair.map_second(pair.Pair(0,-2), dec)
+ |> expect.equal(_, pair.Pair(0, -3))
+
+ pair.map_second(pair.Pair(-10, 20), dec)
+ |> expect.equal(_, pair.Pair(-10, 19))
+}