Intersecting number wheels: Difference between revisions

m
→‎Python: Functional composition: Updated primitives, tidied.
m (→‎Python: Functional composition: Updated primitives, tidied.)
Line 1,780:
<lang python>'''Intersecting number wheels'''
 
from functools import reduce
from itertools import cycle, islice
from functools import reduce
 
 
Line 1,796:
insertDict(wheelName)(leftRotate(wheel))(wheels)
)(v)
return lambda name: go(name)
return click(wheelMap)('A')
 
Line 1,808:
 
 
# TEST --------------------------- TEST -------------------------
# main :: IO ()
def main():
Line 1,832:
 
 
# GENERIC ------------------------- GENERIC ------------------------
 
# Tuple (,) :: a -> b -> (a, b)
Line 1,860:
# insertDict :: String -> a -> Dict -> Dict
def insertDict(k):
'''A new dictionary updated with a (k, v) pair.'''
def go(v, dct):
dup =return dict(dct, **{k: v})
dup.update({k: v})
return dup
return lambda v: lambda dct: go(v, dct)
 
Line 1,870 ⟶ 1,868:
# mapAccumL :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
def mapAccumL(f):
'''A tuple of an accumulation and a list derived by amap
combined map and fold,
with accumulation from left to right.
'''
def gonxt(a, x):
tpl = f(a[0])(x)
return (tpl[0], a[1] + [tpl[1]])
 
return lambda acc: lambda xs: (
def reduce(go, xs, (acc, [])):
def g(xs):
return reduce(nxt, xs, (acc, []))
return dupg
return go
 
 
9,655

edits