Show ASCII table: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: Re-added erroneously removed markup)
(→‎Python Plain text: Refactored outer level, updated primitives (inc. with docstrings))
Line 1,654: Line 1,654:


Composed from generic abstractions:
Composed from generic abstractions:
<lang python>from itertools import (chain)
<lang python>'''Plain text ASCII code table'''


from functools import reduce

from itertools import chain
# main :: IO ()
def main():
print(
asciiTable()
)




# asciiTable :: String
# asciiTable :: String
def asciiTable():
def asciiTable():
'''Table of ASCII codes arranged in 16 rows * 6 columns.'''
return unlines(
return unlines(
_map(compose(concat)(_map(justifyLeft(12)(' '))))(
concat(c.ljust(12, ' ') for c in xs) for xs in (
transpose(
transpose(chunksOf(16)(
chunksOf(16)(
[asciiEntry(n) for n in enumFromTo(32)(127)]
_map(asciiEntry)(
))
enumFromTo(32)(127)
)
)
)
)
)
)
)
Line 1,681: Line 1,674:
# asciiEntry :: Int -> String
# asciiEntry :: Int -> String
def asciiEntry(n):
def asciiEntry(n):
'''Number: character string for point in ASCII code'''
k = asciiName(n)
k = asciiName(n)
return k if '' == k else (
return k if '' == k else (
concat([justifyRight(3)(' ')(str(n)), ' : ', k])
concat([str(n).rjust(3, ' '), ' : ', k])
)
)


Line 1,689: Line 1,683:
# asciiName :: Int -> String
# asciiName :: Int -> String
def asciiName(n):
def asciiName(n):
'''Name or character for given ASCII code.'''
return '' if 32 > n or 127 < n else (
return '' if 32 > n or 127 < n else (
'Spc' if 32 == n else (
'Spc' if 32 == n else (
'Del' if 127 == n else chr(n)
'Del' if 127 == n else chr(n)
)
)
)


# TEST ----------------------------------------------------
# main :: IO ()
def main():
'''Test'''
print(
asciiTable()
)
)


Line 1,700: Line 1,704:
# chunksOf :: Int -> [a] -> [[a]]
# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
def chunksOf(n):
'''A series of lists of length n,
def go(t):
(a, b) = splitAt(n)(t)
subdividing the contents of xs.
return [] if 0 == len(a) else (
Where the length of xs is not evenly divible
[a] + go(b)
the final list will be shorter than n.'''
)
return lambda xs: reduce(
return lambda xs: go(xs)
lambda a, i: a + [xs[i:n + i]],
range(0, len(xs), n), []

) if 0 < n else []

# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
def compose(g):
return lambda f: lambda x: g(f(x))




Line 1,716: Line 1,717:
# concat :: [String] -> String
# concat :: [String] -> String
def concat(xxs):
def concat(xxs):
'''The concatenation of all the elements in a list.'''
xs = list(chain.from_iterable(xxs))
xs = list(chain.from_iterable(xxs))
return [] if not xs else (
unit = '' if isinstance(xs, str) else []
return unit if not xs else (
''.join(xs) if type(xs[0]) is str else xs
''.join(xs) if isinstance(xs[0], str) else xs
)
)




# enumFromTo :: Int -> Int -> [Int]
# enumFromTo :: (Int, Int) -> [Int]
def enumFromTo(m):
def enumFromTo(m):
'''Integer enumeration from m to n.'''
return lambda n: list(range(m, 1 + n))
return lambda n: list(range(m, 1 + n))


# justifyLeft :: Int -> Char -> String -> String
def justifyLeft(n):
return lambda cFiller: lambda a: (
(str(a) + (n * cFiller))[:n]
)


# justifyRight :: Int -> Char -> String -> String
def justifyRight(n):
return lambda cFiller: lambda a: (
((n * cFiller) + str(a))[-n:]
)


# Curried map, returning a list
# map :: (a -> b) -> [a] -> [b]
def _map(f):
return lambda xs: list(map(f, xs))




# splitAt :: Int -> [a] -> ([a], [a])
# splitAt :: Int -> [a] -> ([a], [a])
def splitAt(n):
def splitAt(n):
'''A tuple pairing the prefix of length n
with the rest of xs'''
return lambda xs: (xs[0:n], xs[n:])
return lambda xs: (xs[0:n], xs[n:])




# transpose :: [[a]] -> [[a]]
# transpose :: Matrix a -> Matrix a
def transpose(xs):
def transpose(m):
'''The rows and columns of the argument transposed.
return list(map(list, zip(*xs)))
(The matrix containers and rows can be lists or tuples).'''
if m:
inner = type(m[0])
z = zip(*m)
return (type(m))(
map(inner, z) if tuple != inner else z
)
else:
return m




# unlines :: [String] -> String
# unlines :: [String] -> String
def unlines(xs):
def unlines(xs):
'''A single newline-delimited string derived
from a list of strings.'''
return '\n'.join(xs)
return '\n'.join(xs)