Skip to content

Instantly share code, notes, and snippets.

@jhickner
Forked from swannodette/gist:1214547
Created September 15, 2011 00:55
Show Gist options
  • Save jhickner/1218258 to your computer and use it in GitHub Desktop.
Save jhickner/1218258 to your computer and use it in GitHub Desktop.
closure_and_proto.clj
/*
Silly example - close over s (say a string) returning an instance
of an anonymous type that acts like a ClojureScript collection
(defn make-conjable [s]
(reify
ICollection
(-conj [_ c] (str s c))))
*/
function make_conjable(s) {
// check if the anonymous type is already memoized
if (cljs.core.truth_(cljs.core.undefined_QMARK_.call(null, cljs.user.t4240))) {
// define type
cljs.user.t4240 = (function (s, make_conjable) {
this.s = s;
this.make_conjable = make_conjable;
});
cljs.user.t4240.prototype.cljs$core$ICollection$ = true;
cljs.user.t4240.prototype.cljs$core$ICollection$_conj = (function (_, c) {
var this__4241 = this;
return cljs.core.str.call(null, this__4241.s, c);
});
} else {
}
// instantiate w/ closed over locals
return (new cljs.user.t4240(s, make_conjable));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment