Sum of first n cubes: Difference between revisions

→‎Python :: Functional: Inlined call to itertools.accumulate
m (Rust - simplified code)
(→‎Python :: Functional: Inlined call to itertools.accumulate)
Line 1,389:
<lang python>'''Sum of first N cubes'''
 
from itertools import accumulate, chain
 
 
Line 1,399:
return a + (x ** 3)
 
return scanlaccumulate(go)range(0, n), go)(
range(1, n)
)
 
 
Line 1,413 ⟶ 1,411:
])
)
 
 
# ----------------------- GENERIC ------------------------
 
# scanl :: (b -> a -> b) -> b -> [a] -> [b]
def scanl(f):
'''scanl is like reduce, but defines a succession of
intermediate values, building from the left.
'''
def go(a):
def g(xs):
return accumulate(chain([a], xs), f)
return g
return go
 
 
9,655

edits