Middle three digits: Difference between revisions
Content deleted Content added
→{{header|Python}}: Added sub-headings, updated primitives, refactored test. |
|||
Line 3,966: | Line 3,966: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Procedural=== |
|||
<lang python>>>> def middle_three_digits(i): |
<lang python>>>> def middle_three_digits(i): |
||
s = str(abs(i)) |
s = str(abs(i)) |
||
Line 4,002: | Line 4,003: | ||
>>> </lang> |
>>> </lang> |
||
===Composition of pure functions=== |
|||
Using a composable option type as an alternative to error handling: |
|||
{{Trans|Haskell}} |
{{Trans|Haskell}} |
||
<lang python> |
<lang python>'''Middle 3 digits''' |
||
# mid3digits :: Int -> Either String String |
# mid3digits :: Int -> Either String String |
||
def mid3digits(n): |
def mid3digits(n): |
||
'''Either the middle three digits or an |
|||
⚫ | |||
explanatory string.''' |
|||
⚫ | |||
⚫ | |||
m = abs(n) |
m = abs(n) |
||
s = str(m) |
s = str(m) |
||
return Left('Less than 3 digits') if (100 > m) else ( |
return Left('Less than 3 digits') if (100 > m) else ( |
||
Left('Even digit count') if even(len(s)) else |
Left('Even digit count') if even(len(s)) else Right( |
||
⚫ | |||
⚫ | |||
) |
) |
||
Line 4,023: | Line 4,024: | ||
# TEST ---------------------------------------------------- |
# TEST ---------------------------------------------------- |
||
def main(): |
def main(): |
||
'''Test''' |
|||
⚫ | |||
⚫ | |||
⚫ | |||
return '(' + str(x) + ')' |
|||
⚫ | |||
⚫ | |||
# = max(len(x) for x in [str(x) for x in xs]) |
|||
tabulated('Stellar')(either(bracketed)(str))(mid3digits)([ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
]) |
|||
justifyRight(mx)(' ')(str(n)) + ' -> ' + ( |
|||
either(lambda s: '(' + str(s) + ')')(str)(mid3digits(n)) |
|||
⚫ | |||
) for n in xs |
|||
]), |
|||
) |
) |
||
# GENERIC ------------------------------------------------- |
# GENERIC ------------------------------------------------- |
||
⚫ | |||
⚫ | |||
'''Constructor for an empty Either (option type) value |
|||
with an associated string.''' |
|||
⚫ | |||
⚫ | |||
⚫ | |||
'''Constructor for a populated Either (option type) value''' |
|||
return {'type': 'Either', 'Left': None, 'Right': x} |
|||
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c |
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c |
||
def compose(g): |
def compose(g): |
||
'''Function composition.''' |
|||
return lambda f: lambda x: g(f(x)) |
return lambda f: lambda x: g(f(x)) |
||
Line 4,050: | Line 4,060: | ||
# either :: (a -> c) -> (b -> c) -> Either a b -> c |
# either :: (a -> c) -> (b -> c) -> Either a b -> c |
||
def either(fl): |
def either(fl): |
||
'''The application of fl to e if e is a Left value, |
|||
or the application of fr to e if e is a Right value.''' |
|||
return lambda fr: lambda e: fl(e['Left']) if ( |
return lambda fr: lambda e: fl(e['Left']) if ( |
||
None is e['Right'] |
None is e['Right'] |
||
Line 4,057: | Line 4,069: | ||
# even :: Int -> Bool |
# even :: Int -> Bool |
||
def even(x): |
def even(x): |
||
'''Is x even ?''' |
|||
return 0 == x % 2 |
return 0 == x % 2 |
||
# |
# tabulated :: String -> (b -> String) -> (a -> b) -> [a] -> String |
||
def |
def tabulated(s): |
||
'''Heading -> display function -> tested function -> |
|||
return lambda cFiller: lambda a: ( |
|||
value list -> tabular string.''' |
|||
def go(fShow, f, xs): |
|||
) |
|||
⚫ | |||
⚫ | |||
[str(x).rjust(w, ' ') + ' -> ' + fShow(f(x)) for x in xs] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
return |
return lambda fShow: lambda f: lambda xs: go(fShow, f, xs) |
||
⚫ | |||
⚫ | |||
⚫ | |||