Closed tom-huntington closed 1 year ago
Sorry I was wrong. partial_fold
is a good name for the anamorphism of this signature (a -> b -> a) -> a -> [b] -> [a]
, however, I have been needing the anamorphism of this signature (a -> b -> a) -> (b -> a) -> [b] -> [a]
and have been hacking a solution together with partial_fold
.
The (a -> b -> a) -> (b -> a) -> [b] -> [a]
anamorphism should be named fold_transform
and here's a python implementation:
def fold_transform(fn, fn0, iterable):
xs = iter(iterable)
x0 = next(xs)
y0 = fn0(x0)
yield y0
y_prev = y0 # y_{i-1}
for x_i in xs:
y_i = fn(y_prev, x_i)
yield y_i
y_prev = y_i
I don't think this will be very high priority so this issue should be closed.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2214r2.html#stdpartial_sum-rangespartial_fold-and-stdinexclusive_scan
I've been using range-v3
partial_sum
and python3accumulate
somewhat often, and I've come to conceptualize it as afold_transform
because what I'm really trying to do is anadjacent_transform
where the output is shifted rather than the input.Food for thought