this post was submitted on 02 Jul 2023
191 points (94.4% liked)

Programmer Humor

19594 readers
901 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments

Maybe I'm not understanding it correctly, but Monads are data-structure objects whose methods return an data-structure object of the same type.

Like, (using Typescript):


interface IdentityMonad<T> {
  map: ((fn: (v: T)) => T) => IdentityMonad<T>;
  value: T
}

const Identity = <T>(value: T) => {
  const map = (fn) => Identity(fn(initialValue));
  return {
    map, value
  }
}

const square = (x) => x * x;

const twoId = Identity<number>(2);
console.log(twoId.value) //=> 2;
const sixtyFourId = twoId.map(square).map(square).map(square).map(square).map(square);
console.log(sixtyFourId.value) // => 64;