Skip to content

Instantly share code, notes, and snippets.

@supasympa
Last active March 11, 2019 21:19
Show Gist options
  • Save supasympa/65026f1d00025efa4e3207f8a95a8026 to your computer and use it in GitHub Desktop.
Save supasympa/65026f1d00025efa4e3207f8a95a8026 to your computer and use it in GitHub Desktop.
functional programming functors example
const Id = <T extends {}>(v: T) => {
return {
valueOf: () => v,
map: (fn:any) => Id(fn(v))
}
};
console.log(
Id('foo')
.map(
(v) => v.split('').reverse().join('')
)
.valueOf()
);
console.log(
Id(10)
.map((n:number) => n+10)
.map((n:number) => n -3)
.valueOf()
);
console.log(
Id({ name: 'Tim Jones', age: 45 })
.map((p:any) => ({ ...p , ...{ statement: `${p.name} is ${p.age} years old` } }) )
.valueOf()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment