Last active
September 5, 2017 20:16
-
-
Save imkrish/7e96efa08b3162783708ff3a0b8af195 to your computer and use it in GitHub Desktop.
04-map-filter-sort-reduce.js
This file contains 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 R = require('ramda') | |
const products = [...] | |
// R.map | |
const productNameProp = R.prop('product_name') | |
const getProductNameList = R.map(productNameProp) | |
const mapResult = getProductNameList(products) | |
// console.log(mapResult) | |
// R.filter | |
const qtyMoreThan = (value) => R.propSatisfies(R.gt(R.__, value), 'quantity') | |
const filterQtyMoreThan = (value) => R.filter(qtyMoreThan(value)) | |
const filterQtyMoreThan250 = filterQtyMoreThan(250) | |
const filterQtyMoreThan300 = filterQtyMoreThan(300) | |
const filterResult = filterQtyMoreThan250(products) | |
// const filterResult = filterQtyMoreThan300(products) | |
// console.log(filterResult) | |
// R.sort | |
const quantityProp = R.prop('quantity') | |
const sort = R.curry((isAscending, prop) => isAscending ? R.sort(R.ascend(prop)) : R.sort(R.descend(prop))) | |
const sortAsc = sort(true) | |
const sortDesc = sort(false) | |
const sortQuantityAsc = sortAsc(quantityProp) | |
const sortQuantityDesc = sortDesc(quantityProp) | |
// const sortResult = sortQuantityAsc(products) | |
const sortResult = sortQuantityDesc(products) | |
// console.log(sortResult) | |
// R.reduce | |
const concatProductName = (names, name) => `${names}${names && '\n'}${name}` | |
const reduceProductName = R.reduce(concatProductName, '') | |
const result = R.pipe( | |
filterQtyMoreThan(100), | |
sortQuantityDesc, | |
getProductNameList, | |
reduceProductName | |
)(products) | |
console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment