-
Notifications
You must be signed in to change notification settings - Fork 0
1. Identity
Carlos De Dios edited this page Oct 21, 2015
·
3 revisions
Function whose only purpose is to return the same provided value.
In the Identity module, there are 2 functions, identity and id, they're the exact same id is simply a shorter version.
/* Regular Approach*/
let v = getRandomValue();
let oddAction = (n) => n * 2;
return v % 2 == 0 ? v : oddAction(v);
/* Identity Approach */
import { identity } from "functional-programming-utilities";
let v = getRandomValue();
let oddAction = (n) => n * 2;
return (v % 2 === 0 ? identity : oddAction)(v);This isn't the best example, but there's probably no best example, since Identity simply returns whatever you throw at him.