Giter VIP home page Giter VIP logo

Comments (3)

laurentpayot avatar laurentpayot commented on June 14, 2024

Hi Laurent, if I’m right chaining promises with .then() in JS was already monadic πŸ˜‰

from functional-programming-jargon.

LLyaudet avatar LLyaudet commented on June 14, 2024

Hello Laurent,

Thanks :)
I'm not new to (partly functional - pun intended) JS programming ;).
But I'm new to the concept of Monad in functional programming and it was not immediate for me that the explanation given in this repo applies also to JS Promises.

Monad

A monad is an object with of and chain functions.
chain is like map except it un-nests the resulting nested object.

// Implementation
Array.prototype.chain = function (f) {
  return this.reduce((acc, it) => acc.concat(f(it)), [])
}

// Usage
Array.of('cat,dog', 'fish,bird').chain((a) => a.split(',')) // ['cat', 'dog', 'fish', 'bird']

// Contrast to map
Array.of('cat,dog', 'fish,bird').map((a) => a.split(',')) // [['cat', 'dog'], ['fish', 'bird']]

of is also known as return in other functional languages. chain is also known as flatmap and bind in other languages.

Now I see that new Promise() has the role of of and that indeed .then() acts like chain().
But now I also find it strange that Array was chosen as an example.
Was it because it could illustrate more explicitely the unnesting ?

from functional-programming-jargon.

laurentpayot avatar laurentpayot commented on June 14, 2024

But now I also find it strange that Array was chosen as an example.
Was it because it could illustrate more explicitely the unnesting ?

I don’t know πŸ€” Maybe JS was chosen for this repository because it is a lingua franca that everyone knows enough to understand FP concepts implemented in it, whatever your language background is. In the same way arrays-like structures are implemented in most languages and most people know them, unlike promises.

from functional-programming-jargon.

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.