Roman numerals/Decode: Difference between revisions
Content added Content deleted
m (→Python Declarative: Tidied) |
|||
Line 5,398: | Line 5,398: | ||
if no letter value is defined. |
if no letter value is defined. |
||
''' |
''' |
||
if |
if None in (mb, x): |
||
return |
return None |
||
else: |
else: |
||
r, total = mb |
r, total = mb |
||
return |
return ( |
||
x, |
x, |
||
total + (-x if x < r else x) |
total + (-x if x < r else x) |
||
) |
|||
dct = defaultdict( |
dct = defaultdict( |
||
Line 5,418: | Line 5,418: | ||
go, |
go, |
||
[dct[k.upper()] for k in reversed(list(s))], |
[dct[k.upper()] for k in reversed(list(s))], |
||
(0, 0) |
|||
) |
) |
||
)(snd) |
|||
# |
# ------------------------- TEST ------------------------- |
||
def main(): |
def main(): |
||
'''Testing a sample of dates.''' |
'''Testing a sample of dates.''' |
||
Line 5,439: | Line 5,439: | ||
# |
# ----------------------- GENERIC ------------------------ |
||
# Just :: a -> Maybe a |
|||
def Just(x): |
|||
'''Constructor for an inhabited Maybe (option type) value.''' |
|||
return {'type': 'Maybe', 'Nothing': False, 'Just': x} |
|||
# Nothing :: Maybe a |
|||
def Nothing(): |
|||
'''Constructor for an empty Maybe (option type) value.''' |
|||
return {'type': 'Maybe', 'Nothing': True} |
|||
# bindMay (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b |
# bindMay (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b |
||
Line 5,460: | Line 5,448: | ||
of the (a -> Maybe b) function (mf) to x.''' |
of the (a -> Maybe b) function (mf) to x.''' |
||
return lambda mf: ( |
return lambda mf: ( |
||
m if m |
m if None is m else mf(m) |
||
) |
) |
||
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c |
|||
def compose(g): |
|||
'''Right to left function composition.''' |
|||
return lambda f: lambda x: g(f(x)) |
|||
Line 5,476: | Line 5,458: | ||
where m is Just(x). |
where m is Just(x). |
||
''' |
''' |
||
return lambda f: lambda m: v if m |
return lambda f: lambda m: v if None is m else ( |
||
f(m |
f(m) |
||
) |
) |
||
Line 5,487: | Line 5,469: | ||
# |
# ---------------------- FORMATTING ---------------------- |
||
# fTable :: String -> (a -> String) -> |
# fTable :: String -> (a -> String) -> |
||
# |
# (b -> String) -> (a -> b) -> [a] -> String |
||
def fTable(s): |
def fTable(s): |
||
'''Heading -> x |
'''Heading -> x display function -> |
||
fx display function -> f -> xs -> tabular string. |
|||
''' |
''' |
||
def go(xShow, fxShow, f, xs): |
def go(xShow, fxShow, f, xs): |
||
Line 5,499: | Line 5,481: | ||
w = max(map(len, ys)) |
w = max(map(len, ys)) |
||
return s + '\n' + '\n'.join(map( |
return s + '\n' + '\n'.join(map( |
||
lambda x, y: |
lambda x, y: ( |
||
f'{y.rjust(w, " ")} -> {fxShow(f(x))}' |
|||
), |
|||
xs, ys |
xs, ys |
||
)) |
)) |
||
return lambda xShow: lambda fxShow: lambda f: |
return lambda xShow: lambda fxShow: lambda f: ( |
||
xShow, fxShow, f, xs |
lambda xs: go(xShow, fxShow, f, xs) |
||
) |
) |
||
Line 5,519: | Line 5,503: | ||
MMXVIII -> 2018 |
MMXVIII -> 2018 |
||
MMZZIII -> (Contains unknown character)</pre> |
MMZZIII -> (Contains unknown character)</pre> |
||
=={{header|QBasic}}== |
=={{header|QBasic}}== |