Functor's flavor in JS

Marcos Henrique - Dec 9 '20 - - Dev Community

There's no secret it's just mystery, straight to the point without hysteria ✨🔮

Basically it is a wrapper of a value, an object that encapsulates this value and implements the map () function.

An example as popular as Rock n Roll or Ronaldinho Gaúcho is the Array in JS, where it is a container (object) that contains a list of values or a dimensional and we have the map function that applies processing to these internal values, so much so that you launch a typeof [🦇, 🐙] will have an object as answer.

Creating your own functor

const secureType = value => ({
  value,
  map(fn) { 
    return secureType(fn(this.value))
  }
})

//using
const { value } = secureType('who\'s bad?')
  .map(text => text.toUpperCase())
  .map(text => `${text} 👹`)

console.log(value)
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player