getify / Functional-Light-JS

Pragmatic, balanced FP in JavaScript. @FLJSBook on twitter.
http://FLJSBook.com
Other
16.6k stars 1.96k forks source link

Encourage use of reduce #8

Closed JAForbes closed 8 years ago

JAForbes commented 8 years ago

Hi, really great work :)

I am so excited to have a new voice / perspective in the FP community.

When teaching how to apply FP in applications I find reduce is indispensable. Elm uses foldp to convert streams into DOM snapshots and apply them to the page purely. React and Redux are all about reducers. And then most of the utilities we use in lodash or ramda can be implemented in terms of reduce e.g. (filter, takeWhile, compact, map, compose, etc)

I was just reading ch4 and noticed the variadic compose was using a while loop and popping an array. Which is great from a performance perspective. But for learning FP I thought it might be nice to implement compose using reduce to illustrate how simple compose really is.

function compose(...fns){
   return fns.reduce(compose2)
}

It may complicate your arc, but I thought I should at least mention it.

I remember reading your transducers gist (which was great), so perhaps you are covering reduce in some later chapters (or maybe in a later book). But I think if you cover reduce early you could make use of it again and again throughout the book.

E.g. in the point free section we could redefine compose as

// unapply just turns this into a fn that accepts variadic args instead of an array
const compose = unapply(reduce(compose2))

reduce allows us to create variadic functions using binary signatures so it also could be relevant in discussions of arity.

So, reduce is useful, powerful, practical and elegant. But I know you need to pick your battles when teaching and reduce might complicate matters. So this is merely a vote of support for reduce. And keep doing what you are doing it is exactly what FP and JS needs.

dtipson commented 8 years ago

+1 to this sentiment.

One thing that held me up as a programmer for a long time was not understanding the larger, generic form of reduce. For many self-taught, non-CS/non-FP JS programers, Array is the only real "type" we know, and we think of operations like map and filter and reduce specifically, too specifically, in terms of how they work with Arrays. But the more I get into FP and Types, the more I realize how powerful reduce is, and how many other operations on those types can be expressed in terms of it.

That said, compose is such a central operation to FP that a walkthrough/think-through of the multiple different ways to implement/refactor compose could be instructive in and of itself. In addition to the reduce style there's also the recursive style, which is also fascinating.

Super exciting to watch this book come together!

getify commented 8 years ago

The plan is for an entire chapter (dunno which one yet) to cover list operations map(..), filter(..), and reduce(..). And in that chapter, I'll also point out how map(..) and filter(..) can be reimplemented with reduce(..). In fact, the reduce(..) section will probably have a lot of examples of usage in it, including perhaps alternate implementations of various other operators that we've derived earlier in the book.

The challenge of chapter ordering is that I want list operations to come after side-effects, and that one needs to come after composition, and that one needs to come after .... This is always a problem. But I make careful forward-refs to things when I need to. So far I don't think I need to mention reduce(..) early, but I'll keep an eye on it as the next few chapters roll out.