brevzin / cpp_proposals

My WG21 proposals
35 stars 22 forks source link

<strike>Rename partial_fold as</strike> fold_transform anamorphism p2214 #36

Closed tom-huntington closed 1 year ago

tom-huntington commented 2 years ago

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 python3 accumulate somewhat often, and I've come to conceptualize it as a fold_transform because what I'm really trying to do is an adjacent_transform where the output is shifted rather than the input.

Food for thought

tom-huntington commented 2 years 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.