Hilbert curve: Difference between revisions

m
→‎Python Functional: Updated primitives. Tidied.
m (→‎Python Functional: Updated primitives. Tidied.)
Line 2,213:
<lang Python>'''Hilbert curve'''
 
from itertools import (chain, islice, starmap)
from inspect import signature
 
 
Line 2,278 ⟶ 2,277:
def go(xy, tree):
r = d // 2
 
centres = map(
def lambda deltas(v): (
centres = map return (
xy[0] + (r * v[0]),
xy[1] + (r * v[1])
),
centres = map(deltas, vectors[tree['root']])
)
return chain.from_iterable(
starmapmap(points(r), zip(centres, tree['nest']))
) if tree['nest'] else centres
return lambda xy, tree: go(xy, tree)
 
d = w // 2
Line 2,298 ⟶ 2,297:
'''Width of square canvas -> Point list -> SVG string'''
 
def go(w, xys):
xsdef = ' '.join(mappoints(xy):
lambda xy:return str(xy[0]) + ' ' + str(xy[1]),
xs = ' '.join(map(points, xys))
))
return '\n'.join(
['<svg xmlns="http://www.w3.org/2000/svg"',
Line 2,311 ⟶ 2,309:
]
)
return lambda xys: go(w, xys)
 
 
# TEST -------------------------- TEST --------------------------
def main():
'''Testing generation of the SVG for a Hilbert curve'''
Line 2,320 ⟶ 2,318:
hilbertCurve(6)
)
)print()
 
 
# GENERIC FUNCTIONS -------------------- GENERIC FUNCTIONS -------------------
 
# Node :: a -> [Tree a] -> Tree a
Line 2,335 ⟶ 2,334:
def flip(f):
'''The (curried or uncurried) function f with its
arguments reversed.'''
else:'''
if 1 < len(signature(f).parameters):
return lambda a,: lambda b: f(b, )(a)
else:
return lambda a: lambda b: f(b)(a)
 
 
Line 2,352 ⟶ 2,349:
yield v
v = f(v)
return lambda x: go(x)
 
 
9,659

edits