How is most "pure" way to manipulate DOM in script written in "functional" way.
For example if I simply need to change element width should I use typical syntax like:
document.querySelector(".class").style.width = ...
or write dedicated function, like:
function resize(el, w) {
return el.style.width = w;
}
resize(document.querySelector(".class", 100));
I think, I understand general idea of functional programming but all examples I encountered are focused on manipulating numbers or strings. Manipulating DOM is considered side-effect so I'm wondering how to do it in correct way.
EDIT:
Updated code below, the question is, is iit functionall? If not I'd be gratefull for suggestion how to rewrite it.
"use strict";
const outerW = num => 100 * num + "%";
const innerW = num => 100 / num + "%";
const setW = (el, w) => el.style.width = w;
const mask = document.querySelector(".slider__slides__mask");
const slides = document.querySelectorAll(".slider__slide");
setW(mask, outerW(slides.length));
slides.forEach(function(item) {
setW(item, innerW(slides.length));
});
Every meaningful program must eventually carry out some effects, whether it is in functional or imperative style. The idea of functional programming is to shift theses effects to the edge of a program, so that a large portion remains pure. This makes it much easier to reason about it.
But how can this be put to practice?
Make functions composable
Javascript functions are usually defined as multi-argument functions. Hence we can't defer their evaluation at the calling side:
It's the same with the imperative style:
The consequence is that the pure computation (
add
) can not be separated from the effect (log
). This isn't a big deal for this sketch, but as soon as your program grows bigger, these scattered effects impair its readability and comprehensibility.To prevent this behavior functions must become composable, i.e. their last argument must be partially applicable:
You might want to look into currying for more information.
Wrap effects in thunks
Using thunks we can move the effects even further to the edges. A thunk is just a function that expects no arguments and thus represents a deferred computation:
You might want to look into IO monads for more information.
An (almost) real world example
As a side effect (pun intended)
reads like plain English. I was recently told that this is not a desirable property. Well, I disagree.