Gozala / reducers

Library for higher-order manipulation of collections
MIT License
181 stars 8 forks source link

forEach #15

Closed Raynos closed 11 years ago

Raynos commented 11 years ago

https://github.com/Raynos/reflex-todo/blob/master/lib/forEach.js

Gozala commented 11 years ago

I'm not convinced on forEach, at least not for reducers, because of following reasons:

  1. I like simple property of the reducibles, that is any operation except reduce just composes lazy transformation over given input. Only thing that triggers transformation flow is reduce. This makes it very simple to understand and reason.
  2. forEach implies synchronous operation and embraces state changes to things in the outer scope. Later is fine as long as it's just endpoint doing IO (including rendering etc...). But I think it's just too easy to misuse and expect that average js dev will do that.

Note that reduce is different as it encourages accumulative transformations over passed in data, there for there is much less temptation to mutate things that have not explicitly being passed to it. This also make reducer functions more reusable and composable.

Gozala commented 11 years ago

That being said, I do understand where you're coming from, and I'm considering fold which is just like reduce but with argument order inverted, that way fold(sequence, write) will be equivalent of forEeach if write takes only one arg.

Raynos commented 11 years ago

@Gozala pipe triggers transformation flow.

The reason I want forEach is that i'd use it as a reduce when I don't want reduce.

reduce(thing, function (_, value) {
    /* logic */
})

// vs

forEach(thing, function (value) {
    /* logic */
})

I don't mind fold other then i really don't know what the difference between fold and reduce is. I understand the semantics of forEach

Gozala commented 11 years ago

In 2.0 there is fold instead of reduce which also removes a need for forEach.