Unable to locate the source of a Clojure function
I'm not a Clojure programmer, but I've been tasked to rewrite a core piece of our architecture from it's current Clojure into Node. If you love Clojure and you're wondering why, we can't get Clojure developers anywhere, and JavaScript is pretty universal (at least the being able to read it part). The component itself is small and simple enough, but the lack of Clojure expertise is really holding us back at the moment.
I understand the majority of the code, but I've encountered a function named <fn
which I can't find online (possibly because it's hard to search the special character), in the Clojure docs, or any defn
within our code.
Here it is in a bit of context
(comp (<fn plan/enqueue nil handle-internal-event)
(<fn doto mt/confirm!)
sanitize-object)
Any clues on what this would be doing? Is it a simple expression I've overlooked, or is it likely some sort of lisp macro thing?
Answered
Turns out it was a macro that was required at the top of my file.
(:require [fletching.macros :refer :all])
For anyone interested in the source code of this function it is at https://github.com/jdevuyst/fletching-macros
The way to track things down like this, goes like this (assuming, this is a regular source file and we are talking regular Clojure here).
So if your editor or IDE does not help, but you have that code loaded in a REPL, you can do (source <fn)
.
If the function/macro in question has a namespace prefixed (eg tools*/<fn
), there is a require
for it (eg (:require [my.ns.tools :as tools*])
).
If not:
Make sure, the function is not defined in the same file beforehand.
Make sure, it is no function from clojure.core
. Eg in the cheatsheet
Search for things with require
and use
, that look like this (:require [my.ns.tools :refer :all])
, (:require [my.ns.tools :refer [<fn])
, (:use [my.ns.tools])
If all of this does not help, then there is a chance, that this function got created for you by something else. Examples for that category are the constructor functions for records (eg ->A
and map->A
for the record A
). This is an example where a macro is used to create top level functions.
Honourable mention (but not useful in this case): SymbolHound
Although your question has already been answered, there is a nice hint. Every time you see a function call without a namespace (say, just (foo)
but not (namespace/foo)
), it means the function was injected here in one of three ways:
:use
in ns
macro, eg (:use some.package)
; :require
in ns
with explicit refer
: (:require [some.package :refer [foo]])
:require
referring everything inside a package: (:require [some.package :refer :all])
So you should examine the ns
macro placed in the top of file. But the better approach would be to use either emacs + cider and pressing M-.
to go to symbol definition or at least some IDE with the same feature.
上一篇: paramiko关闭SSH上的一个非连接
下一篇: 无法找到Clojure函数的来源