Middle three digits: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
→‎{{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===

Or, using an option type:
Using a composable option type as an alternative to error handling:
{{Trans|Haskell}}
{{Trans|Haskell}}
<lang python>from functools import (reduce)
<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
def go(s):
return Right(
explanatory string.'''
s[(len(s) - 3) // 2:][0:3]
)
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 go(s)
Left('Even digit count') if even(len(s)) else Right(
s[(len(s) - 3) // 2:][0:3]
)
)
)


Line 4,023: Line 4,024:
# TEST ----------------------------------------------------
# TEST ----------------------------------------------------
def main():
def main():
xs = [
'''Test'''

123, 12345, 1234567, 987654321, 10001, -10001, -123,
def bracketed(x):
-100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0
return '(' + str(x) + ')'
]
mx = max(map(compose(len)(str), xs))
print(
# = max(len(x) for x in [str(x) for x in xs])
tabulated('Stellar')(either(bracketed)(str))(mid3digits)([
print (
123, 12345, 1234567, 987654321, 10001, -10001, -123,
'\n'.join([
-100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0
(
])
justifyRight(mx)(' ')(str(n)) + ' -> ' + (
either(lambda s: '(' + str(s) + ')')(str)(mid3digits(n))
)
) for n in xs
]),
)
)




# GENERIC -------------------------------------------------
# GENERIC -------------------------------------------------

# Left :: a -> Either a b
def Left(x):
'''Constructor for an empty Either (option type) value
with an associated string.'''
return {'type': 'Either', 'Right': None, 'Left': x}


# Right :: b -> Either a b
def Right(x):
'''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




# justifyRight :: Int -> Char -> String -> String
# tabulated :: String -> (b -> String) -> (a -> b) -> [a] -> String
def justifyRight(n):
def tabulated(s):
'''Heading -> display function -> tested function ->
return lambda cFiller: lambda a: (
((n * cFiller) + str(a))[-n:]
value list -> tabular string.'''
def go(fShow, f, xs):
)
w = max(map(compose(len)(str), xs))

return s + ':\n' + '\n'.join(

[str(x).rjust(w, ' ') + ' -> ' + fShow(f(x)) for x in xs]
# Left :: a -> Either a b
)
def Left(x):
return {'type': 'Either', 'Right': None, 'Left': x}
return lambda fShow: lambda f: lambda xs: go(fShow, f, xs)


# Right :: b -> Either a b
def Right(x):
return {'type': 'Either', 'Left': None, 'Right': x}