Middle three digits: Difference between revisions

Content added Content deleted
(→‎{{header|Python}}: Added sub-headings, updated primitives, refactored test.)
(→‎Python - Composition of pure functions: Defined output header, updated output, reformatted test.)
Line 4,028: Line 4,028:
def bracketed(x):
def bracketed(x):
return '(' + str(x) + ')'
return '(' + str(x) + ')'

print(
print(
tabulated('Stellar')(either(bracketed)(str))(mid3digits)([
tabulated('Middle three digits, where defined:\n')(
either(bracketed)(str))
(mid3digits)([
123, 12345, 1234567, 987654321, 10001, -10001, -123,
123, 12345, 1234567, 987654321, 10001, -10001, -123,
-100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0
-100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0
Line 4,079: Line 4,081:
def go(fShow, f, xs):
def go(fShow, f, xs):
w = max(map(compose(len)(str), xs))
w = max(map(compose(len)(str), xs))
return s + ':\n' + '\n'.join(
return s + '\n' + '\n'.join(
[str(x).rjust(w, ' ') + ' -> ' + fShow(f(x)) for x in xs]
[str(x).rjust(w, ' ') + ' -> ' + fShow(f(x)) for x in xs]
)
)
Line 4,088: Line 4,090:
main()</lang>
main()</lang>
{{Out}}
{{Out}}
<pre> 123 -> 123
<pre>Middle three digits, where defined:

123 -> 123
12345 -> 234
12345 -> 234
1234567 -> 345
1234567 -> 345