Skip to content

Instantly share code, notes, and snippets.

@coodoo
Created July 2, 2017 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coodoo/6f66f83e18e8b0573eaa1059088a32e5 to your computer and use it in GitHub Desktop.
Save coodoo/6f66f83e18e8b0573eaa1059088a32e5 to your computer and use it in GitHub Desktop.
Application of Bulk monoid
const Task = require('data.task')
const { List, Map } = require('immutable-ext')
const { Pair, Sum, Intersection } = require('./monoid')
const { findArtist, relatedArtists } = require('./spotify')
const Bulk = (...xs) => ({
xs,
concat: ys => Bulk(...xs.map( (x, idx) => x.concat(ys.xs[idx]) )),
bulkMap: (...fs) => Bulk(...xs.map((x, idx) => fs[idx](x))),
toList: _ => xs,
inspect: () => `Bulk(${ xs && xs.inspect ? xs.inspect() : require( 'util' ).inspect( xs, false, 3, true )})`,
})
const Union = xs =>
({
xs: Array.isArray(xs) ? xs : [xs],
concat: ({xs: ys}) => {
return Union(xs.concat(ys))
},
inspect: () => `Union(${ xs && xs.inspect ? xs.inspect() : require( 'util' ).inspect( xs, false, 3, true )})`,
})
const Unique = xs =>
({
xs: Array.isArray(xs) ? xs : [xs],
concat: ({xs: ys}) => {
const res = [...new Set(xs.concat(ys))]
return Unique(res)
},
inspect: () => `Unique(${ xs && xs.inspect ? xs.inspect() : require( 'util' ).inspect( xs, false, 3, true )})`,
})
const Disjoint = xs =>
({
xs,
concat: ({xs: ys}) => {
xs = Array.isArray(xs) ? xs : [xs]
ys = Array.isArray(ys) ? ys : [ys]
const res1 = xs.filter(x => ys.includes(x) === false) // ok
const res2 = ys.filter(y => xs.includes(y) === false) // ok
return Disjoint(res1.concat(res2))
},
inspect: () => `Disjoint(${ xs && xs.inspect ? xs.inspect() : require( 'util' ).inspect( xs, false, 3, true )})`,
})
/*
- Goal: given an order list, find out following info:
- Order list by name
- Total amount
- Categories of the orders
- Customer with only one order
- Customer with multiple orders
*/
const orderA = { customer:'Slash', amount:10, category:['a', 'b']}
const orderB = { customer:'Satriani', amount:20, category:['b', 'c']}
const orderC = { customer:'Vai', amount:30, category:['a', 'd']}
const orderD = { customer:'Slash', amount:15, category:['e', 'd']}
var t = List([orderA, orderB, orderC, orderD])
.foldMap(p => Bulk(
Union([p.customer]),
Sum(p.amount),
Unique(p.category),
Disjoint(p.customer),
Union([p.customer]),
))
.bulkMap(
val => val.xs.join(', '),
val => val.x,
val => val.xs,
val => val.xs,
val => val.xs.sort().filter((v, idx, arr) => arr[idx+1] === v),
)
.bulkMap(
val => `Customer List: ${val}`,
val => `Total Amount: $${val}`,
val => `Categories: ${val}`,
val => `With only one order: ${val}`,
val => `With multiple orders: ${val}`,
)
.toList()
console.log( t )
// =>
// [ 'Customer List: Slash, Satriani, Vai, Slash',
// 'Total Amount: $75',
// 'Categories: a,b,c,d,e',
// 'With only one order: Satriani,Vai',
// 'With multiple orders: Slash' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment