Next highest int from digits: Difference between revisions
→Python: Generator: Tidied, updated primitives.
(→Python: Generator: Tidied, updated primitives.) |
|||
Line 1,286:
A variant which defines (in terms of a concatMap over permutations), a generator of '''all''' digit-shuffle successors for a given integer:
<lang python>'''
from itertools import chain, islice, permutations, tee
# ------------ NON-FINITE STREAM OF SUCCESSORS -------------
# digitShuffleSuccessors :: Int -> [Int]
def digitShuffleSuccessors(n):
'''
successors of n, where 0 <= n.
'''
Line 1,299 ⟶ 1,301:
delta = int(''.join(ds)) - n
return [] if 0 >= delta else [delta]
return map(
permutations(str(n))
))▼
)▼
)▼
#
# main :: IO ()
def main():
Line 1,315 ⟶ 1,321:
harvest = take(n)(ys)
return (
repr(len(harvest)) + ' of ' +
repr(len(list(zs))) + ': '
)
).rjust(12, ' ') + repr(harvest)
return
print(
Line 1,335 ⟶ 1,343:
#
# add (+) :: Num a => a -> a -> a
def add(a):
'''Curried addition.'''
def go(b):
return a + b
return go
# concatMap :: (a -> [b]) -> [a] -> [b]
def concatMap(f):
'''The concatenation of a mapping.
The list monad can be derived by using a function f
▲ empty list to represent computational failure).
'''
def go(xs):
return go
Line 1,351 ⟶ 1,368:
# (b -> String) -> (a -> b) -> [a] -> String
def fTable(s):
'''Heading -> x display function ->
fx display function -> f -> xs -> tabular string.
'''
def
▲ ))
def arrowed(x, y):
return y.rjust(w, ' ') + (
' -> ' + fxShow(f(x))
▲ )
)
return s + '\n' + '\n'.join(
map(arrowed, xs, ys)
)
return goxs
return gof
return gofx
return gox
Line 1,372 ⟶ 1,397:
or xs itself if n > length xs.
'''
else list(islice(xs, n))
▲ )
)
return go
|