-
Notifications
You must be signed in to change notification settings - Fork 19
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
Adds collect methods for arrays of results and maybes #46
Conversation
Co-authored-by: Jason Rametta <rametta@outlook.com>
I've not handled the case of empty array's as I'm not sure what you think's best. Currently
|
Hello @JackSpagnoli, this is interesting. I like the idea, but I don't think we should be modifying the Array prototype. Would you be able to update and make it a utility like the justs function? Thanks! |
if (failures.length > 0) { | ||
return Err(failures) | ||
} | ||
return Ok(successes) |
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 there an existing collect
function that you are modelling this logic off-of? I'm just curious how other libraries are using this
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.
Not really, I was trying pratica as part of another project and wrote this when verifying and translating an array to allow for code that looked like inputs.map(translate).collect()
.
I thought it might be a nice inclusion here, but I can't say I've done any research into wider implementations.
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.
I've done some digging, turns out I did something similar in Rust ages ago, which explains why I thought to call it collect. Only difference is Rust returns Err with the first found error, so Vec<Result<T, E>> -> Result<Vec<T>, E>
, rather than Vec<Result<T, E>> -> Result<Vec<T>, Vec<E>>
fn main() {
let bad_values: Vec<Result<u32, &str>> = vec![Ok(1), Err("error"), Ok(3)];
let bad_collection = bad_values.into_iter().collect::<Result<Vec<_>, _>>();
assert!(bad_collection.is_err());
assert_eq!(bad_collection.unwrap_err(), "error");
let good_values: Vec<Result<u32, &str>> = vec![Ok(1), Ok(2), Ok(3)];
let good_collection = good_values.into_iter().collect::<Result<Vec<_>, _>>();
assert!(good_collection.is_ok());
assert_eq!(good_collection.unwrap(), vec![1, 2, 3]);
}
Refactored to a utility function f4032cb |
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.
Looks good, thanks!
Adds collect methods for arrays of results and maybes
Adds function
collect
asArray<Result<O,E>> -> Result<Array<O>, Array<E>>
andArray<Maybe<O>> -> Maybe<Array<O>>
.For results, if any result in array is
Err
, returns an array of allErr
s, else returns an array of allOk
s.For maybes, if any result in array is
Nothing
, returnsNothing
, else returns an array of allJust
s.Updates to version 2.3.0.