Skip to content

Instantly share code, notes, and snippets.

@Restuta
Created August 13, 2020 06:32
Show Gist options
  • Save Restuta/b1cd2923f0d5c03b8996271bcb0c1737 to your computer and use it in GitHub Desktop.
Save Restuta/b1cd2923f0d5c03b8996271bcb0c1737 to your computer and use it in GitHub Desktop.
Tranducers and mergeWith experiments
const R = require('ramda')
const numbers = [1, 2, 8, 9]
const transducer = R.compose(
R.filter(x => {
console.log('filter', x)
return x > 3
}),
R.map(x => {
console.log('map', x)
return x + 100
}),
// v => {
// console.log('map', v)
// return R.map(x => x + 100, v)
// },
// v => {
// console.log('take', v)
// return R.take(2, v)
// },
// R.tap(x => console.log('transducer', x)),
// x => {
// console.log('transducer', x)
// return x
// }
)
// R.transduce(
// transducer,
// (acc, item) => {
// console.log('iterator', item)
// return acc.concat(item)
// },
// [],
// numbers,
// ) //?
//
// // const sumReducer = (acc, item) => acc + item
// const multiplyAll = R.reduce((acc, item) => acc * item, 1)
// const sum = R.reduce((acc, item) => acc + item, 0)
// sum([1,2,3])//?
// const reduceResults = (acc, item) => acc.concat(item)
// const foo = R.map(multiplyAll, [ [1], [2, 2], [3, 3] ]) //?
// const totalSum = multiplyAll(foo) //?
const mapFunc = x => x
const sources = [
{
'hector-chauran': { name: 'hector-chauran', count: 2 },
ReaLoretta: { name: 'ReaLoretta', count: 1 },
Restuta: { name: 'Restuta', count: 4 },
},
{
Restuta: { name: 'Restuta', count: 4 },
GinkgoBun: { name: 'GinkgoBun', count: 1 },
JujubeBun: { name: 'JujubeBun', count: 1 },
},
// {
// Restuta: { name: "Restuta", count: 4 },
// GinkgoBun: { name: "GinkgoBun", count: 1 },
// JujubeBun: { name: "JujubeBun", count: 1 },
// },
]
// const actual = R.reduce(
// R.mergeDeepWithKey((k, a, b) => (k === 'count' ? a + b : a)),
// {},
// sources,
// )
//
const actual = R.reduce(R.mergeWith((a, b) => ({
name: a.name,
count: a.count + b.count,
})), {}, sources)
// const actual = R.reduce(
// R.mergeDeepWith((a, b) => (Number.isInteger(a) ? a + b : a)),
// {},
// sources,
// )
//
// const actual = R.reduce((acc, item) => {
//
// }, {}, sources)
// actual //?
// expected output
const expected = {
'hector-chauran': { name: 'hector-chauran', count: 2 },
ReaLoretta: { name: 'ReaLoretta', count: 1 },
Restuta: { name: 'Restuta', count: 8 },
GinkgoBun: { name: 'GinkgoBun', count: 1 },
JujubeBun: { name: 'JujubeBun', count: 1 },
}
R.equals(expected, actual) //?
// R.transduce(
// // multiplyAll,
// R.map(x => sum(x)),
// // x => {
// // return mapFunc(x)
// // },
// // (acc, item) => {
// // console.log('iterator', item)
// // return acc.concat(item)
// // },
// (acc, item) => acc + item,
// 0,
// [ [1], [2, 2], [3, 3] ],
// ) //?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment