NX2@feddit.de to Programmer Humor@programming.dev · 1 year agoWhat the f*ck is a monadfeddit.deimagemessage-square37fedilinkarrow-up188arrow-down16
arrow-up182arrow-down1imageWhat the f*ck is a monadfeddit.deNX2@feddit.de to Programmer Humor@programming.dev · 1 year agomessage-square37fedilink
minus-squareKerryAnnCoder@lemmy.blahaj.zonelinkfedilinkarrow-up2·1 year agoMaybe 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;
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;