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 itertools import cycle, islice |
from itertools import cycle, islice |
||
⚫ | |||
Line 1,796: | Line 1,796: | ||
insertDict(wheelName)(leftRotate(wheel))(wheels) |
insertDict(wheelName)(leftRotate(wheel))(wheels) |
||
)(v) |
)(v) |
||
return |
return go |
||
return click(wheelMap)('A') |
return click(wheelMap)('A') |
||
Line 1,808: | Line 1,808: | ||
# |
# ------------------------- TEST ------------------------- |
||
# main :: IO () |
# main :: IO () |
||
def main(): |
def main(): |
||
Line 1,832: | Line 1,832: | ||
# |
# ----------------------- 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): |
||
return dict(dct, **{k: v}) |
|||
dup.update({k: v}) |
|||
⚫ | |||
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 |
'''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 |
def nxt(a, x): |
||
tpl = f(a[0])(x) |
tpl = f(a[0])(x) |
||
return |
return tpl[0], a[1] + [tpl[1]] |
||
return lambda acc: lambda xs: ( |
|||
def go(acc): |
|||
) |
def g(xs): |
||
return reduce(nxt, xs, (acc, [])) |
|||
⚫ | |||
return go |
|||