According to docs dir lists only "public" vars.
Indeed, it does not show itself, and may not be available in current namespace:
user=> (dir user)
nil
user=>
user=>
user=> (in-ns 'foo)
#<Namespace foo>
foo=>
foo=>
foo=> (dir foo)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: dir in this context, compiling:(NO_SOURCE_PATH:17)
What other (non-public) types of names/vars exist? (These are probably Java types?) How to list these non-public names? How are they imported into the namespace (or into some "default case" namespaces) on the startup? How default namespaces are searched through in runtime?
For instance in Python: dir() lists everything in current namespace, if a referenced variable is not found it is checked among dir(__builtins__).
This question was already answered here. You could list all public vars in a namespace using:
(keys (ns-publics 'foo))
There also is a dir function like python does, so you could do:
(clojure.repl/dir foo)
EDIT
If you really want to see the privates as well, you could do:
(defn ns-all-vars[n]
(filter (fn[[_ v]]
(and (instance? clojure.lang.Var v)
(= n (.getName (.ns v)))))
(ns-map n)))
and then simply call
(println (map first (ns-all-vars 'foo)))
To print all symbols in 'foo namespace.
Related
If I've been playing around in a clojure REPL (might have been experimenting with (assoc) or (seq) or (reduce) and created some temporary maps for examples), but wanted to print a listing of variables defined in that session, is there a way to do this? Something like (println def) or something?
You can use (dir user) if you are in the user namespace. Or any other ns you are interested into. This shows you all the def-ed things. If it's not there (it usually gets useed automatically in the REPL, you can find it in clojure.repl/dir).
ns-interns can get you pretty close I think.
user=> (def x 1)
#'user/x
user=> (def y 2)
#'user/y
user=> (ns-interns *ns*)
{x #'user/x, y #'user/y}
I've been trying for a while to solve this nREPL ticket and I'm out of ideas, so I decided to ask for a bit of help here.
Basically we need to be bind *print-namespace-maps* only if it's present or find some way to define if it's not present that doesn't mess up Clojure 1.9+.
Here are some of the things that don't work:
;; compilation error
(when (resolve '*print-namespace-maps*)
(set! *print-namespace-maps* (#bindings #'*print-namespace-maps*)))
;; shadows `clojure.core/*print-namespace-maps*` on 1.9, as the def gets executed always
(when-not (resolve 'clojure.core/*print-namespace-maps*)
(def ^:dynamic *print-namespace-maps*
"Define the var for clojure versions below 1.9"
nil))
Seems that dynamic vars can't be conditionally bound at all, which really sucks, but I guess there must be some way to do something like what we're aiming for.
Take a look at the linked issue for more details. Any help would be much appreciated!
I don't totally understand the details of the linked issue or nREPL impl., but the def behavior is a bit surprising here:
Clojure 1.8.0
user=> (when false (def ^:dynamic *foo* 1))
nil
I would've expected there to be no *foo* var after that evaluation, but it creates an unbound var:
user=> (var-get #'*foo*)
#object[clojure.lang.Var$Unbound 0x20da8800 "Unbound: #'user/*foo*"]
And I suppose that def compiler behavior is what causes the exception in 1.9 in your example:
WARNING: *print-namespace-maps* already refers to: #'clojure.core/*print-namespace-maps* in namespace: user, being replaced by: #'user/*print-namespace-maps*
Maybe a macro could be used so that the resolve check happens during macro expansion, and doesn't emit a def if it resolves:
(defmacro def-dynamic-when-not-resolve [sym value]
(when-not (resolve sym)
`(def ~(vary-meta sym assoc :dynamic true) ~value)))
It seems to work on Clojure 1.8:
Clojure 1.8.0
user=> (def-dynamic-when-not-resolve *print-namespace-maps* 'sure)
#'user/*print-namespace-maps*
user=> *print-namespace-maps*
sure
And on Clojure 1.9:
Clojure 1.9.0
user=> (def-dynamic-when-not-resolve *print-namespace-maps* 'sure)
nil
user=> *print-namespace-maps*
true
So I think clojure.core/bean is pretty close to what I want, but I'm working with a Java application that has nested beans, such that I end up with maps like this:
{:month-total 3835.0 :name "Jan's Meat Diner" :owners #<BarOwner[] [Lcom.fancypants.BarOwner;#1fb332d}
How, do I call bean recursively on a Java object so that I can get my imaginary BarOwner object to emit itself as a map, too:
{:month-total 3835.0 :name "Jan's Meat Diner" :owners { [:name "Jack"] [:name "Jill"] } }
Edit 1
I have found that clojure/java.data and from-java is probably a better fit for this kind of thing than bean.
Although it's probably not an ideal answer to "how to use bean recursively", using more of the richer contrib libraries under the Clojure community's site did solve it. Specifically
clojure/java.data
provides simple recursive bean resolving, and can be configured to handle java types specifically in the hairy cases. I'd recommend this to other people who want to use bean.
It is very tricky to find out what is a bean and what is not. This seems to do the trick for beans inside beans and properties that are lists. Probably you will want to add more classes to the probably-bean? function and perhaps some support for properties that are maps.
(defn probably-bean? [o]
(and (not (coll? o))
((complement #{Class Long String clojure.lang.Keyword}) (class o))))
(defn transf [o]
(cond
(instance? java.util.List o) (into [] (map transf o))
(probably-bean? o) (into {} (seq (bean o)))
:else o))
(defn to-bean [o]
(clojure.walk/prewalk #(transf %) o))
I'm developing a web application with Clojure, currently with Ring, Moustache, Sandbar and Hiccup. I have a resource named job, and a route to show a particular step in a multi-step form for a particular job defined like this (other routes omitted for simplicity):
(def web-app
(moustache/app
;; matches things like "/job/32/foo/bar"
:get [["job" id & step]
(fn [req] (web.controllers.job/show-job id step))]))
In the view my controller renders, there are links to other steps within the same job. At the moment, these urls are constructed by hand, e.g. (str "/job/" id step). I don't like that hard-coded "/job/" part of the url, because it repeats what I defined in the moustache route; if I change the route I need to change my controller, which is a tighter coupling than I care for.
I know that Rails' routing system has methods to generate urls from parameters, and I wish I had similar functionality, i.e. I wish I had a function url-for that I could call like this:
(url-for :job 32 "foo" "bar")
; => "/job/32/foo/bar"
Is there a Clojure web framework that makes this easy? If not, what are your thoughts on how this could be implemented?
Noir provides something similar. It's even called url-for.
The example function you have mentioned could be implemented as below. But I am not sure if this is exactly what you are looking for.
(defn url-for [& rest]
(reduce
#(str %1 "/" %2) "" (map #(if (keyword? %1) (name %1) (str %1)) rest)))
I am trying to understand how Clojure hierarchies work, with the derive and is-a? constructs. I'm not sure how I would use these with the maps and records in my program. Has anyone used these?
I find your question a bit vague. Have you read the documentation on the Clojure website?
http://clojure.org/multimethods
I find the examples there quite easy to follow:
user=> ::rect
:user/rect
user=> (derive ::rect ::shape)
nil
user=> (parents ::rect)
#{:user/shape}
user=> (derive ::square ::rect)
nil
user=> (ancestors ::square)
#{:user/shape :user/rect}
user=> (isa? ::square ::shape)
true
There's also this blog post with a more "real-world" example:
http://www.prodevtips.com/2010/06/20/clojure-inheritance/