-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fold map function for List #8894
Conversation
A few comments, not including an opinion on whether this should go in:
|
I took inspiration from the above mentioned implementation, the name is inspired by the recently added |
The failing CI build is due to missing changes, I wanted to wait until the name is fixed before adding them. |
testsuite/tests/lib-list/test.ml
Outdated
@@ -42,6 +42,7 @@ let () = | |||
assert (List.compare_length_with ['1'] 1 = 0); | |||
assert (List.compare_length_with ['1'] 2 < 0); | |||
assert (List.filter_map string_of_even_opt l = ["0";"2";"4";"6";"8"]); | |||
assert (List.fold_map (fun a b -> a + b, b) 0 l = (45, l)); |
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.
nit, but this test case doesn't check that the map
part of fold_map
does anything.
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 added some more tests.
Err, sorry to be back with more annoying bits :)
|
|
Given that we have |
Woops, my bad. Indeed, but it's a bit slower as you noted. :) |
I think if we can agree on the name and arguments of the function, we can actually do even a bit more and add the function to all modules that have a |
I rebased the branch. I still think this is a useful and often used function and would like to have it included in the standard library. |
Thanks for the contribution. I agree this is a useful addition. I have a preference for Does anyone prefer to keep
Indeed, one can even generically transform a let map_accu map f acc l =
let acc = ref acc in
let g x =
let new_acc, y = f !acc x in
acc := new_acc;
y
in
let r = map g l in
!acc, r and this shows that providing the function is "just" a convenience for common cases. But let's keep that for later. I find it much less natural for data structures that implements a |
+1 to naming it |
I'm okay with |
stdlib/listLabels.mli
Outdated
f:('a -> 'b -> 'a * 'c) -> init:'a -> 'b list -> 'a * 'c list | ||
(** [fold_left_map] is a combination of [fold_left] and [map] hat threads an | ||
accumulator through calls to [f] | ||
@since 4.10.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.
@since 4.10.0 | |
@since 4.11.0 |
stdlib/list.mli
Outdated
val fold_left_map : ('a -> 'b -> 'a * 'c) -> 'a -> 'b list -> 'a * 'c list | ||
(** [fold_left_map] is a combination of [fold_left] and [map] that threads an | ||
accumulator through calls to [f] | ||
@since 4.10.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.
@since 4.10.0 | |
@since 4.11.0 |
Changes
Outdated
@@ -56,6 +56,11 @@ OCaml 4.10.0 | |||
(Guillaume Munch-Maccagnoni, review by Jacques-Henri Jourdan, | |||
Stephen Dolan, and Gabriel Scherer) | |||
|
|||
### Standard library: | |||
|
|||
- #8894: Added List.fold_left_map function combining map and fold. |
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.
To be moved to current working version (=> 4.11).
LGTM. Could you move the change entry to 4.11 and update the since tags accordingly? Then it should be good to merge if nobody complains. |
The fold_map function is quite common combination of fold and map which allow it to pass an accumulator through map.
I cleaned up the history, changed the target version and add @cfcs and @alainfrisch as reviewer. |
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.
LGTM, will merge soon if nobody complains.
@bschommer : thanks for your contribution (in general, no need to put efforts rewriting the history since we tend to squash-merge small PRs anyway).
No problem. |
The fold_map function is quite common combination of fold and map which allow it to pass an accumulator through map.
As the title indicates this PR implements the quite common and popular function that combines
fold
andmap
. I don't have any strong opinion on the order of arguments and or the name.