Created
June 12, 2014 04:16
-
-
Save remydagostino/c8da9bfac7fbbcafb219 to your computer and use it in GitHub Desktop.
Idea for pure objects in javascript
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
// PureObj is an object wrapper for fantasy land | |
// which is pure, every change is cloned into | |
// a new object | |
// - Semigroup (concat) | |
// - monoid (empty) | |
// - functor (map) | |
// When a pure obj is created, the original values are cloned | |
// deeply into a new object | |
var myObj = PureObj({ a: 'hello' }); | |
// Pointfree console log | |
var log = console.log.bind(console.log) | |
// Map unwraps the pure, it does this by creating a new object | |
// which is the original object with the list of applied changes | |
myObj.map(log); | |
// >> { a: 'hello' } | |
// Empty is an empty object wrapped in a PureObj | |
PureObj.empty().map(log); | |
// >> { } | |
// Internally, every time you modify the PureObj it avoids cloning | |
// the object in favor of keeping a changeset | |
PureObj({ a: 20, b: 'hello' }).concat({ b: 'goodbye' }).map(log); | |
// >> { a: 20, b: 'goodbye' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment