Cheryl's birthday: Difference between revisions
m
→Python :: Functional: Tidied
m (→{{header|REXX}}: added the computer programming language REXX.) |
m (→Python :: Functional: Tidied) |
||
Line 1,963:
<lang python>'''Cheryl's Birthday'''
from itertools import
from
Line 1,972 ⟶ 1,971:
'''Derivation of the date.'''
print(
# (3 :: A "Then I also know")
Line 1,998 ⟶ 1,997:
#
# monthsWithUniqueDays :: Bool -> [(Month, Day)] -> [(Month, Day)]
def monthsWithUniqueDays(blnInclude):
'''The subset of months with (or without) unique days.
'''
def go(xs):
months =
return
if blnInclude or not (md[month]
return
))▼
return lambda xs: go(xs)▼
# uniquePairing :: DatePart -> [(Month, Day)] -> [(Month, Day)]
def uniquePairing(i):
'''Subset of months (or days) with a unique intersection.
'''
def go(xs):
def inner(md):
dct = md[i]
uniques =
return
return inner
return
# bindPairs :: [(Month, Day)] ->
# ((Dict String [String], Dict String [String])
#
def bindPairs(xs):
'''List monad injection operator for lists
of (Month, Day) pairs.
'''
return lambda f: list(f(
(dictFromPairs(xs), dictFromPairs(map(swap, xs)))
Line 2,042 ⟶ 2,044:
def dictFromPairs(xs):
'''A dictionary derived from a list of
month day pairs.
return {
sorted(xs, key=fst), key=fst
)
#
# ap :: (a -> b -> c) -> (a -> b) -> a -> c
def ap(f):
'''Applicative instance for functions.
'''
def go(g):
def fxgx(x):
return f(x)(
g(x)
return fxgx
# fst :: (a, b) -> a
def fst(tpl):
'''First component of a pair.
'''
return tpl[0]
Line 2,060 ⟶ 2,077:
# snd :: (a, b) -> b
def snd(tpl):
'''Second component of a pair.
'''
return tpl[1]
Line 2,066 ⟶ 2,084:
# swap :: (a, b) -> (b, a)
def swap(tpl):
'''
return (tpl[1], tpl[0])
|