Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

johtso
Copy link
Contributor

@johtso johtso commented Jun 9, 2024

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!

[x, ..xs] ->
case fun(x) {
Ok(y) -> do_try_map_with_index(xs, fun, [y, ..acc], i + 1)
Error(error) -> Error(#(i, error))
Copy link
Contributor Author

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?

/// try_map([[1], [], [2]], first)
/// // -> Error(#(1, Nil))
/// ```
pub fn try_map_with_index(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catchy name? 😂

Copy link
Member

@lpil lpil left a 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.

@johtso
Copy link
Contributor Author

johtso commented Jun 12, 2024

@lpil something like that?

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic! Thank you

Comment on lines 354 to 359
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))))
}
Copy link
Member

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

Suggested change
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)))
}

Copy link
Contributor Author

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..

Copy link
Contributor Author

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?

src/gleam/dynamic.gleam Outdated Show resolved Hide resolved
with fun: fn(a) -> Result(b, e),
) -> Result(List(b), #(Int, e)) {
do_try_map_with_index(list, fun, [], 0)
}
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Return index of undecodable element in dynamic.list DecodeError path
2 participants