Skip to content

Instantly share code, notes, and snippets.

@moatorres
Last active June 27, 2021 13:50
Show Gist options
  • Save moatorres/afbcfe91ebc619eb5dfd4ea4c1847fc5 to your computer and use it in GitHub Desktop.
Save moatorres/afbcfe91ebc619eb5dfd4ea4c1847fc5 to your computer and use it in GitHub Desktop.
Identity type in JS/Node (without classes)
import { Identity } from './Identity'
let res = Identity(3)
.map((v) => v * 3)
.map((v) => v * 3)
.map((v) => v * 33)
.chain(Identity)
console.log(res.map((v) => v - 1).inspect()) // => 'Identity(890)'
export const Identity = (x) => ({
chain: (f) => f(x),
unwrap: () => x,
map: (f) => Identity(f(x)),
inspect: () => `Identity(${x})`,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment