Intersecting number wheels: Difference between revisions

Content added Content deleted
m (→‎Python: Functional composition: Updated primitives, tidied.)
Line 1,780: Line 1,780:
<lang python>'''Intersecting number wheels'''
<lang python>'''Intersecting number wheels'''


from functools import reduce
from itertools import cycle, islice
from itertools import cycle, islice
from functools import reduce




Line 1,796: Line 1,796:
insertDict(wheelName)(leftRotate(wheel))(wheels)
insertDict(wheelName)(leftRotate(wheel))(wheels)
)(v)
)(v)
return lambda name: go(name)
return go
return click(wheelMap)('A')
return click(wheelMap)('A')


Line 1,808: Line 1,808:




# TEST ----------------------------------------------------
# ------------------------- TEST -------------------------
# main :: IO ()
# main :: IO ()
def main():
def main():
Line 1,832: Line 1,832:




# GENERIC -------------------------------------------------
# ----------------------- GENERIC ------------------------


# Tuple (,) :: a -> b -> (a, b)
# Tuple (,) :: a -> b -> (a, b)
Line 1,860: Line 1,860:
# insertDict :: String -> a -> Dict -> Dict
# insertDict :: String -> a -> Dict -> Dict
def insertDict(k):
def insertDict(k):
'''A dictionary updated with a (k, v) pair.'''
'''A new dictionary updated with a (k, v) pair.'''
def go(v, dct):
def go(v, dct):
dup = dict(dct)
return dict(dct, **{k: v})
dup.update({k: v})
return dup
return lambda v: lambda dct: go(v, dct)
return lambda v: lambda dct: go(v, dct)


Line 1,870: Line 1,868:
# mapAccumL :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
# mapAccumL :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
def mapAccumL(f):
def mapAccumL(f):
'''A tuple of an accumulation and a list derived by a
'''A tuple of an accumulation and a map
combined map and fold,
with accumulation from left to right.
with accumulation from left to right.
'''
'''
def go(a, x):
def nxt(a, x):
tpl = f(a[0])(x)
tpl = f(a[0])(x)
return (tpl[0], a[1] + [tpl[1]])
return tpl[0], a[1] + [tpl[1]]

return lambda acc: lambda xs: (
reduce(go, xs, (acc, []))
def go(acc):
)
def g(xs):
return reduce(nxt, xs, (acc, []))
return g
return go