-
-
Save alandipert/2346460 to your computer and use it in GitHub Desktop.
(extend-type js/RegExp | |
cljs.core/IFn | |
(-invoke ([this s] (re-matches this s)))) | |
(#"foo.*" "foobar") ;=> "foobar" | |
(#"zoo.*" "foobar") ;=> nil | |
(filter #".*foo.*" ["foobar" "goobar" "foobaz"]) ;=> ("foobar" "foobaz") |
This Old Gist™
Despite the fact that this is seekrit I still point it out to clojure journeyfolk because I think it teaches deep Clojure by way of radical ClojureScript in one beautiful motion.
Nowadays, no longer need to hide seekrits. Keywords and symbols are actually types that support IFn
:
https://github.com/clojure/clojurescript/blob/r1.7.228/src/main/cljs/cljs/core.cljs#L2941-L2945
https://github.com/clojure/clojurescript/blob/r1.7.228/src/main/cljs/cljs/core.cljs#L945-L949
@mfikes I can understand keywords, but why are symbols functions of collections? That seems like a juicy one to leave open for users.
Edit
Nevermind, answered my own question. Clojure does the same thing, which must have been the rationale. Fortunately regexes and many other atoms are available for IFn extension 😈
@mjijackson the behavior @fogus mentions is incidental, and is a result of the way
:keywords
are implemented atop JavaScript strings. That said, I would classify this behavior as seekrit and not sweet because it depends on implementation detail.But then, maybe seekrit things are sweet?