-
-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return index of undecodable element in dynamic.list DecodeError path #628
base: main
Are you sure you want to change the base?
Conversation
src/gleam/list.gleam
Outdated
[x, ..xs] -> | ||
case fun(x) { | ||
Ok(y) -> do_try_map_with_index(xs, fun, [y, ..acc], i + 1) | ||
Error(error) -> Error(#(i, error)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is a tuple a reasonable data structure to use?
src/gleam/list.gleam
Outdated
/// try_map([[1], [], [2]], first) | ||
/// // -> Error(#(1, Nil)) | ||
/// ``` | ||
pub fn try_map_with_index( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catchy name? 😂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lovely! What a good idea.
Let's implement this without creating any new functions in the list module as we don't want to expand that API.
@lpil something like that? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic! Thank you
src/gleam/dynamic.gleam
Outdated
let result = try_map_with_index(list, decoder_type) | ||
case result { | ||
Ok(values) -> Ok(values) | ||
Error(#(index, errors)) -> | ||
Error(list.map(errors, push_path(_, int.to_string(index)))) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make the helper function specific
let result = try_map_with_index(list, decoder_type) | |
case result { | |
Ok(values) -> Ok(values) | |
Error(#(index, errors)) -> | |
Error(list.map(errors, push_path(_, int.to_string(index)))) | |
} | |
let result = decode_list(list, decoder_type, 0, []) | |
case result { | |
Ok(values) -> Ok(values) | |
Error(#(index, errors)) -> | |
Error(list.map(errors, push_path(_, index))) | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You want it to be typed to take a Decoder
rather than just an a -> b
function? We already have a function called decode_list
.
If you have an idea for a name that doesn't conflict with the existing decode_list
..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lpil do you think it's alright as it is now?
with fun: fn(a) -> Result(b, e), | ||
) -> Result(List(b), #(Int, e)) { | ||
do_try_map_with_index(list, fun, [], 0) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These generic functions are still here, please remove them
Closes #627
I'm new to Gleam, so fully prepared for what I'm doing here to be unidiomatic or otherwise distasteful for some reason, but thought it would be worth having a crack at it!