Cheryl's birthday: Difference between revisions

m
m (→‎{{header|REXX}}: added the computer programming language REXX.)
Line 1,963:
<lang python>'''Cheryl's Birthday'''
 
from itertools import (groupby)
from operatorre import not_split
from re import (split)
 
 
Line 1,972 ⟶ 1,971:
'''Derivation of the date.'''
 
(month, day) = (0, 1)
print(
# (3 :: A "Then I also know")
Line 1,998 ⟶ 1,997:
 
 
# QUERY FUNCTIONS -------------------- QUERY FUNCTIONS --------------------
 
# monthsWithUniqueDays :: Bool -> [(Month, Day)] -> [(Month, Day)]
def monthsWithUniqueDays(blnInclude):
'''The subset of months with (or without) unique days.'''
'''
def go(xs):
(month, day) = (0, 1)
months = list(map([fst,(x) for x in uniquePairing(day)(xs)))]
return list(filter([
lambdamd for md: (in xs
if blnInclude or not (md[month] if blnInclude elsein not_months)
)(md[month] in months),
return xsgo
))
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 = list(filter([
lambda k: 1for ==k in len(dct[k].keys(),
dct.keysif 1 == len(dct[k])
))]
return filter(lambda[tpl for tpl: in xs if tpl[i] in uniques, xs)]
return inner
return lambda xs: ap(bindPairs(xs)(go(xs))
 
 
# bindPairs :: [(Month, Day)] -> ((Dict String [String], Dict String [String])
# ((Dict String [String], Dict String [String])
# -> [(Month, Day)]) -> [(Month, Day)]
def bindPairs(xs):
'''List monad injection operator for lists of (Month, Day) pairs.'''
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 dict('''
return {
(k,: list[snd(map(snd,x) for x in m)))] for k, m in groupby(
sorted(xs, key=fst), key=fst
)
)}
 
 
# GENERIC ------------------------- GENERIC ------------------------
 
# 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
return lambda xs: go(xs)
 
 
# 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):
"Swap the'''Swapped components of a pair."
'''
return (tpl[1], tpl[0])
 
9,655

edits