Last active
September 19, 2017 19:21
-
-
Save joeegan/2755ad7151dc3f21950e3ac8fbbff366 to your computer and use it in GitHub Desktop.
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
| // From http://randycoulman.com/blog/2016/06/07/thinking-in-ramda-partial-application/ | |
| const books = [{title: 'foo', year: 1984}, {title: 'bar', year: 1985}] | |
| // original | |
| const publishedInYear = (book, year) => book.year === year | |
| const titlesForYear = (books, year) => { | |
| const selected = filter(book => publishedInYear(book, year), books) | |
| return map(book => book.title, selected) | |
| } | |
| titlesForYear(books, 1985) | |
| // Simplify by returning a function that returns another function | |
| const publishedInYear2 = year => book => book.year === year | |
| const titlesForYear2 = (books, year) => { | |
| const selected = filter(publishedInYear2(year), books) | |
| return map(book => book.title, selected) | |
| } | |
| titlesForYear2(books, 1985) | |
| // Use partial to acheive the above whilst maintaining a nicer signature for publishedInYear | |
| const publishedInYear3 = (book, year) => book.year === year | |
| const titlesForYear3 = (books, year) => { | |
| const selected = filter(partialRight(publishedInYear3, [year]), books) | |
| return map(book => book.title, selected) | |
| } | |
| titlesForYear3(books, 1985) | |
| // Using curry | |
| const publishedInYear4 = curry((year, book) => book.year === year) | |
| const titlesForYear4 = (books, year) => { | |
| const selected = filter(publishedInYear4(year), books) | |
| return map(book => book.title, selected) | |
| } | |
| titlesForYear4(books, 1985) | |
| // Adding a pipeline | |
| const publishedInYear5= curry((year, book) => book.year === year) | |
| const titlesForYear5 = (books, year) => | |
| pipe( | |
| filter(publishedInYear5(year)), | |
| map(book => book.title) | |
| )(books) | |
| titlesForYear5(books, 1985) | |
| // Currying again to keep code in the 'data last' ramda convention | |
| const publishedInYear6 = curry((year, book) => book.year === year) | |
| const titlesForYear6 = curry((year, books) => | |
| pipe( | |
| filter(publishedInYear6(year)), | |
| map(book => book.title) | |
| )(books) | |
| ) | |
| titlesForYear6(1985, books) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment