Giter VIP home page Giter VIP logo

Comments (5)

buzzdecafe avatar buzzdecafe commented on June 7, 2024

i disagree. First, we don't have a fold, we have foldl and foldr. foldl folds the list from the left with the provided accumulator, as opposed to foldr which folds from the right. foldl is the same behavior as reduce. foldl without an initial accumulator, i.e. using the head of the list, is sometimes called foldl1, and it's my POV that it should not be in the library, because of the problem of deciding what the behavior is when you call foldl1 with an empty list. Since it is so easy to implement foldl1, I argue it is best to leave its problems out of the library.

from ramda.

wavebeem avatar wavebeem commented on June 7, 2024

I wasn't aware that "fold" was something other than assumed shorthand for "foldl".

It seems to me like reduce generally can work without an initial value, whereas fold always requires it.

I would expect a JavaScript library to throw an exception if it was asked to do something invalid like that, but I suppose it's possible to expect null or perhaps something else entirely, and can understand requiring the library user to define it themselves if they want it.

from ramda.

buzzdecafe avatar buzzdecafe commented on June 7, 2024

I would expect a JavaScript library to throw an exception if it was asked to do something invalid like that,

exactly--and since it is so easy to wind up in a situation where you pass an empty array to foldl1 inadvertently and then the app goes boom. or worse, you wrap it in a try-catch.

Returning undefined or null is not good either. The point is to compose these functions; which means we want functions that have--as much as possible in a JS environment--clearly defined inputs and outputs. If foldl1 is in some composition of functions that winds up passing an empty list, and it returns null, then the consuming function goes boom, or has to type-check its arguments.

That's why I think foldl1 is a non-starter in this environment.

from ramda.

buzzdecafe avatar buzzdecafe commented on June 7, 2024

we covered this before: https://github.com/CrossEye/ramda/issues/20

at that time we opted to remove foldl1 and foldr1--somehow they snuck back in. I've pulled them from the lib. Thanks for the input.

from ramda.

CrossEye avatar CrossEye commented on June 7, 2024

For further reference, there is a great list of how various languages handle this in Wikipedia's Fold Entry

There are a few that do it as Brian is suggesting, including some important ones like Scheme and Scala. There is really only Haskell that uses the foldl/foldl1 distinction. But the overwhelming majority use some sort of overloading to reuse the same name for the two scenarios.

We can't really do that in Ramda with our focus on curried functions. There's no good way to combine those notions with optional parameters, and that's the only version of overloading available in Javascript.

from ramda.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.