aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2022-01-01 21:54:51 +0000
committerLouis Pilfold <louis@lpil.uk>2022-01-01 21:54:51 +0000
commitc036ec986bd0de33bb13c80748fe0296ee90a753 (patch)
tree701b09b480bc8a02f0b40b20bd6c2dcbf87f9e48
parentf127748fda4e8f1176d243c0e6c3f2bde964601d (diff)
downloadgleam_stdlib-c036ec986bd0de33bb13c80748fe0296ee90a753.tar.gz
gleam_stdlib-c036ec986bd0de33bb13c80748fe0296ee90a753.zip
Convert tuple functions
-rw-r--r--CHANGELOG.md1
-rw-r--r--src/gleam/dynamic.gleam167
-rw-r--r--test/gleam/dynamic_test.gleam193
3 files changed, 53 insertions, 308 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3d0e77a..9abf981 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@
- The `dynamic.typed_result` function has been renamed to `dynamic.result`.
- The `dynamic.any` is now available on JavaScript and has correct information
in any errors returned.
+- The `dynamic.typed_tuple*` functions have been renamed to `dynamic.tuple*`.
## v0.18.1 - 2021-12-19
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam
index 26e61ff..ff040db 100644
--- a/src/gleam/dynamic.gleam
+++ b/src/gleam/dynamic.gleam
@@ -542,114 +542,64 @@ fn put_expected(
}
}
-/// Checks to see if the `Dynamic` value is a 3-element tuple.
-///
-/// If you do not wish to decode all the elements in the tuple use the
-/// `typed_tuple3` function instead.
-///
-/// ## Examples
-///
-/// > tuple3(from(#(1, 2, 3)))
-/// Ok(#(from(1), from(2), from(3)))
-///
-/// > tuple3(from(#(1, 2)))
-/// Error(DecodeError(expected: "3 element tuple", found: "3 element tuple"))
-///
-/// > tuple3(from(""))
-/// Error(DecodeError(expected: "3 element tuple", found: "String"))
-///
-pub fn tuple3(
- from value: Dynamic,
-) -> Result(#(Dynamic, Dynamic, Dynamic), DecodeError) {
- try _ = assert_is_tuple(value, 3)
- Ok(unsafe_coerce(value))
-}
-
/// Checks to see if a `Dynamic` value is a 3-element tuple containing
/// specifically typed elements.
///
-/// If you wish to decode all the elements in the list use the `typed_tuple3`
-/// instead.
-///
/// ## Examples
///
-/// > typed_tuple3(from(#(1, 2, 3)), int, int, int)
+/// > tuple3(from(#(1, 2, 3)), int, int, int)
/// Ok(#(1, 2, 3))
///
-/// > typed_tuple3(from(#(1, 2.0, "3")), int, float, string)
+/// > tuple3(from(#(1, 2.0, "3")), int, float, string)
/// Ok(#(1, 2.0, "3"))
///
-/// > typed_tuple3(from(#(1, 2)), int, float, string)
+/// > tuple3(from(#(1, 2)), int, float, string)
/// Error(DecodeError(expected: "3 element tuple", found: "2 element tuple"))
///
-/// > typed_tuple3(from(""), int, float, string)
+/// > tuple3(from(""), int, float, string)
/// Error(DecodeError(expected: "3 element tuple", found: "String"))
///
-pub fn typed_tuple3(
- from tup: Dynamic,
+pub fn tuple3(
+ from value: Dynamic,
first decode_first: Decoder(a),
second decode_second: Decoder(b),
third decode_third: Decoder(c),
) -> Result(#(a, b, c), DecodeError) {
- try #(first, second, third) = tuple3(tup)
+ try _ = assert_is_tuple(value, 3)
+ let #(first, second, third) = unsafe_coerce(value)
try a = decode_first(first)
try b = decode_second(second)
try c = decode_third(third)
Ok(#(a, b, c))
}
-/// Checks to see if a `Dynamic` value is a 4-element tuple.
-///
-/// If you do not wish to decode all the elements in the tuple use the
-/// `typed_tuple4` function instead.
-///
-/// ## Examples
-///
-/// > tuple4(from(#(1, 2, 3, 4)))
-/// Ok(#(from(1), from(2), from(3), from(4)))
-///
-/// > tuple4(from(#(1, 2)))
-/// Error(DecodeError(expected: "4 element tuple", found: "2 element tuple"))
-///
-/// > tuple4(from(""))
-/// Error(DecodeError(expected: "4 element tuple", found: "String"))
-///
-pub fn tuple4(
- from value: Dynamic,
-) -> Result(#(Dynamic, Dynamic, Dynamic, Dynamic), DecodeError) {
- try _ = assert_is_tuple(value, 4)
- Ok(unsafe_coerce(value))
-}
-
/// Checks to see if a `Dynamic` value is a 4 element tuple containing
/// specifically typed elements.
///
-/// If you wish to decode all the elements in the list use the `typed_tuple4`
-/// instead.
-///
/// ## Examples
///
-/// > typed_tuple4(from(#(1, 2, 3, 4)), int, int, int, int)
+/// > tuple4(from(#(1, 2, 3, 4)), int, int, int, int)
/// Ok(#(1, 2, 3, 4))
///
-/// > typed_tuple4(from(#(1, 2.0, "3", 4)), int, float, string, int)
+/// > tuple4(from(#(1, 2.0, "3", 4)), int, float, string, int)
/// Ok(#(1, 2.0, "3", 4))
///
-/// > typed_tuple4(from(#(1, 2)), int, float, string, int)
+/// > tuple4(from(#(1, 2)), int, float, string, int)
/// Error("Expected a 4 element tuple, found a 2 element tuple")
/// Error(DecodeError(expected: "4 element tuple", found: "2 element tuple"))
///
-/// > typed_tuple4(from(""), int, float, string, int)
+/// > tuple4(from(""), int, float, string, int)
/// Error(DecodeError(expected: "4 element tuple", found: "String"))
///
-pub fn typed_tuple4(
- from tup: Dynamic,
+pub fn tuple4(
+ from value: Dynamic,
first decode_first: Decoder(a),
second decode_second: Decoder(b),
third decode_third: Decoder(c),
fourth decode_fourth: Decoder(d),
) -> Result(#(a, b, c, d), DecodeError) {
- try #(first, second, third, fourth) = tuple4(tup)
+ try _ = assert_is_tuple(value, 4)
+ let #(first, second, third, fourth) = unsafe_coerce(value)
try a = decode_first(first)
try b = decode_second(second)
try c = decode_third(third)
@@ -657,58 +607,33 @@ pub fn typed_tuple4(
Ok(#(a, b, c, d))
}
-/// Checks to see if a `Dynamic` value is a 5-element tuple.
-///
-/// If you do not wish to decode all the elements in the tuple use the
-/// `typed_tuple5` function instead.
-///
-/// ## Examples
-///
-/// > tuple5(from(#(1, 2, 3, 4, 5)))
-/// Ok(#(from(1), from(2), from(3), from(4), from(5)))
-///
-/// > tuple5(from(#(1, 2)))
-/// Error(DecodeError(expected: "5 element tuple", found: "2 element tuple"))
-///
-/// > tuple5(from(""))
-/// Error(DecodeError(expected: "5 element tuple", found: "String"))
-///
-pub fn tuple5(
- from value: Dynamic,
-) -> Result(#(Dynamic, Dynamic, Dynamic, Dynamic, Dynamic), DecodeError) {
- try _ = assert_is_tuple(value, 5)
- Ok(unsafe_coerce(value))
-}
-
/// Checks to see if a `Dynamic` value is a 5-element tuple containing
/// specifically typed elements.
///
-/// If you wish to decode all the elements in the list use the `typed_tuple5`
-/// instead.
-///
/// ## Examples
///
-/// > typed_tuple5(from(#(1, 2, 3, 4, 5)), int, int, int, int, int)
+/// > tuple5(from(#(1, 2, 3, 4, 5)), int, int, int, int, int)
/// Ok(#(1, 2, 3, 4, 5))
///
-/// > typed_tuple5(from(#(1, 2.0, "3", 4, 5)), int, float, string, int, int)
+/// > tuple5(from(#(1, 2.0, "3", 4, 5)), int, float, string, int, int)
/// Ok(#(1, 2.0, "3", 4, 5))
///
-/// > typed_tuple5(from(#(1, 2)), int, float, string, int, int)
+/// > tuple5(from(#(1, 2)), int, float, string, int, int)
/// Error(DecodeError(expected: "5 element tuple", found: "2 element tuple"))
///
-/// > typed_tuple5(from(""), int, float, string, int, int)
+/// > tuple5(from(""), int, float, string, int, int)
/// Error(DecodeError(expected: "5 element tuple", found: "String"))
///
-pub fn typed_tuple5(
- from tup: Dynamic,
+pub fn tuple5(
+ from value: Dynamic,
first decode_first: Decoder(a),
second decode_second: Decoder(b),
third decode_third: Decoder(c),
fourth decode_fourth: Decoder(d),
fifth decode_fifth: Decoder(e),
) -> Result(#(a, b, c, d, e), DecodeError) {
- try #(first, second, third, fourth, fifth) = tuple5(tup)
+ try _ = assert_is_tuple(value, 5)
+ let #(first, second, third, fourth, fifth) = unsafe_coerce(value)
try a = decode_first(first)
try b = decode_second(second)
try c = decode_third(third)
@@ -717,54 +642,25 @@ pub fn typed_tuple5(
Ok(#(a, b, c, d, e))
}
-/// Checks to see if a `Dynamic` value is a 6-element tuple.
-///
-/// If you do not wish to decode all the elements in the tuple use the
-/// `typed_tuple6` function instead.
-///
-/// ## Examples
-///
-/// > tuple6(from(#(1, 2, 3, 4, 5, 6)))
-/// Ok(#(from(1), from(2), from(3), from(4), from(5), from(6)))
-///
-/// > tuple6(from(#(1, 2)))
-/// Error(DecodeError(expected: "6 element tuple", found: "2 element tuple"))
-///
-/// > tuple6(from(""))
-/// Error(DecodeError(expected: "6 element tuple", found: "String"))
-///
-pub fn tuple6(
- from value: Dynamic,
-) -> Result(
- #(Dynamic, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic),
- DecodeError,
-) {
- try _ = assert_is_tuple(value, 6)
- Ok(unsafe_coerce(value))
-}
-
/// Checks to see if a `Dynamic` value is a 6-element tuple containing
/// specifically typed elements.
///
-/// If you wish to decode all the elements in the list use the `typed_tuple6`
-/// instead.
-///
/// ## Examples
///
-/// > typed_tuple6(from(#(1, 2, 3, 4, 5, 6)), int, int, int, int, int, int)
+/// > tuple6(from(#(1, 2, 3, 4, 5, 6)), int, int, int, int, int, int)
/// Ok(#(1, 2, 3, 4, 5, 6))
///
-/// > typed_tuple6(from(#(1, 2.0, "3", 4, 5, 6)), int, float, string, int, int)
+/// > tuple6(from(#(1, 2.0, "3", 4, 5, 6)), int, float, string, int, int)
/// Ok(#(1, 2.0, "3", 4, 5, 6))
///
-/// > typed_tuple6(from(#(1, 2)), int, float, string, int, int, int)
+/// > tuple6(from(#(1, 2)), int, float, string, int, int, int)
/// Error(DecodeError(expected: "6 element tuple", found: "2 element tuple"))
///
-/// > typed_tuple6(from(""), int, float, string, int, int, int)
+/// > tuple6(from(""), int, float, string, int, int, int)
/// Error(DecodeError(expected: "6 element tuple", found: "String"))
///
-pub fn typed_tuple6(
- from tup: Dynamic,
+pub fn tuple6(
+ from value: Dynamic,
first decode_first: Decoder(a),
second decode_second: Decoder(b),
third decode_third: Decoder(c),
@@ -772,7 +668,8 @@ pub fn typed_tuple6(
fifth decode_fifth: Decoder(e),
sixth decode_sixth: Decoder(f),
) -> Result(#(a, b, c, d, e, f), DecodeError) {
- try #(first, second, third, fourth, fifth, sixth) = tuple6(tup)
+ try _ = assert_is_tuple(value, 6)
+ let #(first, second, third, fourth, fifth, sixth) = unsafe_coerce(value)
try a = decode_first(first)
try b = decode_second(second)
try c = decode_third(third)
diff --git a/test/gleam/dynamic_test.gleam b/test/gleam/dynamic_test.gleam
index 3a1504c..ea40190 100644
--- a/test/gleam/dynamic_test.gleam
+++ b/test/gleam/dynamic_test.gleam
@@ -359,50 +359,22 @@ pub fn tuple2_test() {
pub fn tuple3_test() {
#(1, 2, 3)
|> dynamic.from
- |> dynamic.tuple3
- |> should.equal(Ok(#(dynamic.from(1), dynamic.from(2), dynamic.from(3))))
-
- #(1, "", 3.0)
- |> dynamic.from
- |> dynamic.tuple3
- |> should.equal(Ok(#(dynamic.from(1), dynamic.from(""), dynamic.from(3.0))))
-
- #(1, 2)
- |> dynamic.from
- |> dynamic.tuple3
- |> should.equal(Error(DecodeError(
- expected: "Tuple of 3 elements",
- found: "Tuple of 2 elements",
- )))
-
- 1
- |> dynamic.from
- |> dynamic.tuple3
- |> should.equal(Error(DecodeError(
- expected: "Tuple of 3 elements",
- found: "Int",
- )))
-}
-
-pub fn typed_tuple3_test() {
- #(1, 2, 3)
- |> dynamic.from
- |> dynamic.typed_tuple3(dynamic.int, dynamic.int, dynamic.int)
+ |> dynamic.tuple3(dynamic.int, dynamic.int, dynamic.int)
|> should.equal(Ok(#(1, 2, 3)))
#(1, "", 3.0)
|> dynamic.from
- |> dynamic.typed_tuple3(dynamic.int, dynamic.string, dynamic.float)
+ |> dynamic.tuple3(dynamic.int, dynamic.string, dynamic.float)
|> should.equal(Ok(#(1, "", 3.0)))
#(1, 2, "")
|> dynamic.from
- |> dynamic.typed_tuple3(dynamic.int, dynamic.int, dynamic.int)
+ |> dynamic.tuple3(dynamic.int, dynamic.int, dynamic.int)
|> should.equal(Error(DecodeError(expected: "Int", found: "String")))
#(1, 2)
|> dynamic.from
- |> dynamic.typed_tuple3(dynamic.int, dynamic.int, dynamic.int)
+ |> dynamic.tuple3(dynamic.int, dynamic.int, dynamic.int)
|> should.equal(Error(DecodeError(
expected: "Tuple of 3 elements",
found: "Tuple of 2 elements",
@@ -410,7 +382,7 @@ pub fn typed_tuple3_test() {
1
|> dynamic.from
- |> dynamic.typed_tuple3(dynamic.int, dynamic.int, dynamic.int)
+ |> dynamic.tuple3(dynamic.int, dynamic.int, dynamic.int)
|> should.equal(Error(DecodeError(
expected: "Tuple of 3 elements",
found: "Int",
@@ -420,65 +392,22 @@ pub fn typed_tuple3_test() {
pub fn tuple4_test() {
#(1, 2, 3, 4)
|> dynamic.from
- |> dynamic.tuple4
- |> should.equal(Ok(#(
- dynamic.from(1),
- dynamic.from(2),
- dynamic.from(3),
- dynamic.from(4),
- )))
-
- #(1, "", 3.0, 4)
- |> dynamic.from
- |> dynamic.tuple4
- |> should.equal(Ok(#(
- dynamic.from(1),
- dynamic.from(""),
- dynamic.from(3.0),
- dynamic.from(4),
- )))
-
- #(1, 2)
- |> dynamic.from
- |> dynamic.tuple4
- |> should.equal(Error(DecodeError(
- expected: "Tuple of 4 elements",
- found: "Tuple of 2 elements",
- )))
-
- 1
- |> dynamic.from
- |> dynamic.tuple4
- |> should.equal(Error(DecodeError(
- expected: "Tuple of 4 elements",
- found: "Int",
- )))
-}
-
-pub fn typed_tuple4_test() {
- #(1, 2, 3, 4)
- |> dynamic.from
- |> dynamic.typed_tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int)
+ |> dynamic.tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int)
|> should.equal(Ok(#(1, 2, 3, 4)))
#(1, "", 3.0, 4)
|> dynamic.from
- |> dynamic.typed_tuple4(
- dynamic.int,
- dynamic.string,
- dynamic.float,
- dynamic.int,
- )
+ |> dynamic.tuple4(dynamic.int, dynamic.string, dynamic.float, dynamic.int)
|> should.equal(Ok(#(1, "", 3.0, 4)))
#(1, 2, 3, "")
|> dynamic.from
- |> dynamic.typed_tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int)
+ |> dynamic.tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int)
|> should.equal(Error(DecodeError(expected: "Int", found: "String")))
#(1, 2)
|> dynamic.from
- |> dynamic.typed_tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int)
+ |> dynamic.tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int)
|> should.equal(Error(DecodeError(
expected: "Tuple of 4 elements",
found: "Tuple of 2 elements",
@@ -486,7 +415,7 @@ pub fn typed_tuple4_test() {
1
|> dynamic.from
- |> dynamic.typed_tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int)
+ |> dynamic.tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int)
|> should.equal(Error(DecodeError(
expected: "Tuple of 4 elements",
found: "Int",
@@ -496,47 +425,7 @@ pub fn typed_tuple4_test() {
pub fn tuple5_test() {
#(1, 2, 3, 4, 5)
|> dynamic.from
- |> dynamic.tuple5
- |> should.equal(Ok(#(
- dynamic.from(1),
- dynamic.from(2),
- dynamic.from(3),
- dynamic.from(4),
- dynamic.from(5),
- )))
-
- #(1, "", 3.0, 4, 5)
- |> dynamic.from
- |> dynamic.tuple5
- |> should.equal(Ok(#(
- dynamic.from(1),
- dynamic.from(""),
- dynamic.from(3.0),
- dynamic.from(4),
- dynamic.from(5),
- )))
-
- #(1, 2)
- |> dynamic.from
- |> dynamic.tuple5
- |> should.equal(Error(DecodeError(
- expected: "Tuple of 5 elements",
- found: "Tuple of 2 elements",
- )))
-
- 1
- |> dynamic.from
- |> dynamic.tuple5
- |> should.equal(Error(DecodeError(
- expected: "Tuple of 5 elements",
- found: "Int",
- )))
-}
-
-pub fn typed_tuple5_test() {
- #(1, 2, 3, 4, 5)
- |> dynamic.from
- |> dynamic.typed_tuple5(
+ |> dynamic.tuple5(
dynamic.int,
dynamic.int,
dynamic.int,
@@ -547,7 +436,7 @@ pub fn typed_tuple5_test() {
#(1, "", 3.0, 4, 5)
|> dynamic.from
- |> dynamic.typed_tuple5(
+ |> dynamic.tuple5(
dynamic.int,
dynamic.string,
dynamic.float,
@@ -558,7 +447,7 @@ pub fn typed_tuple5_test() {
#(1, 2, 3, 4, "")
|> dynamic.from
- |> dynamic.typed_tuple5(
+ |> dynamic.tuple5(
dynamic.int,
dynamic.int,
dynamic.int,
@@ -569,7 +458,7 @@ pub fn typed_tuple5_test() {
#(1, 2)
|> dynamic.from
- |> dynamic.typed_tuple5(
+ |> dynamic.tuple5(
dynamic.int,
dynamic.int,
dynamic.int,
@@ -583,7 +472,7 @@ pub fn typed_tuple5_test() {
1
|> dynamic.from
- |> dynamic.typed_tuple5(
+ |> dynamic.tuple5(
dynamic.int,
dynamic.int,
dynamic.int,
@@ -599,49 +488,7 @@ pub fn typed_tuple5_test() {
pub fn tuple6_test() {
#(1, 2, 3, 4, 5, 6)
|> dynamic.from
- |> dynamic.tuple6
- |> should.equal(Ok(#(
- dynamic.from(1),
- dynamic.from(2),
- dynamic.from(3),
- dynamic.from(4),
- dynamic.from(5),
- dynamic.from(6),
- )))
-
- #(1, "", 3.0, 4, 5, 6)
- |> dynamic.from
- |> dynamic.tuple6
- |> should.equal(Ok(#(
- dynamic.from(1),
- dynamic.from(""),
- dynamic.from(3.0),
- dynamic.from(4),
- dynamic.from(5),
- dynamic.from(6),
- )))
-
- #(1, 2)
- |> dynamic.from
- |> dynamic.tuple6
- |> should.equal(Error(DecodeError(
- expected: "Tuple of 6 elements",
- found: "Tuple of 2 elements",
- )))
-
- 1
- |> dynamic.from
- |> dynamic.tuple6
- |> should.equal(Error(DecodeError(
- expected: "Tuple of 6 elements",
- found: "Int",
- )))
-}
-
-pub fn typed_tuple6_test() {
- #(1, 2, 3, 4, 5, 6)
- |> dynamic.from
- |> dynamic.typed_tuple6(
+ |> dynamic.tuple6(
dynamic.int,
dynamic.int,
dynamic.int,
@@ -653,7 +500,7 @@ pub fn typed_tuple6_test() {
#(1, "", 3.0, 4, 5, 6)
|> dynamic.from
- |> dynamic.typed_tuple6(
+ |> dynamic.tuple6(
dynamic.int,
dynamic.string,
dynamic.float,
@@ -665,7 +512,7 @@ pub fn typed_tuple6_test() {
#(1, 2, 3, 4, 5, "")
|> dynamic.from
- |> dynamic.typed_tuple6(
+ |> dynamic.tuple6(
dynamic.int,
dynamic.int,
dynamic.int,
@@ -677,7 +524,7 @@ pub fn typed_tuple6_test() {
#(1, 2)
|> dynamic.from
- |> dynamic.typed_tuple6(
+ |> dynamic.tuple6(
dynamic.int,
dynamic.int,
dynamic.int,
@@ -692,7 +539,7 @@ pub fn typed_tuple6_test() {
1
|> dynamic.from
- |> dynamic.typed_tuple6(
+ |> dynamic.tuple6(
dynamic.int,
dynamic.int,
dynamic.int,