DEV Community

Discussion on: Function vs Object

 
stereobooster profile image
stereobooster • Edited

Closure without mutation or re-assignment ("pure"):

const addSome = (x) => (y) => x + y;
const addFive = addSome(5);
addFive(1) === addFive(1) 

Closure with re-assignment += ("not pure"):

const counter = (x) => (y) => x += y;
const countFromZero = counter(0);
countFromZero(1) !== countFromZero(1)

My point is that there is nothing wrong with closures. It is re-assignment and mutation which make closures (or functions) "not pure".

Thread Thread
 
codemouse92 profile image
Jason C. McDonald

Yup, makes sense!

Thread Thread
 
bootcode profile image
Robin Palotai • Edited

Re state: in pure FP I think we mean a "named" piece of data whose instances get changed over time:

go :: Int -> Int
go 0 = 0
go s = s + go (s-1)

Here s can be thought of as state, even though specific bindings of s don't change.

We observe state over the course of computation.