diff options
author | Louis Pilfold <louis@lpil.uk> | 2022-01-09 17:06:04 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-01-09 17:06:04 +0000 |
commit | 3b3ee6b405ac3b74aeba06be74ea36645cf0240f (patch) | |
tree | 7603eaab8e4908649a006921ea1bcb211fcc312c /src | |
parent | 1b087519b8e7fa8ae553e542455bde601aaf6ec8 (diff) | |
download | gleam_stdlib-3b3ee6b405ac3b74aeba06be74ea36645cf0240f.tar.gz gleam_stdlib-3b3ee6b405ac3b74aeba06be74ea36645cf0240f.zip |
Curry any
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/dynamic.gleam | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam index 9281d8b..be48206 100644 --- a/src/gleam/dynamic.gleam +++ b/src/gleam/dynamic.gleam @@ -569,7 +569,7 @@ fn put_expected(error: DecodeError, expected: String) -> DecodeError { fn push_path(error: DecodeError, name: t) -> DecodeError { let name = from(name) - let decoder = any(_, [string, fn(x) { result.map(int(x), int.to_string) }]) + let decoder = any([string, fn(x) { result.map(int(x), int.to_string) }]) let name = case decoder(name) { Ok(name) -> name Error(_) -> @@ -847,7 +847,7 @@ if javascript { /// /// ```gleam /// > import gleam/result -/// > let bool_or_string = any(_, of: [ +/// > let bool_or_string = any(of: [ /// > string, /// > fn(x) { result.map(bool(x), fn(_) { "a bool" }) } /// > ]) @@ -861,21 +861,20 @@ if javascript { /// Error(DecodeError(expected: "unknown", found: "unknown", path: [])) /// ``` /// -pub fn any( - from data: Dynamic, - of decoders: List(Decoder(t)), -) -> Result(t, DecodeErrors) { - case decoders { - [] -> - Error([ - DecodeError(found: classify(data), expected: "another type", path: []), - ]) - - [decoder, ..decoders] -> - case decoder(data) { - Ok(decoded) -> Ok(decoded) - Error(_) -> any(data, decoders) - } +pub fn any(of decoders: List(Decoder(t))) -> Decoder(t) { + fn(data) { + case decoders { + [] -> + Error([ + DecodeError(found: classify(data), expected: "another type", path: []), + ]) + + [decoder, ..decoders] -> + case decoder(data) { + Ok(decoded) -> Ok(decoded) + Error(_) -> any(decoders)(data) + } + } } } |