diff options
author | Louis Pilfold <louis@lpil.uk> | 2019-03-23 10:58:52 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2019-03-23 10:58:52 +0000 |
commit | 27fec2149c765de657d45382af91c361eb3c953c (patch) | |
tree | 2736c4e1320c7f6b144071b2a11e10df598982ce /src/tuple.gleam | |
parent | 6af3f431609b2c530ae3f8598ac16815e26748d2 (diff) | |
download | gleam_stdlib-27fec2149c765de657d45382af91c361eb3c953c.tar.gz gleam_stdlib-27fec2149c765de657d45382af91c361eb3c953c.zip |
stdlib additions
Diffstat (limited to 'src/tuple.gleam')
-rw-r--r-- | src/tuple.gleam | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/tuple.gleam b/src/tuple.gleam index d437a9f..5da1810 100644 --- a/src/tuple.gleam +++ b/src/tuple.gleam @@ -1,4 +1,18 @@ import expect +import result +import list + +pub fn new(a, b) { + {a, b} +} + +test new { + new(1, 2) + |> expect:equal(_, {1, 2}) + + new(2, "3") + |> expect:equal(_, {2, "3"}) +} pub fn first(tup) { let {a, _} = tup @@ -21,3 +35,39 @@ test second { |> second |> expect:equal(_, 2) } + +pub fn swap(tup) { + let {a, b} = tup + {b, a} +} + +test swap { + {1, "2"} + |> swap + |> expect:equal(_, {"2", 1}) +} + +pub fn fetch(haystack, needle) { + list:find(haystack, fn(tuple) { + case first(tuple) == needle { + | True -> Ok(second(tuple)) + | False -> Error([]) + } + }) +} + +test fetch { + let proplist = [{0, "1"}, {1, "2"}] + + proplist + |> fetch(_, 0) + |> expect:equal(_, Ok("1")) + + proplist + |> fetch(_, 1) + |> expect:equal(_, Ok("2")) + + proplist + |> fetch(_, 2) + |> expect:is_error +} |