Created
March 13, 2017 21:10
-
-
Save i-am-tom/07d7ff6322fe80115db4a9c43c55c5af to your computer and use it in GitHub Desktop.
Fantas, Eel, and Specification
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 { tagged } = require('daggy') | |
const First = tagged('val') | |
First.prototype.concat = function (that) { | |
return this | |
} | |
const Min = tagged('val') | |
Min.prototype.concat = function (that) { | |
return Min(Math.min(this.val, that.val)) | |
} | |
const Any = tagged('val') | |
Any.prototype.concat = function (that) { | |
return Any(this.val || that.val) | |
} | |
const Tuple4 = tagged('a', 'b', 'c', 'd') | |
Tuple4.prototype.concat = function (that) { | |
return Tuple4(this.a.concat(that.a), | |
this.b.concat(that.b), | |
this.c.concat(that.c), | |
this.d.concat(that.d)) | |
} | |
const Customer = tagged( | |
'name', | |
'favouriteThings', | |
'registrationDate', | |
'hasMadePurchase' | |
) | |
const myStrategy = { | |
to: customer => Tuple4( | |
First(customer.name), | |
customer.favouriteThings, | |
Min(customer.registrationDate), | |
Any(customer.hasMadePurchase) | |
), | |
from: ({ a, b, c, d }) => | |
Customer(a.val, b, c.val, d.val) | |
} | |
const merge = strategy => x => y => | |
strategy.from( | |
strategy.to(x) | |
.concat(strategy.to(y)) | |
) | |
const Tom1 = Customer('Tom', ['socks'], 100000, false) | |
const Tom2 = Customer('TomH', ['gloves'], 90000, true) | |
// { name: 'Tom' | |
// , favouriteThings: ['socks', 'gloves'] | |
// , registrationDate: 90000 | |
// , hasMadePurchase: true | |
// } | |
merge(myStrategy)(Tom1)(Tom2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment