Essentially the code would be the same as separated_list0, except for the Vec::new() we call init(), and instead of Vec::push() we call g(). With these code changes, separated_list0 could be written in terms of fold_separated_list0. Just pass the appropriate Vec methods for init and g.
fold_separated_list1 would be based on separated_list1, with the same additional parameters.
I wrote my own version of this for advent of code after I realized there wasn't a built-in way.
Essentially this is like separated_list0/separated_list1, but with an
init
andg
parameter like fold_many0.This makes it very flexible to parse separated lists and avoid needing to allocate, unless required.
My proposal would be to add a
fold_separated_list0
andfold_separated_list1
.Essentially the code would be the same as
separated_list0
, except for theVec::new()
we callinit()
, and instead ofVec::push()
we callg()
. With these code changes,separated_list0
could be written in terms offold_separated_list0
. Just pass the appropriateVec
methods forinit
andg
.fold_separated_list1
would be based onseparated_list1
, with the same additional parameters.Thoughts? Suggestions?