Open malthe opened 2 years ago
If you need a map
with some internal state, maybe you want https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.scan ?
@scottmcm scan
returns that internal state as the result – what I am looking for is to still return the result of the map so to speak (and simply discarding the internal state after iteration completes).
I have found it useful to have a map that is able to hold a cached value, which can be used during the iteration.
The name
fold_map
comes to mind because it can be implemented usingfold
but works like amap
.In the example below, a vector of booleans is mapped such that values around a true value are also true.
This would yield an iterator for
[true, true, false, true, true, true, true]
.And a smaller, somewhat random example:
This would yield an iterator for
[1, 3, 5, 7]
.