А вот так я писал, когда повзрослел немного
import { map, reduce, filter, reject, isNil } from 'ramda';
function RoseTree(value, children) {
if (!this) {
return new RoseTree(value, children);
}
Object.assign(this, { value, children });
}
RoseTree.prototype['fantasy-land/map'] = function (f) {
return RoseTree(f(this.value), map(map(f), this.children));
};
RoseTree.prototype['fantasy-land/reduce'] = function (f, acc) {
return reduce(
(innerAcc, rt) => reduce(f, innerAcc, rt),
f(acc, this.value),
this.children,
);
};
RoseTree.prototype.find = function (f) {
const stack = [this];
while (stack.length) {
const searched = stack.pop();
if (f(searched.value)) return searched;
stack.push(...searched.children);
}
return undefined;
};
RoseTree.prototype.filter = function (f) {
if (!f(this.value)) return null;
return RoseTree(this.value, reject(isNil, map(filter(f), this.children)));
};
export default RoseTree;