RosettaCodeData/Task/Generate-lower-case-ASCII-alphabet/Python/generate-lower-case-ascii-alphabet-2.py
2023-07-01 13:44:08 -04:00

101 lines
2.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'''Enumeration a-z'''
from inspect import signature
import enum
# TEST ----------------------------------------------------
def main():
'''Testing particular instances of a general pattern:
'''
print(
fTable(__doc__ + ':\n')(repr)(showList)(
uncurry(enumFromTo)
)([
('a', 'z'),
('α', 'ω'),
('א', 'ת'),
(1, 10),
(round((5**(1 / 2) - 1) / 2, 5), 5),
('🌱', '🍂')
])
)
# GENERIC -------------------------------------------------
# enumFromTo :: Enum a => a -> a -> [a]
def enumFromTo(m):
'''Enumeration of values [m..n]'''
def go(x, y):
t = type(m)
i = fromEnum(x)
d = 0 if t != float else (x - i)
return list(map(
lambda x: toEnum(t)(d + x),
range(i, 1 + fromEnum(y))
) if int != t else range(x, 1 + y))
return lambda n: go(m, n)
# fromEnum :: Enum a => a -> Int
def fromEnum(x):
'''Index integer for enumerable value.'''
Enum = enum.Enum
return ord(x) if isinstance(x, str) else (
x.value if isinstance(x, Enum) else int(x)
)
# toEnum :: Type -> Int -> a
def toEnum(t):
'''Enumerable value from index integer'''
dct = {
int: int,
float: float,
str: chr,
bool: bool
}
return lambda x: dct[t](x) if t in dct else t(x)
# uncurry :: (a -> b -> c) -> ((a, b) -> c)
def uncurry(f):
'''A function over a tuple, derived from
a vanilla or curried function.
'''
if 1 < len(signature(f).parameters):
return lambda xy: f(*xy)
else:
return lambda xy: f(xy[0])(xy[1])
# FORMATTING -------------------------------------------------
# fTable :: String -> (a -> String) ->
# (b -> String) -> (a -> b) -> [a] -> String
def fTable(s):
'''Heading -> x display function -> fx display function ->
f -> xs -> tabular string.
'''
def go(xShow, fxShow, f, xs):
ys = [xShow(x) for x in xs]
w = max(map(len, ys))
return s + '\n' + '\n'.join(map(
lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
xs, ys
))
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
xShow, fxShow, f, xs
)
# showList :: [a] -> String
def showList(xs):
'''Stringification of a list.'''
return '[' + ','.join(str(x) for x in xs) + ']'
# MAIN ---
if __name__ == '__main__':
main()