Hilbert curve: Difference between revisions

m
(→‎JavaScript Functional: Updated primitives, tidied.)
Line 1,803:
// hilbertTree :: Dict Char [Char] -> Int -> Tree Char
const hilbertTree = rule => n => {
const go = tree => {
Node(const xs = tree.root)(nest;
 
Boolean(tree.nest.length) ? (
return Node(tree.nest.maproot)(go)
Boolean(tree.nestxs.length) ? (
)(txs.nestmap(go)
) : rule[tree.root].map(
x => Nodeflip(xNode)([])
)
);
};
const seed = Node("a")([]);
 
return 0 < Boolean(n) ? (
take(n)(iterate(go)(seed)).slice(-1)[0]
) : seed;
Line 1,833 ⟶ 1,836:
 
return Boolean(t.nest.length) ? (
zipWith(go(r))(centres)(t.nest)
centres
)(t.nest)
.flat()
) : centres;
Line 1,843 ⟶ 1,844:
return go(d)([d, d], tree);
};
 
 
// svgFromPoints :: Int -> [(Int, Int)] -> String
Line 1,856 ⟶ 1,858:
 
// -------------------- TEST ---------------------
const main = () => {
// rule :: Dict Char [Char]hilbertCurve({
const rule = {
a: ["d", "a", "a", "b"],
b: ["c", "b", "b", "a"],
c: ["b", "c", "c", "d"],
d: ["a", "d", "d", "c"]
};
 
// vectors :: Dict Char [(Int, Int)]
const vectors = ({
"a": [
[-1, 1],
Line 1,891 ⟶ 1,884:
[-1, -1]
]
});({
a: ["d", "a", "a", "b"],
 
b: ["c", "b", "b", "a"],
return hilbertCurve(vectors)(rule)(6);
c: ["b", "c", "c", "d"],
};
d: ["a", "d", "d", "c"]
})(6);
 
 
Line 1,909 ⟶ 1,904:
nest: xs || []
});
 
 
// flip :: (a -> b -> c) -> b -> a -> c
const flip = op =>
// The binary function op with
// its arguments reversed.
1 !== op.length ? (
(a, b) => op(b, a)
) : (a => b => op(b)(a));
 
 
Line 1,924 ⟶ 1,928:
};
 
 
// Returns Infinity over objects without finite length.
// This enables zip and zipWith to choose the shorter
// argument when one is non-finite, like cycle, repeat etc
 
// length :: [a] -> Int
9,659

edits