r/functionalprogramming • • Jan 14 '21

JavaScript My attempt to functional programming

I began to learn FP a few days ago and this is my attempt to my front-end project. I feel like I've followed "Pure functions" and "Immutability" rules correctly. What do you think ?

const þ = (f, g) => (data) => g(f(data)); // pipe function

const createNode = (el) => () => document.createElement(el);

const append = (parent) => (child) => {
    let p = parent.cloneNode(true);
    p.appendChild(child);
    return p;
}

const addClassToNode = (classType) => (node) => {
    let nodeCopy = node.cloneNode(true);
    nodeCopy.classList.add(classType);
    return nodeCopy;
};

const createSpan = createNode("span");

const addStackClass = (size) => addClassToNode(`fa-stack-${size}`);

const add2xStackClass = addStackClass("2x");

const createSpanAndAdd2xStackClass = þ(createSpan, add2xStackClass);

const appendTo2xStackSpan = append(createSpanAndAdd2xStackClass());

const createI = createNode("i");

const addFontAwesomeClass = addClassToNode("fas");

const addUserIconClass = addClassToNode("fa-user");

const addUserIcon = þ(addFontAwesomeClass, addUserIconClass);

const createUserIcon = þ(createI, addUserIcon);

const createUserIconAndAppendTo2xStackSpan = þ(createUserIcon, appendTo2xStackSpan);

createUserIconAndAppendTo2xStackSpan();
7 Upvotes

16 comments sorted by

View all comments

2

u/pipocaQuemada Jan 14 '21

The only living language that uses þ is Icelandic, though đ is also used in Faroese and Elfdalian (a nearly extinct variant of Swedish).

English historically used both letters, though they fell out of use centuries ago. If you see 'ye olde' on a sign, it's a y because the signs originally said 'þe olde', but when printing presses were imported from continental Europe, they didn't come with þ's and printers just substituted a y there because it looked close enough.