Skip to content

Instantly share code, notes, and snippets.

@tgriesser
Last active August 29, 2015 14:05
Show Gist options
  • Save tgriesser/31b530c8b000e0eff652 to your computer and use it in GitHub Desktop.
Save tgriesser/31b530c8b000e0eff652 to your computer and use it in GitHub Desktop.
// 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