Created
August 20, 2016 06:03
-
-
Save stefanfrede/3d515e6044c2a84c924608ea028023b0 to your computer and use it in GitHub Desktop.
getWith takes the name of an attribute and returns a function that extracts the value of that attribute from an object.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const getWith = (attr) => (object) => object[attr] | |
| /** | |
| * Examples: | |
| */ | |
| const inventory = { | |
| apples: 0, | |
| oranges: 144, | |
| eggs: 36 | |
| }; | |
| getWith('oranges')(inventory); | |
| //=> 144 | |
| // Combined with mapWith: | |
| // https://gist.github.com/stefanfrede/596597d8b544de08491364eab20053c6 | |
| const inventories = [ | |
| { apples: 0, oranges: 144, eggs: 36 }, | |
| { apples: 240, oranges: 54, eggs: 12 }, | |
| { apples: 24, oranges: 12, eggs: 42 } | |
| ]; | |
| mapWith(getWith('oranges'))(inventories); | |
| //=> [ 144, 54, 12 ] | |
| // Much nicer than writing: | |
| mapWith((inventory) => inventory.oranges)(inventories); | |
| //=> [ 144, 54, 12 ] | |
| // Combined with maybe: | |
| // https://gist.github.com/stefanfrede/3f83f68c756998e43cb39a350a709e2c | |
| mapWith(maybe(getWith('oranges')))(inventories); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment