aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2020-05-21 23:11:36 +0100
committerLouis Pilfold <louis@lpil.uk>2020-05-21 23:11:36 +0100
commit1a8168fa8673393b927540e35a30cefa76b96508 (patch)
treec1d53264b1e69c175c9c8fa1621d78b5939e6d5c /src
parent97cd0c25d06a630721abffea3d54270094472476 (diff)
downloadgleam_stdlib-1a8168fa8673393b927540e35a30cefa76b96508.tar.gz
gleam_stdlib-1a8168fa8673393b927540e35a30cefa76b96508.zip
dynamic.opaque_list
Diffstat (limited to 'src')
-rw-r--r--src/gleam/dynamic.gleam23
1 files changed, 19 insertions, 4 deletions
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam
index 89826ec..3778a47 100644
--- a/src/gleam/dynamic.gleam
+++ b/src/gleam/dynamic.gleam
@@ -113,14 +113,29 @@ pub external fn bool(from: Dynamic) -> Result(Bool, String) =
pub external fn thunk(from: Dynamic) -> Result(fn() -> Dynamic, String) =
"gleam_stdlib" "decode_thunk"
-external fn list_dynamic(from: Dynamic) -> Result(List(Dynamic), String) =
- "gleam_stdlib" "decode_list"
-
/// Check to see whether a Dynamic value is a list, and return the list if it
/// is.
///
/// ## Examples
///
+/// > opaque_list(from(["a", "b", "c"]))
+/// Ok([from("a"), from("b"), from("c")])
+///
+/// > opaque_list(1)
+/// Error("Expected an Int, got a binary")
+///
+pub external fn opaque_list(from: Dynamic) -> Result(List(Dynamic), String) =
+ "gleam_stdlib" "decode_list"
+
+/// Check to see whether a Dynamic value is a list of a particular type, and
+/// return the list if it is.
+///
+/// The second argument is a decoder function used to decode the elements of
+/// the list. The list is only decoded if all elements in the list can be
+/// successfully decoded using this function.
+///
+/// ## Examples
+///
/// > list(from(["a", "b", "c"]), string)
/// Ok(["a", "b", "c"])
///
@@ -135,7 +150,7 @@ pub fn list(
containing decoder_type: fn(Dynamic) -> Result(inner, String),
) -> Result(List(inner), String) {
dynamic
- |> list_dynamic
+ |> opaque_list
|> result.then(list_mod.traverse(_, decoder_type))
}