Show ASCII table: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) 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> |
<lang python>'''Plain text ASCII code table''' |
||
from functools import reduce |
|||
from itertools import chain |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
# asciiTable :: String |
# asciiTable :: String |
||
def asciiTable(): |
def asciiTable(): |
||
'''Table of ASCII codes arranged in 16 rows * 6 columns.''' |
|||
return unlines( |
return unlines( |
||
concat(c.ljust(12, ' ') for c in xs) for xs in ( |
|||
transpose( |
transpose(chunksOf(16)( |
||
[asciiEntry(n) for n in enumFromTo(32)(127)] |
|||
)) |
|||
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([ |
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 ---------------------------------------------------- |
|||
⚫ | |||
⚫ | |||
'''Test''' |
|||
⚫ | |||
⚫ | |||
) |
) |
||
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, |
|||
⚫ | |||
subdividing the contents of xs. |
|||
Where the length of xs is not evenly divible |
|||
the final list will be shorter than n.''' |
|||
return lambda xs: reduce( |
|||
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)) |
||
unit = '' if isinstance(xs, str) else [] |
|||
return unit if not xs else ( |
|||
''.join(xs) if |
''.join(xs) if isinstance(xs[0], str) else xs |
||
) |
) |
||
# enumFromTo :: 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 :: |
# transpose :: Matrix a -> Matrix a |
||
def transpose( |
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).''' |
|||
⚫ | |||
inner = type(m[0]) |
|||
⚫ | |||
return (type(m))( |
|||
map(inner, z) if tuple != inner else z |
|||
⚫ | |||
⚫ | |||
⚫ | |||
# 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) |
||