Show ASCII table: Difference between revisions
→Python Plain text: Refactored outer level, updated primitives (inc. with docstrings)
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:
Composed from generic abstractions:
<lang python>
from functools import reduce
from itertools import chain
# main :: IO ()▼
def main():▼
print(▼
asciiTable()▼
)▼
# asciiTable :: String
def asciiTable():
'''Table of ASCII codes arranged in 16 rows * 6 columns.'''
return unlines(
transpose(chunksOf(16)(
)▼
)▼
)▼
)
)
Line 1,681 ⟶ 1,674:
# asciiEntry :: Int -> String
def asciiEntry(n):
'''Number: character string for point in ASCII code'''
k = asciiName(n)
return k if '' == k else (
concat([
)
Line 1,689 ⟶ 1,683:
# asciiName :: Int -> String
def asciiName(n):
'''Name or character for given ASCII code.'''
return '' if 32 > n or 127 < n else (
'Spc' if 32 == n else (
'Del' if 127 == n else chr(n)
)
▲ )
# TEST ----------------------------------------------------
▲# main :: IO ()
▲def main():
'''Test'''
▲ print(
▲ asciiTable()
)
Line 1,700 ⟶ 1,704:
# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
'''A series of lists of length n,
def go(t):▼
subdividing
Where
the final list will be
return lambda xs:
range(0, len(xs), n), []
) if 0 < n else []
Line 1,716 ⟶ 1,717:
# concat :: [String] -> String
def concat(xxs):
'''The concatenation of all the elements in a list.'''
xs = list(chain.from_iterable(xxs))
return unit if not xs else (
''.join(xs) if
)
# enumFromTo :: (Int
def enumFromTo(m):
'''Integer enumeration from m to n.'''
return lambda n: list(range(m, 1 + n))
)▼
# splitAt :: Int -> [a] -> ([a], [a])
def splitAt(n):
'''A tuple pairing the prefix of length n
with the rest of xs'''
return lambda xs: (xs[0:n], xs[n:])
# transpose ::
def transpose(
'''The rows and columns of the argument transposed.
(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
def unlines(xs):
'''A single newline-delimited string derived
from a list of strings.'''
return '\n'.join(xs)
|