Loops/Increment loop index within loop body: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
→‎{{header|Python}}: Added a functional draft, expressed in terms of a generic iterate function, implemented as a generator.
Hout (talk | contribs)
→‎Functional Python: Pruned out a little scaffolding – two unused imports.
Line 3,236: Line 3,236:
<lang python>'''Loops/Increment loop index within loop body.'''
<lang python>'''Loops/Increment loop index within loop body.'''


from itertools import chain, count, islice, takewhile
from itertools import islice, takewhile
from functools import reduce
from functools import reduce
import operator
import operator
Line 3,297: Line 3,297:
lambda a, f: f(a),
lambda a, f: f(a),
fs[::-1], x
fs[::-1], x
)


# concatMap :: (a -> [b]) -> [a] -> [b]
def concatMap(f):
'''A concatenated list or string over which a function f
has been mapped.
The list monad can be derived by using an (a -> [b])
function which wraps its output in a list (using an
empty list to represent computational failure).
'''
return lambda xs: (
chain.from_iterable(map(f, xs))
)
)