Jump to content

Show ASCII table: Difference between revisions

→‎Python Plain text: Refactored outer level, updated primitives (inc. with docstrings)
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'''Plain itertoolstext importASCII (chain)code table'''
 
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(
_map(compose(concat)(_map(justifyLeftc.ljust(12)(, ' ') for c in xs))) for xs in (
transpose(chunksOf(16)(
chunksOf[asciiEntry(16n) for n in enumFromTo(32)(127)]
_map(asciiEntry)()
enumFromTo(32)(127)
)
)
)
)
)
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([justifyRightstr(3n).rjust(3, ' ')(str(n)), ' : ', k])
)
 
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 (a,the b)contents =of splitAt(n)(t)xs.
Where returnthe []length ifof 0xs ==is len(a)not elseevenly (divible
the final list will be [a]shorter +than go(b)n.'''
return lambda xs: )reduce(
return lambda xsa, i: go(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 ⟶ 1,717:
# concat :: [String] -> String
def concat(xxs):
'''The concatenation of all the elements in a list.'''
xs = list(chain.from_iterable(xxs))
returnunit []= '' if not isinstance(xs, str) else ([]
return unit if not xs else (
''.join(xs) if typeisinstance(xs[0]) is, str) else xs
)
 
 
# enumFromTo :: (Int ->, Int) -> [Int]
def enumFromTo(m):
'''Integer enumeration from m to 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])
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 :: [[Matrix a]] -> [[Matrix a]]
def transpose(xsm):
'''The rows and columns of the argument transposed.
return list(map(list, zip(*xs)))
(The matrix containers and rows can be lists or tuples).'''
defif go(t)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
def unlines(xs):
'''A single newline-delimited string derived
from a list of strings.'''
return '\n'.join(xs)
 
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.