This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-05-28
Channels
- # beginners (67)
- # boot (31)
- # cider (1)
- # cljs-dev (12)
- # cljsjs (1)
- # cljsrn (23)
- # clojure (86)
- # clojure-russia (2)
- # clojure-spec (6)
- # clojure-uk (12)
- # clojurescript (171)
- # core-async (2)
- # core-matrix (1)
- # cursive (3)
- # events (1)
- # lumo (6)
- # off-topic (118)
- # om (51)
- # onyx (16)
- # perun (3)
- # re-frame (14)
- # reagent (19)
- # uncomplicate (5)
- # unrepl (6)
- # untangled (6)
do people use edn as a configuration format? How’s that working out? It seems like clojure-the-language really wants me to use namespaced keywords, but that seems very verbose when using EDN since it doesn’t seem to have ns aliases
@noisesmith yeah I guess. That seems iffier than writing the real transducer though 😕
kinda noob here, but I just typed in a function into the repl. How do I print the function back out as source code? I tried (clojure.repl/source myfun) but “Source not found”
the source function only works for code that came from a file
it's really too bad
We have source
for REPL-entered def
s working in Lumo, Planck, and Replete—it seems like a reasonable and useful thing to have.
@lvh, I've use EDN (without namespaced keywords) for config for years in various projects and it has worked well, I do recommend it.
Not sure if Clojure in general pushes for namespaced keywords, but clojure.spec does. I like the idea in principle, but haven't used it much in practice. My config-validation specs use (s/keys :req-un [...])
…
for namespaced keywords, shouldn't having a namespace alias in effect apply it to an edn file you read?
hi guys, can cljfmt rewrite one line if to 3 lines:
(if true true false) to
(if true
true
false)
@nxqd: no, because both are equally valid from cljfmt’s POV. zprint might be able to, though.
@weavejester thanks for the info. I will check zprint.
is there any best practice for noting something is an element of a collection? (map (fn [fluffy-cow] (fiddle fluffy-cow) fluffy-cows) or is just dropping the “s” the convention?
bherrmann: i prefer "the-foo" over "a-foo", since the latter implies sth like "any old foo". but we're always dealing with the specific foo we've been handed. works for fn args too.
Still, I do tend to do the singular/plural thing when naming: (map (fn [row] ...) rows)
and so on
Just found this gem: http://tonsky.me/blog/readable-clojure/
@U060FKQPN everyone knows you prefix with ! for a mutable reference
@U051H1KL1 this is the first time I hear/see !
for refs in 3 years, but I like it more than *
. even though *
is a nod to C++/objC pointers
;; import static jcuda.jcudnn.JCudnn.cudnnConvolutionForward;
;; import static jcuda.jcudnn.JCudnn.cudnnCreate;
;; import static jcuda.jcudnn.JCudnn.cudnnCreateConvolutionDescriptor;
;; import static jcuda.jcudnn.JCudnn.cudnnCreateFilterDescriptor;
;; import static jcuda.jcudnn.JCudnn.cudnnCreateLRNDescriptor;
;; import static jcuda.jcudnn.JCudnn.cudnnCreatePoolingDescriptor;
is there a way to import static functions from javak instead of classes ?Is there a rule-of-thumb on "when to use qualified keywords in domain data structures?" E.g. datomic's tx-report has unqualified keys, like :db-before, even though tx-report is not a data structure internal to datomic – it is used all over the client apps.
misha: that's an interesting topic. consider the html "domain". usually attribs get passed as a kw map, e.g. {:class "foo"}. if you use an ns you could do {:html/class "foo"} and then validate. but then you'd get stuff like {:html/style {:html.style/color :red} or similar. is it worth the trouble? i think there's a convenience tradeoff, similar to the one between strictly and loosely typed languages.
@U051HUZLD I've learned to use them almost always by default, because the benefits in terms of code clarity and data traceability usually outweigh the slight inconvenience in typing. I believe this idea has matured only recently in the Clojure community (and is notably championed by spec), which is why we still see a lot of unqualified keywords out there.
A more important point of consideration IMHO is whether these namespace-qualified keys are acceptable by your non-clojure consumers, in which case you may want to use another syntax for namespacing (e.g :html_class
instead of :html/class
)
@U06GS6P1N but :html_class is not namespaced.
@U0LGCREMU it's not in the Clojure sense, but it gives you most of the benefits of namespacing in terms of readability and data traceability
sure but that misses the point of namespaces. so you don't need to parse global names. compare :html/class, :html/id, etc. to :html_class, :html_id, etc.
Because the JS guys may not want to consume the dots and slashes in the keys :)
My point being that including the namespace information in this way is a better alternative than giving up on it in this case
Yeah not a great situation to be in
you can always translate namespaces to strings for languages that do not support nss.
@U0LGCREMU you mean keywords?
yeah basically my approach for now. Slightly inconvenient in JS to write m['my.ns/key'] but it works. Actually, I'd be more worried about languages that rely heavily on classes to represent information, e.g that parse incoming JSON into instances of classes defined ahead-of time. I reckon this is common in the Scala world for example
bherrmann: i prefer "the-foo" over "a-foo", since the latter implies sth like "any old foo". but we're always dealing with the specific foo we've been handed. works for fn args too.
e.g. (defn f [the-foo] (let [foo (munge the-foo)]...)) "the-" always tells me i'm dealing with an arg, not something letted or a global.
misha: that's an interesting topic. consider the html "domain". usually attribs get passed as a kw map, e.g. {:class "foo"}. if you use an ns you could do {:html/class "foo"} and then validate. but then you'd get stuff like {:html/style {:html.style/color :red} or similar. is it worth the trouble? i think there's a convenience tradeoff, similar to the one between strictly and loosely typed languages.
yeah basically my approach for now. Slightly inconvenient in JS to write m['my.ns/key'] but it works. Actually, I'd be more worried about languages that rely heavily on classes to represent information, e.g that parse incoming JSON into instances of classes defined ahead-of time. I reckon this is common in the Scala world for example
question about recur & co, I wrote the following algorithm to fill in an image with a certain colour (inspired loosely on https://en.wikipedia.org/wiki/Flood_fill )
(defn fill-coordinates
"Recursive function that generates all the coordinates
which need to be filled in with the new colour.
It's inspired by the flood-fill algorithm, but had to be
heavily modified since there is no mutation here."
([img coord old-colour new-colour coords]
(if (or (= new-colour old-colour)
;; when outside of the range no need for the extra check
(not (valid-coord? coord img)))
coords
(if (or
(contains? coords coord)
(not= (get-in img coord) old-colour))
coords
(letfn [(rec-call [direction filled-coords]
(fill-coordinates img (move direction coord) old-colour new-colour filled-coords))]
(let [c-incl (union coords #{coord})
c-north (rec-call :north c-incl)
c-east (rec-call :east c-north)
c-south (rec-call :south c-east)]
(rec-call :west c-south))))))
([img coord new-colour]
(let [old-colour (get-in img coord)]
(fill-coordinates img coord old-colour new-colour #{}))))
now the problem is that on big images I get a stack overflow
but I don't think I can actually use recur in this case right?
I was trying to avoid in place mutation, and I also have a letfn
there for convenience
recur
doesn't like leftn
as point to jump to, but more importantly I'm not sure this is tail recursive or could be written as such
wait, why can't you recur inside a letfn? - if you mean jumping from tail of one function to another function, check out trampoline
trampoline is another way to avoid growing stack
(without being limited to self calls)
I meant that if I just do a recur
it will fail saying it's expecting 5 arguments instead of 2, so it's trying to call the outer function I guess
you can only recur into the same function - a recur to a letfn bound function must be done inside that function
and well these are all self calls in a way, even if there is a chain of them
(let [c-incl (union coords #{coord})
c-north (rec-call :north c-incl)
c-east (rec-call :east c-north)
c-south (rec-call :south c-east)]
(rec-call :west c-south)))))
and you can only recur to the same arity
ahh ok sorry I understood now
ok I can try
(defn map<-lst [f lst]
(into {} (map (fn [e] [(f e) e]) lst)))
is there a better way to write this ?move a paren so it uses a transducer
(defn map<-lst [f lst]
(into {} (map (fn [e] [(f e) e])) lst))
one paren moves from being after lst to being after the fn arg to map
(defn map<-lst [f lst]
(into {} (map (juxt f identity)) lst))
not really - there's no accumulated result over args
(not in the way scanl does that is - of course it's all accumulated into a hash-map)
that's a lot worse than (juxt f identity)
v is a poor performing version of vector