aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gleam/dynamic.gleam3
-rw-r--r--src/gleam_stdlib.erl14
2 files changed, 16 insertions, 1 deletions
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam
index 8086aeb..0660a67 100644
--- a/src/gleam/dynamic.gleam
+++ b/src/gleam/dynamic.gleam
@@ -48,3 +48,6 @@ pub fn list(from dynamic, containing decoder_type) {
pub external fn field(from: Dynamic, named: a) -> Result(Dynamic, String)
= "gleam_stdlib" "decode_field"
+
+pub external fn element(from: Dynamic, position: Int) -> Result(Dynamic, String)
+ = "gleam_stdlib" "decode_element"
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl
index 8a4a290..abfdfbb 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_get/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_list/1, decode_field/2, parse_int/1, parse_float/1, compare_strings/2]).
+ decode_list/1, decode_field/2, decode_element/2, parse_int/1, parse_float/1, compare_strings/2]).
expect_equal(Actual, Expected) -> ?assertEqual(Expected, Actual).
expect_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual).
@@ -70,6 +70,18 @@ decode_field(Data, Key) ->
decode_error_msg(io_lib:format("a map with key `~p`", [Key]), Data)
end.
+decode_element(Data, Position) ->
+ case catch element(Position + 1, Data) of
+ {'EXIT', _Reason} ->
+ {error, nil};
+
+ Value ->
+ {ok, Value};
+
+ _ ->
+ {error, nil}
+ end.
+
parse_int(String) ->
case string:to_integer(binary:bin_to_list(String)) of
{Integer, []} ->