Roman numerals/Decode: Difference between revisions

Content added Content deleted
Line 5,398: Line 5,398:
if no letter value is defined.
if no letter value is defined.
'''
'''
if mb.get('Nothing') or None is x:
if None in (mb, x):
return Nothing()
return None
else:
else:
r, total = mb.get('Just')
r, total = mb
return Just((
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))],
Just((0, 0))
(0, 0)
)
)
)(compose(Just)(snd))
)(snd)




# TEST ----------------------------------------------------
# ------------------------- TEST -------------------------
def main():
def main():
'''Testing a sample of dates.'''
'''Testing a sample of dates.'''
Line 5,439: Line 5,439:




# GENERIC -------------------------------------------------
# ----------------------- 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.get('Nothing') else mf(m.get('Just'))
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.get('Nothing') else (
return lambda f: lambda m: v if None is m else (
f(m.get('Just'))
f(m)
)
)


Line 5,487: Line 5,469:




# FORMATTING ----------------------------------------------
# ---------------------- FORMATTING ----------------------


# fTable :: String -> (a -> String) ->
# fTable :: String -> (a -> String) ->
# (b -> String) -> (a -> b) -> [a] -> String
# (b -> String) -> (a -> b) -> [a] -> String
def fTable(s):
def fTable(s):
'''Heading -> x display function -> fx display function ->
'''Heading -> x display function ->
f -> xs -> tabular string.
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: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
lambda x, y: (
f'{y.rjust(w, " ")} -> {fxShow(f(x))}'
),
xs, ys
xs, ys
))
))
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
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}}==