Intercalating rose trees

node v8.12.0
version: 1.0.0
endpointsharetweet
require("@babel/polyfill"); const { Fn, IntSum, Arr, implement, Functor, Apply, Chain, Foldable, Traversable } = require("@masaeedu/fp"); const RoseTree_ = (() => { // Constructors const Node = l => c => ({ label: l, children: c }); // Traversable const traverse = A => f => ({ label, children }) => A.lift2(Node)(f(label))(Arr.traverse(A)(traverse(A)(f))(children)); return { Node, traverse } })() // derive stuff const derivations = Arr.map(implement)([Traversable, Foldable, Functor]) const RoseTree = Fn.pipe(derivations)(RoseTree_) const intercalate = F => M => sep => { let empty = true; return F.foldl(m => x => M.append(m)(empty ? (empty = false, x) : M.append(sep)(x)))(M.empty) } const Str = { empty: "", append: x => y => x + y } const { Node: N } = RoseTree; intercalate(RoseTree)(Str)(", ")( N("one")([ N("two")([ N("three")([]) ]), N("four")([]) ]) ) // => "one, two, three, four"
Loading…

no comments

    sign in to comment