Last active
August 29, 2015 14:05
-
-
Save tgriesser/31b530c8b000e0eff652 to your computer and use it in GitHub Desktop.
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
// An object which implements the | |
// IAssociative, ILookup, ICloneable protocols: | |
function MapCursor(data) { | |
this.data = mori.is_map(data) ? data : mori.js_to_clj(data) | |
} | |
MapCursor.prototype = { | |
clone: function() { return new MapCursor(this.data); }, | |
has: function(key) { return mori.has_key(this.data, key); | |
get: function(key, fallback) { | |
var val = mori.get(this.data, key, fallback); | |
if (mori.is_map(val)) { | |
return new MapCursor(val); | |
} | |
return val; | |
}, | |
set: function(k, v) { | |
this.data = mori.assoc(this.data, k, v) | |
return this | |
}, | |
}; | |
// Define the protocol functions and the associated | |
// functions in JS | |
mori.protocol.IAssociative.implement( | |
MapCursor.prototype, | |
'-contains-key?', 'has', | |
'-assoc', 'set' | |
) | |
mori.protocol.ICloneable.implement( | |
MapCursor.prototype, | |
'-clone', 'clone' | |
) | |
mori.protocol.ILookup.implement( | |
MapCursor.prototype, | |
'-lookup', 'get' | |
) | |
// Then this should work: | |
var cursor = new MapCursor({name: {first: 'Tim', last: 'G'}}); | |
mori.get(cursor, 'name'); | |
mori.get_in(cursor, ['name', 'first']); | |
mori.assoc(cursor, 'key', 'value'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment