Skip to content

Instantly share code, notes, and snippets.

@swannodette
Created September 13, 2011 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save swannodette/1214547 to your computer and use it in GitHub Desktop.
Save swannodette/1214547 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