This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-12-29
Channels
- # adventofcode (11)
- # beginners (98)
- # boot (2)
- # cider (3)
- # clara (3)
- # cljs-dev (15)
- # cljsrn (2)
- # clojure (122)
- # clojure-austin (4)
- # clojure-russia (1)
- # clojure-spec (2)
- # clojurescript (30)
- # css (6)
- # cursive (5)
- # docs (1)
- # fulcro (4)
- # hoplon (118)
- # keechma (3)
- # leiningen (5)
- # lumo (1)
- # off-topic (28)
- # onyx (6)
- # re-frame (4)
- # reagent (1)
- # reitit (1)
- # shadow-cljs (53)
- # sql (1)
- # unrepl (1)
Hey guys. I'm just starting out with clojure and reagent, was wondering why luminus does this
(defn mount-components []
(r/render [#'navigation/navbar] (.getElementById js/document "navbar"))
(r/render [#'router/current-page] (.getElementById js/document "app")))
;; --------^ why hash quote? it works fine without it too
that's probably so they reference the var rather than the function itself...so when they reload the file containing router/current-page the new value of the var is taken into account
router/current page is a multimethod. Anyways, it's not a big deal right now, was just curious. I'll read up on vars at some point
clojurescript has slightly different semantic for vars, not quite sure that's appropriate or not
simple reagent component.
(defn navbar []
(fn []
[:nav.navbar.navbar-dark.bg-primary
...
try this : remove the #' for the navbar code ... load that file ...then modify your navbar and load that file
for clojure code that's quite necessary during development. I'm not sure about clojurescript
a simple test, in ns a have : (def a b/b) , in ns b have : (def b 1) ... if you change b and reload the ns b, a should still be 1, unless you set b dynamic and change a to (def a #'b/b)
I’m not bitter either, I’m having too much fun writing clojure 😉
could anyone explain the rationale for the following?
(def foo {:a 1})
(when-let [a (:a foo)] (println a))
;; => prints "1", returns nil
(when-let [{:keys [a]} foo] (println a))
;; => prints "1", returns nil
(when-let [a (:a (assoc foo :a nil))] (println a))
;; => returns nil
(when-let [{:keys [a]} (assoc foo :a nil)] (println a))
;; => prints "nil", returns nil
i would like for the final expression to not print “nil”, ie the destructuring bind on a nil key to not evaluate the body of the when
does clojure have a distinction between a missing and nil key in its map type?
@alexstokesit's because the value pre-destructuring is not nil.
right
ok thanks @dominicm
so the idea is that the expression used as the value for that binding must be nil for the when
to not execute?
contains has nothing to do with what @alexstokes is asking here -- there's just no way to use when-let
/`if-let` on destructured values
@bronsa no that was helpful! i get its not exactly related to my example under question
because it expands to something like (when-let [the-map x] (let [{:keys [a]} the-map ..))
right
got it
if you think about it, that's the only behaviour that makes sence as you could do (when-let [{:keys [a b]} c] ..)
just because you are looking for a single nil?
right — you would have to supply some predicate
cool thanks!
@alexstokes
(macroexpand-1 '(when-let [{:keys [a]} (assoc foo :a nil)] (println a)))
=>
(clojure.core/let [temp__5457__auto__ (assoc foo :a nil)]
(clojure.core/when temp__5457__auto__ (clojure.core/let [{:keys [a]} temp__5457__auto__] (println a))))
temp__5457__auto__
will be a map {:a nil}
, that is truly,got it
how do you require
in an eval? ie: clj -e "(require '[clojure.pprint :as pprint]) (pprint '(+ 1 1))"
I'm getting this:
Exception in thread "main" java.lang.RuntimeException: Unable to resolve symbol: pprint in this context
well you would need "(require '[clojure.pprint :refer [pprint]]) (pprint '(+ 1 1))"
$ clj -e "(require '[clojure.pprint :refer [pprint]]) (pprint '(+ 1 1))"
(+ 1 1)
ah, that works!
now, how could I get it to work if I put it in a single file?
that's where I get the RuntimeException: clojure ...
the way I usually do it is (require '[clojure.pprint :as pprint :refer [pprint]))
if you put what in a single file? and what’s the full RuntimeException?
everything that works in a repl will work in a file - the rules are the same (though there are a couple of things that work in a file that are no-ops in a repl but they aren’t relevant here)
ah, ok -- it's working now
thanks!
why do you use as
and refer
?
:as means that I can use eg. pprint/print-table
instead of clojure.pprint/print-table
and :refer means that I can use pprint
instead of pprint/pprint
the doc for :as is under (doc alias)
, the doc for :refer is under (doc refer)
(that’s clojure.repl/doc but a repl will have clojure.repl in scope normally on startup)
hrmm ... I was hoping pprint
would render to stdout without \n
you could pass a StringWriter or use with-out-str
, then trim the resulting string (hacky, but simpler than any other option I can think of here)
(ins)user=> (let [sw (java.io.StringWriter.)] (pprint {:a 0 :b 1} sw) (str sw))
"{:a 0, :b 1}\n"
(ins)user=> (let [sw (java.io.StringWriter.)] (pprint {:a 0 :b 1} sw) (clojure.string/trim (str sw)))
"{:a 0, :b 1}"
(ins)user=> (let [sw (java.io.StringWriter.)] (pprint {:a 0 :b 1} sw) (print (clojure.string/trim (str sw))))
{:a 0, :b 1}nil
hrmm ... those aren't spitting out anything for me
right, without a newline you need to use (flush)
to force output
the vm will-auto-flush on newline but without that you need to flush by hand
(and of course just making a string won’t make anything print to the console when running a program - in the ones not using print)
something.clj
(require '[clojure.pprint :as pprint :refer [pprint]])
(let [sw (java.io.StringWriter.)]
(pprint '(+ 1 1) sw)
(flush))
clj something.clj
returns nothingright, pprint with sw as an arg returns a string
you need to use print
well, it puts something in sw, you need to call str to get a string out of sw
then you need to print that string
the extra arg to pprint is a writer, it overrides *out*
as the place to put the output - and you need to use clojure.string/trim on the resulting string to remove the newline (as in my example above)
hrmm ... and it looks like I'm back to where I was without the sw
you need sw in order to trim the string - but I showed everything you need in my example
clj something.clj
"(ns guestbook.db\n (:require\n
...
here's my something.clj
now:
(require '[clojure.pprint :as pprint :refer [pprint]])
(let [sw (java.io.StringWriter.)]
(pprint (slurp "./db.cljs") sw)
(print (clojure.string/trim (str sw)))
(flush))
that should do what you want
(you might need to require clojure.string)
but it's resulting in the same output as
(require '[clojure.pprint :as pprint :refer [pprint]])
(pprint (slurp "./db.cljs")
well slurp returns a string - pprint of a string just adds “” and otherwise acts like println
err… it also prints \n instead of real newlines inside the string etc.
so maybe it's slurp that's messin' me up 😕
😆 all I want is to run https://github.com/weavejester/cljfmt at the CLI without a lein project
haha yeah this sounds like a hell of an x/y then
it’s too bad cljfmt and lein-cljfmt aren’t separate projects
oh wait they are
(just one github)
oh -- can I use like deps.edn
to grab it and use it via the CLI?
yeah - you can use cljfmt without the lein stuff - looks like a straightforward ns to use https://github.com/weavejester/cljfmt/blob/master/cljfmt/src/cljfmt/core.cljc
https://clojars.org/cljfmt - this is the clojars dep
😆 slurp
messes me up there too
same output as pprint
cljfmt has functions for forms and for strings though
and you can use clojure.edn/read-string
to get forms out of a string (or attempt it at least)
(def create-handles (fn [] (let [cudnn-h (cudnn-handle)
_ (cudnn-create cudnn-h)
src-tensor-d (cudnn-tensor-d)
dst-tensor-d (cudnn-tensor-d)
bias-tensor-d (cudnn-tensor-d)
filter-d (cudnn-filter-d)
conv-d (cudnn-conv-d)
pooling-d (cudnn-pooling-d)
norm-d (cudnn-lrn-d)
cublas-h (cublas-handle)
_ (cublas-create cublas-h)]
{::cudnn-h cudnn-h
::src-tensor-d src-tensor-d
::dst-tensor-d dst-tensor-d
::blas-tensor-d blas-tensor-d
::filter-d filter-d
::conv-d conv-d
::pooling-d pooling-d
::norm-d norm-d
::cublas-h cublas-h})))
is there a way to shorten this code? There seems to be too much repetition.that let block is pointless - just put the forms in the hash-map
it’s just noise
(except for the things to reuse one of the the bindings
then call into assoc
I need to create the cudnn handle before calling the other functions, as these are java bindings
(defn create-handles
[]
(let [cudnn-h (cudnn-handle)]
(cudnn-create cudnn-h)
(assoc {:cudnn-h cudnn-h}
::src-tensor-d (cudnn-tensor-d)
...)))
biting the bullet -- /me installing lein
(fn []
(into {::cudnn-h (doto (cudnn-handle) (cudnn-create))
::cublas-h (doto (cublas-handle) (cublas-create))}
{::src-tensor-d (cudnn-tensor-d)
::dst-tensor-d (cudnn-tensor-d)
::bias-tensor-d (cudnn-tensor-d)
::filter-d (cudnn-filter-d)
::conv-d (cudnn-conv-d)
::pooling-d (cudnn-pooling-d)
::norm-d (cudnn-lrn-d)}))
I think this works toooh, if that’s the only part of the order that matters, yeah
but the assoc is nicer imho and does the same thing
(and also guarantees evaluation order)
I guess it comes down to whether the extra {} are an improvement or background noise
you know, what, I'm going to use assoc, just in case the ordering is stricter than I think it is, - in which case mine may cause a non deterministic bug
usage of assoc with many keys in one call us under-appreciated IMHO 😄
to make it all "uniform", I'm going with:
(assoc {}
::cudnn-h (doto (cudnn-handle) (cudnn-create))
::cublas-h (doto (cublas-handle) (cublas-create))
::src-tensor-d (cudnn-tensor-d)
::dst-tensor-d (cudnn-tensor-d)
::bias-tensor-d (cudnn-tensor-d)
::filter-d (cudnn-filter-d)
::conv-d (cudnn-conv-d)
::pooling-d (cudnn-pooling-d)
::norm-d (cudnn-lrn-d))
the only slight problem is that cider indent doesn't auto indent the assoc k v pairs for me