Closed
Description
The core internal iteration methods in terms of which the other Iterator
methods can be implemented.
PR #45595, commit b32267f -- merged 2017-11-17
https://doc.rust-lang.org/nightly/std/iter/trait.Iterator.html#method.try_fold
https://doc.rust-lang.org/nightly/std/iter/trait.DoubleEndedIterator.html#method.try_rfold
Also try_for_each
(PR #48157, merged 2018-02-24), a convenience method that can be considered the fallible version of for_each
or the stateless version of try_fold
.
https://doc.rust-lang.org/nightly/std/iter/trait.Iterator.html#method.try_for_each
Pre-stabilization question list:
- Are these the best type parameters? Inference worse in method signature using associated type than with new generic type parameter eq-constrained to the associated type #45462Should there be a
try_foreach
as well? impl FromIterator<()> for () #45379 (comment)Stabilizerfold
beforetry_rfold
? Tracking issue for Iterator::try_fold and try_rfold (feature iterator_try_fold) #45594 (comment)Stabilizetry_fold
before the others? Tracking issue for Iterator::try_fold and try_rfold (feature iterator_try_fold) #45594 (comment)
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
leonardo-m commentedon Oct 28, 2017
Is this usable to implement a fold1?
http://zvon.org/other/haskell/Outputprelude/foldl1_f.html
bluss commentedon Oct 28, 2017
There's already a fold1 in itertools and its implementation is quite simple:
The major difference to haskell foldl1 is that itertools fold1 returns an Option, with None for when the iterator is empty, instead of a runtime error like in Haskell.
scottmcm commentedon Oct 29, 2017
That same technique is also used for the
min
&max
methods onIterator
: https://github.com/rust-lang/rust/pull/45595/files#diff-1398f322bc563592215b583e9b0ba936R2360cuviper commentedon Nov 8, 2017
I think
try_rfold
should conceptually depend on stabilizingrfold
first -- #44705.(even though the default implementation goes the other way,
rfold
callingtry_rfold
)scottmcm commentedon Nov 8, 2017
Well,
rfold
is already in the current beta, so it could probably be proposed for stabilization in 1.23...Iterator::try_fold
rayon-rs/rayon#495cramertj commentedon Jan 4, 2018
I wound up writing some code that really wanted
try_filter_map
(return aResult<Option<T>>
instead of justOption<T>
), and instead wound up doing something like.map(|x| x.to_res_opt()).try_filter_map(res_opt_to_opt_res)
. I wound up opening this PR as a result, which allows you to useInto
to get theres_opt_to_opt_res
behavior, however I still think built-intry_xxx
s are more straightforward (I could seetry_filter
,try_filter_map
,try_scan
,try_inspect
, etc... all being handy). I'm not sure how far we should take this before we just say to write a dangfor
loop 😜find_map
to the Iterator trait rust-lang/rfcs#1801Rollup merge of rust-lang#48157 - scottmcm:try-for-each, r=dtolnay
cuviper commentedon Feb 20, 2018
Should we try to stabilize
try_fold
alone? (leavingtry_rfold
andtry_for_each
for later)SoniEx2 commentedon Feb 23, 2018
Why not try_fold into
Iterator<R>
? make it the opposite of flat_map!19 remaining items