-
-
Save richdouglasevans/e89b1798820ada6480b6f439d5aca5f2 to your computer and use it in GitHub Desktop.
Fantas, Eel, and Specification
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
import { tagged } from "daggy"; | |
const First = tagged("First", ["val"]); | |
First.prototype.concat = function(that) { | |
return this; | |
}; | |
const Min = tagged("Min", ["val"]); | |
Min.prototype.concat = function(that) { | |
return Min(Math.min(this.val, that.val)); | |
}; | |
const Any = tagged("Any", ["val"]); | |
Any.prototype.concat = function(that) { | |
return Any(this.val || that.val); | |
}; | |
const Tuple4 = tagged("Tuple4", ["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("Customer", [ | |
"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