[Python-ideas] Start argument for itertools.accumulate() [Was: Proposal: A Reduce-Map Comprehension and a "last" builtin]
Tim Peters
tim.peters at gmail.com
Sun Apr 8 03:02:58 EDT 2018
FYI:
[Raymond]
> ...
> Q. Do other languages do it?
> A. Numpy, no. R, no. APL, no. Mathematica, no. Haskell, yes.
>
> ...
> * https://www.haskell.org/hoogle/?hoogle=mapAccumL
Haskell has millions of functions ;-) `mapAccumL` is a God-awful
mixture of Python's map(), reduce(), and accumulate() :-( The
examples here should convince you it's nearly incomprehensible:
http://zvon.org/other/haskell/Outputlist/mapAccumL_f.html
A more-than-less direct equivalent to Python's `accumulate` is
Haskell's `scanl1`:
http://zvon.org/comp/r/ref-Haskell.html#Functions~Prelude.scanl1
That doesn't allow specifying an initial value. But, Haskell being
Haskell, the closely related `scanl` function requires specifying an
initial value, which is also the first element of the list it returns:
http://zvon.org/comp/r/ref-Haskell.html#Functions~Prelude.scanl
Of the two, `scanl` is more basic - indeed, the Standard Prelude
defines scanl1 by peeling off the first element of the list and
passing it as the initial value for scanl to use
scanl :: (a -> b -> a) -> a -> [b] -> [a]
scanl f q xs = q : (case xs of
[] -> []
x:xs -> scanl f (f q x) xs)
scanl1 :: (a -> a -> a) -> [a] -> [a]
scanl1 f (x:xs) = scanl f x xs
scanl1 _ [] = []
There are also analogous `scanr` and `scanr1` functions for doing
"right-to-left" accumulation.
So, bottom line: as usual, when looking to Haskell for prior art,,
"all of the above - for a start" applies ;-)
More information about the Python-ideas
mailing list