Department numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Python}}: (defined bind in terms of operator.add))
Line 1,925: Line 1,925:
14: 6 5 1 </pre>
14: 6 5 1 </pre>


Or, encoding directly and declaratively in terms of '''reduce''', without importing permutations:
Or, expressing the options directly and declaratively in terms of '''reduce''' and '''add''', without importing permutations:


<lang python>from functools import (reduce)
<lang python>from functools import (reduce)
from operator import (add)




Line 1,960: Line 1,961:
# bind (>>=) :: [a] -> (a -> [b]) -> [b]
# bind (>>=) :: [a] -> (a -> [b]) -> [b]
def bind(xs):
def bind(xs):
return lambda f: reduce(
return lambda f: reduce(add, map(f, xs), [])
lambda a, b: a + b,
map(f, xs),
[]
)