diff options
author | Louis Pilfold <louis@lpil.uk> | 2022-01-09 18:48:54 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-01-09 18:48:54 +0000 |
commit | b68260179e2e17930dfb0604fc2b26355fc79173 (patch) | |
tree | 90a716e8c66645df691fb48de29f20d8a6e8dba5 /src | |
parent | ec3d3abbdf94e7bc998fbf90919627779386c30e (diff) | |
download | gleam_stdlib-b68260179e2e17930dfb0604fc2b26355fc79173.tar.gz gleam_stdlib-b68260179e2e17930dfb0604fc2b26355fc79173.zip |
Curry result
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/dynamic.gleam | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam index 1f56172..44e6852 100644 --- a/src/gleam/dynamic.gleam +++ b/src/gleam/dynamic.gleam @@ -61,14 +61,14 @@ if javascript { "../gleam_stdlib.mjs" "identity" } -/// Converts a `Dynamic` value into a `Dynamic` value. +/// Decodes a `Dynamic` value from a `Dynamic` value. /// /// This function doesn't seem very useful at first, but it can be convenient /// when you need to give a decoder function but you don't actually care what /// the to-decode value is. /// -pub fn dynamic(term: Dynamic) -> Dynamic { - unsafe_coerce(term) +pub fn dynamic(term: Dynamic) -> Decoder(Dynamic) { + Ok } /// Checks to see whether a `Dynamic` value is a bit_string, and returns that bit string if @@ -292,32 +292,40 @@ if javascript { /// ## Examples /// /// ```gleam -/// > result(of: from(Ok(1)), ok: int, error: string) +/// > from(Ok(1)) +/// > |> result(ok: int, error: string) /// Ok(Ok(1)) /// -/// > result(of: from(Error("boom")), ok: int, error: string) +/// > from(Error("boom")) +/// > |> result(ok: int, error: string) /// Ok(Error("boom")) /// -/// > result(of: from(123), ok: int, error: string) +/// > from(123) +/// > |> result(ok: int, error: string) /// Error([DecodeError(expected: "2 element tuple", found: "Int", path: [])]) /// ``` /// pub fn result( - of dynamic: Dynamic, ok decode_ok: Decoder(a), error decode_error: Decoder(e), -) -> Result(Result(a, e), DecodeErrors) { - try inner_result = decode_result(dynamic) - - case inner_result { - Ok(raw) -> - raw - |> decode_ok - |> result.map(Ok) - Error(raw) -> - raw - |> decode_error - |> result.map(Error) +) -> Decoder(Result(a, e)) { + fn(value) { + try inner_result = decode_result(value) + + case inner_result { + Ok(raw) -> { + try value = + decode_ok(raw) + |> map_errors(push_path(_, "ok")) + Ok(Ok(value)) + } + Error(raw) -> { + try value = + decode_error(raw) + |> map_errors(push_path(_, "error")) + Ok(Error(value)) + } + } } } |