This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-11-13
Channels
- # admin-announcements (6)
- # beginners (19)
- # boot (1)
- # cbus (2)
- # cider (3)
- # clara (24)
- # cljs-dev (4)
- # cljsrn (18)
- # clojure (168)
- # clojure-boston (1)
- # clojure-dev (55)
- # clojure-russia (199)
- # clojure-sg (2)
- # clojurescript (38)
- # clojurex (1)
- # core-async (15)
- # css (16)
- # cursive (62)
- # datomic (23)
- # editors-rus (17)
- # events (3)
- # funcool (1)
- # hoplon (360)
- # ldnclj (37)
- # lein-figwheel (11)
- # leiningen (1)
- # nginx (1)
- # off-topic (13)
- # om (361)
- # onyx (1)
- # re-frame (56)
- # reagent (24)
- # robots (1)
- # spacemacs (46)
- # yada (9)
@ghadi @triss - here's a little more on debugging from within a go block or thread: http://stackoverflow.com/questions/32715869/clojure-log-to-console-when-code-is-being-executed-in-a-go-block-or-thread/32717490#32717490
I prefer (case choice :set value "")
over (if (= choice :set) value "")
. Am I strange?
Extremely amazing and convenient that (assoc ["foo"] 1 "bar")
adds an element to the vector!
why not using (conj [“foo”] “bar”)
?
@delaguardo: It's convenient that I don't have to distinguish between modifying an element of the vector and adding one to the end.
because (not= nil [])
)
and in doc section for assoc - Note - index must be <= (count vector).
for vectors
@bhagany: Thanks! I have (update-in db [:product :times] (fnil #(assoc % index time) []))
now
@mbertheau: alright, nice. Maybe I don't quite understand your goal, but it makes more sense to me to wrap the '%' in fnil
… otherwise (I think) you're going to end up with an empty vector if you don't already have a vector, instead of a one-element vector
Hi - really simple question -- why doesnt this code work: http://pastebin.com/cFu7juQH
lein repl
nREPL server started on port 54991 on host 127.0.0.1 -
REPL-y 0.3.7, nREPL 0.2.10
Clojure 1.7.0
OpenJDK 64-Bit Server VM 1.7.0-u60-unofficial-b30
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
user=> #?(:clj 1 :cljs 2)
RuntimeException Conditional read not allowed clojure.lang.Util.runtimeException (Util.java:221)
IllegalArgumentException Wrong number of args passed to keyword: :clj clojure.lang.Keyword.throwArity (Keyword.java:97)
This should work right?I can iterate over a map with for
: (for [e {:a 1 :b 2}] e)
. How can I put the result back into a neat map?
Yeah, there are utility libraries like https://github.com/Prismatic/plumbing that provide map-keys
and map-vals
.
Hmm, I have a map {:mon {:a true :b [2 4 3]} :tue {:a false :b [1 5 2]}}
and I want to return the same map with :b
of the weekday ordered, but leave the current weekday (`current-weekday`) alone.
That seems so easy to put in words, but hard to express with clojure. I have (reduce-kv #(assoc %1 %2 (case %2 current-weekday %3 (update-in %3 [:b] order-b))) {} my-map)
If you are trying to update a value in a map, you probably want an update-in like function. (update-in {:a {:b 10}} [:a :b] #(+ 5 %)) ;=> {:a {:b 15}}
@mbertheau: Can the ordering you want be permanent? If so perhaps easier to use a sorted-map... (not sure your business use)
@mbertheau: maybe specter can help you there https://github.com/nathanmarz/specter
(require '[com.rpl.specter :as sp])
(sp/transform [sp/ALL sp/LAST :b] (comp vec sort) {:mon {:a true :b [2 4 3]} :tue {:a false :b [1 5 2]}})
=> {:mon {:a true, :b [2 3 4]}, :tue {:a false, :b [1 2 5]}}
😊 great minds think alike
anybody using pedestal here? I have an url like "/campaign/:code-name/", and requests to that url with trailing slash return 404. While requests without trailing slash are handled normally. Any ideas how can I handle trailing slash?
explicit route that 301s? never alias bad for seo
^ so am told
@asolovyov: they’re different routes in Pedestal’s world. i recommend pinging @ohpauleez on twitter for a quick answer
they're different routes in any world. I think the think that apache does and I guess nginx with try_files followed by index directive leads to this idea that both should work as some standard practice
@robert-stuttaford: yeah, I had an interceptor strip-trailing, which stopped working... 😕
@asolovyov ah I see, that is odd
when I try to put an interceptor which would strip trailing slash from requests, it never gets called - pedestal returns 404 before that
when I want override a specific implementation of a multimethod in a test, is def
ing it in the test namespace “clean”? are there other ways?
@martinklepsch: you can alter-var-root on it, or I think you can setDynamic on the var and then use binding
@tcrayford: what would I bind it to though? if I alter-var-root the var/dispatcher the already defined implementations will probably just stick around?
@noonian, @cjmurphy, @meikemertsch: Thanks for your input :) specter
looks like the most readable. I'll see if I can get the conditional in as well. Thanks
@martinklepsch: rebind it to a function, and the already defined implementations do stick around, but you don't hit them
@tcrayford: if I do that all other implementations will not work anymore and they’re needed 😄
@martinklepsch: in which case I'd do the typical thing of "change your design so that this kind of var based thing can be swapped out in tests without actually using vars"
this is about clojure.test/report
— guess I’ll stick with def’ing it in the test namespace
@martinklepsch: I hate clojure.test/report
bolth directly monkeypatches the method definitions in it: https://github.com/yeller/bolth/blob/master/src/bolth/runner.clj
@martinklepsch: I am ashamed to admit it, but I got around this by declaring a ‘normal’ defn
which simply dispatched to a defmulti
and then in tests overrode the defn
. For an individual defmethod
I had the implementation call out to a defn-
which I could override in a test … yeah, not great but it worked
@colin.yates: funky! 😄
specifically the usecase is allowing users of test.chuck to customize how their smallest failing thing is getting printed
@martinklepsch: huh, so they could pretty print it? Is this for checking
or something else?
@martinklepsch: btw Gary and I have been talking a lot about making defspec
work significantly better with clojure.test
, including incorporating the ideas in checking
with regards to report
and such. It seems like pretty printing or whatever would also be a good thing to throw in that (might wanna ping him about it)
yes for checking
… Mostly stumbled upon it because I tried making collection-check and then test.chuck work with cljs
@tcrayford: have some PRs here https://github.com/gfredericks/test.chuck/pulls (in good company haha)
@martinklepsch: that must be a lein thing - this works in 1.7 without lein
@martinklepsch: in reference to your reader conditional repl question earlier that is
? ¯\(ツ)/¯
if you want to file a jira, you could see if anyone votes for it as useful. not sure what Rich would think about it.
I'm fully aware this is a somewhat stupid question, but is there any JVM/Clojure hook that gets called on program exit?
@jaen: I think Runtime.addShutdownHook() might do - e.g. https://stackoverflow.com/questions/11709639/how-to-catch-ctrlc-in-clojure
@itruslove: looks legit, thanks
jaen: Works great, but won’t be invoked with “lein run” then Control-C. Does work is you “lein trampoline run” before Control-C.
jaen: Yeah, one can get, uh, frustrated, wondering why it doesn’t work before one tries running from an uberjar or something. ;)
quiz: I have a vector [:a :b :c :d]
and an integer 6, and I want to “extend” the vector repeating its last argument until its length is 6, like this: [:a :b :c :d :d :d]
. What’s the most concise way?
I had (map-indexed (fn [i e] (nth v i e)) (repeat n (last v))
, v
being my original vector and n
the integer
@eraserhd: +1 nice example of thinking functionally rather than idiomatically
or maybe ‘declaratively’ is a better work than ‘functionally'
this one’s not as good as @eraserhd’s but: (into v (repeat (- n (count v)) (last v))))
?
how about this: (if (<= 6 (count v)) v (ask-slack)))
OH: Col, you need to stop trying to be funny!
@colin.yates: good one! that’s the one I went with 😉
you're in a macro... then who cares, it's run only once. butlast
is prevalent in macros
if you’ve got something like (defn foo [& args] (my-very-inefficient-macro…))
, wouldn’t the macro get run every time the function gets called?
only once, the compiler runs the macro and uses the result to create the runtime function
It's worth mentioning that if e.g. last
is used in the code returned by the macro (i.e. if it's in the macro-expanded form), it will be run whenever the macro is run. But if it's just in the code that generates the expansion, it will only be run once.
At least, I'm pretty sure that's how macros work…
unless you're doing weird stuff with eval
(that you shouldn't be), macros won't expand at runtime
it helps to think of macros as compile-time functions that generate functions
@colin.yates: it probably helps because that's what they are
or think of them as the ‘text-expander’ utilities that you get on OSX
macros are literally implemented as functions run by the compiler rather than the runtime
@bronza . They aren’t often described like that though
yeah, presumably the defwhatever
-type macros you get in e.g. korma don’t necessarily generate functions
@swizzard: when learning lisps, I found that it helped me quite a lot to understand macros by understanding that there's nothing magical about `
yep - code rather than functions - my bad
and that's it's only used in macros because it's convenient, rather than because it's needed
so to take the usual example of
(defmacro unless [cond & body] `(if (not ~cond) body))
my first and (probably) last macro: https://groups.google.com/d/msg/clojure/jQoXknHxu44/aABKUfVtBwAJ
if you think of it as a function (defn unless [cond & body] (list* 'if (list 'not cond) body))
and realize that the difference between this function and the defmacro is just: 1- we don't need to use "` ", it's not special syntax it's just a convenience to avoid nesting quotes and calls to list
i think part of the problem is that i haven’t had enough use cases where the pain of writing a working macro is less than just using HOFs or whatever
most experienced clojure devs would tell you that if you can use a HOF, you shouldnt' write a macro for that
@jaen I believe you have to provide it as normal metadata: (defonce ^{:doc “Hello”} foo (+ 1 2))
there's a ticket for this - turns out to be surprisingly complicated to fix
you can follow the comment/patch trail there
can someone explain what happens to a record, when it gets evaluated?
(defrecord Kikka [])
(= (->Kikka) (->Kikka)) ; true
(= (->Kikka) (eval (->Kikka))) ; false
there were changes in this area in 1.8 - I think you will now see true for both
https://github.com/clojure/clojure/commit/1d5237f9d7db0bc5f6e929330108d016ac7bf76c in particular
I think for a records with at least one element, you would have seen true for both even in 1.7
this was the special case of records or types with no fields (which is not common)
So the weird thing about using test.check to verify how rewrite-clj does things is that I’m generating random clojure programs.
what about this one (with 1.8.0-RC1):
(= pos? (eval pos?)) ; true
(= [pos?] (eval [pos?])) ; false
relates to the real life case I’m trying to figure out: (= schema.core/Keyword (eval schema.core/Keyword)) ; false
. Keyword is a record with clojure.core/keyword?
in it. Evaluating the record loses it’s equality.
I am looking for a way to kill the repetition in this file: https://gist.github.com/eraserhd/bcdc5a04a54f96e95ab2
All I can think of so far is a macro that takes a map and generates all the things, but this seems pretty ugly.
You might be interested in this library that helps create test.check generators: https://github.com/holguinj/jen
@eraserhd: yep, there is an example in the readme. YMMV as I haven’t used it myself. But I know the author and watched him give a talk on the library. Its motivation was making it easier to create complex generators.
It appears that all it does is call gen/recursive anyhow. I think what I want is letrec for generators.