admin管理员组文章数量:1297068
I am trying to wrap my head around pure functions, but I am not sure that I really understand it. I know that pure functions shouldn't mutate external state, and it should return the same output every time as long as it has the same input.
I know that for example this function is impure, because it mutates the cart variable which other parts of the program may use:
const addToCart = (cart, item) => {
cart.push(item);
return cart;
};
The same function in a pure state:
const addToCart = (cart, item) => {
const newCart = lodash.cloneDeep(cart);
newCart.push(item);
return newCart;
};
This makes sense to me. I have learned that pure functions should always return something.
However, I am working on some stuff that requires me to use the HTML5 canvas element, and I have this function which clears the canvas:
function clearCanvas(canvas) {
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
}
How can I make the above function pure? I realize it's impure because it doesn't return anything, also it mutates the state of the canvas variable. Is DOM-manipulation inherently impure?
Any help would be appreciated :)
I am trying to wrap my head around pure functions, but I am not sure that I really understand it. I know that pure functions shouldn't mutate external state, and it should return the same output every time as long as it has the same input.
I know that for example this function is impure, because it mutates the cart variable which other parts of the program may use:
const addToCart = (cart, item) => {
cart.push(item);
return cart;
};
The same function in a pure state:
const addToCart = (cart, item) => {
const newCart = lodash.cloneDeep(cart);
newCart.push(item);
return newCart;
};
This makes sense to me. I have learned that pure functions should always return something.
However, I am working on some stuff that requires me to use the HTML5 canvas element, and I have this function which clears the canvas:
function clearCanvas(canvas) {
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
}
How can I make the above function pure? I realize it's impure because it doesn't return anything, also it mutates the state of the canvas variable. Is DOM-manipulation inherently impure?
Any help would be appreciated :)
Share Improve this question asked May 22, 2017 at 9:27 Olav GundersenOlav Gundersen 15310 bronze badges3 Answers
Reset to default 5DOM-manipulation is impure. You cannot clear the canvas in a "pure" way.
Anything which changes the system's state or interacts with the outside world is said to have side effects, and in a purely-functional programming environment side effects are to be avoided.
However, DOM-manipulation clearly does both of these; clearing the canvas is both a change of state (uncleared to cleared) and the user can see this change.
You may want to have a deeper look into functional programming, which seems to be what you're trying to achieve with your pure/impure function approach.
The object of functional programming is to avoid changing state, so that, for example, one program doesn't change an object while another program is working with it, which can have unexpected and unwanted results.
the IO monad
You might be interested in the IO monad – essentially IO contains a thunk, or a lazy function, which only runs when we call runIO
. More importantly tho, we can keep things bottled up in IO and map
an ordinary function that allows us to operate on the contained value.
For a good read and another IO implementation, see Chapter 9: Monadic Onions from Brian Lonsdorf's book
little demo
// IO :: (void -> a) -> IO a
const IO = f => ({
// runIO :: void -> a
runIO: f,
// map :: IO a => (a -> b) -> IO b
map: g =>
IO(() => g(f())),
// chain :: IO a => (a -> IO b) -> IO b
chain: g =>
IO(g(f()).runIO)
})
// IO.of :: a -> IO a
IO.of = x => IO(() => x)
// log :: String -> a -> IO a
const log = label => x =>
IO(() => (console.log(label, x), x))
// $ :: String -> IO HTMLElement
const $ = x =>
IO(() => document.querySelector(x))
// getText :: HTMLElement -> String
const getText = e =>
e.textContent
// main :: String -> IO String
const main = selector =>
$(selector)
.map(getText)
.chain(log('A'))
.map(s => s.toUpperCase())
.chain(log('B'))
.runIO()
main('title')
// A hello world
// B HELLO WORLD
<title>hello world</title>
To do it via the function programming paradigm
function clearCanvas(canvas) {
const imgData = canvas.getContext('2d').getImageData(0,0,canvas.width,canvas.height);
new Uint32Array(imgData.data.buffer).set(0);
return data;
}
canvas.getContext('2d').setImageData(clearCanvas(canvas),0,0);
No side effects and you return the same data for the same arguments.
But functional programming is a defensive style that works on the assumption that code is dangerous and that the dangers must be isolated. It is thus very resource hungry and slow and not at all suited to performant code.
本文标签: javascriptPure functions when working with DOM manipulationStack Overflow
版权声明:本文标题:javascript - Pure functions when working with DOM manipulation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741640958a2389914.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论