This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-02-16
Channels
- # aleph (2)
- # beginners (68)
- # boot (25)
- # bristol-clojurians (5)
- # cider (10)
- # cljs-dev (60)
- # cljsrn (1)
- # clojure (138)
- # clojure-austin (1)
- # clojure-france (2)
- # clojure-greece (11)
- # clojure-italy (4)
- # clojure-russia (16)
- # clojure-spec (4)
- # clojure-uk (75)
- # clojurescript (26)
- # core-async (28)
- # cursive (25)
- # data-science (4)
- # datomic (16)
- # defnpodcast (2)
- # devops (2)
- # docs (10)
- # duct (11)
- # emacs (1)
- # events (1)
- # figwheel (8)
- # fulcro (61)
- # garden (2)
- # hoplon (6)
- # java (6)
- # jobs-discuss (1)
- # lein-figwheel (14)
- # leiningen (86)
- # luminus (11)
- # off-topic (8)
- # parinfer (9)
- # pedestal (2)
- # re-frame (19)
- # reagent (1)
- # ring (3)
- # ring-swagger (8)
- # shadow-cljs (278)
- # spacemacs (13)
- # sql (5)
- # testing (7)
- # unrepl (8)
- # yada (25)
I was hoping it would get fixed for 1.9 -- I opened that ticket 18 months ago after seeing several people trip over it (via Slack and via the mailing list) and it's a "known" issue that goes back longer than that...
How can I expand a symbol being passed into a macro in both clojure and clojurescript?
(s/def :my-spec/my-thing (s/keys :req-un my-keys))
I want to expand my-keys
hereI was able to get things working in clojure by doing
(s/def :my-spec/my-thing (eval `(s/keys :req-un ~my-keys)))
But this did not work in clojurescript as the eval works differently@caleb.macdonaldblack write another macro which emits the correct s/def
form
(defmacro def-my-spec [my-keys]
`(s/def :my-spec/my-thing (s/keys :req-un ~my-keys)))
(def-my-spec my-keys)
@moxaj Thanks that works. Do you think this could be improved or reduced any further?
And is this pretty much the standard solution to this problem?
Okay thank you
(defn vec-dot [lhs rhs]
(reduce + (map * lhs rhs)))
(vec-dot [1 2 3] [2 3 4])
(defn my-do [f v offsets]
(letfn [(helper [so-far rst]
(if (= (count rst) 0)
(f so-far (vec-dot so-far offsets))
(let [[h & rst] rst]
(doseq [i (range h)]
(helper (conj so-far i) rst)))))]
(helper [] v)))
(my-do (fn [& args] (println args))
[2 4 3]
[1 2 3])
Is there a better way to write this? I'd like to get rid of the *
in vec-dot
by cleverly incrementing while looping, and I would like ot generate the 'lattice' without using nested function calls.what's the difference between with-bindings
and binding
?
with-bindings is easier to use when the binding map is dynamic
Hello everyone, lets assume I have structure like this:
({:logged_at #<DateTime 2013-03-29T12:00:05.000Z, :data "somedata"} ,
{:logged_at #<DateTime 2013-03-29T12:10:05.000Z, :data "someotherdata"} )
what im trying to do is convert it to something like this:
({:logged_at "2013-03-29" :data "somedata"},
{:logged_at "2013-03-29" :data "somedata"} )
I wanted to achieve this by something like this:
(map #({:logged_at (t/date->formatter (:logged_at %) "dd.MM.yyyy")
:data (:data %)}) my-map))
but clojure wont allow me to do that 😕 Any ideas how to achieve this?In other words I want to convert all the DateTimes to just string represantation of date 🙂
try adding do
?
(map #(do {:logged_at (t/date->formatter (:logged_at %) "dd.MM.yyyy")
:data (:data %)})
my-map))
i still get the clojure.lang.ArityException: Wrong number of args (0) passed to: PersistentArrayMap
error
oh nvm, got it now, any explanation why it works with do and not with only map?
you can't use map literals with function literals
do #(hash-map :k :v)
or (fn [] {:k :v})
but not both
that error message is telling you that it tried to invoke the map as a function, passing it 0 args
the anonymous function you've got there is expanding out to something like (fn [m] ({:logged_at ..., :data ...}))
whereas, with do
, it expands to (fn [m] (do {:logged_at ..., :data ...}))
, which is no longer attempting to invoke the map as a function
@ondrej.l.cermak (mapv (fn [{:keys [logged_at] :as h}] (assoc h :logged_at (t/date->formatter logged_at "dd.MM.yyyy"))) data)
given you're only updating one key, it's preferable to use update
- something like (map #(update % :logged_at t/date->formatter "dd.MM.yyyy") my-map)
should do the trick
more examples at https://clojuredocs.org/clojure.core/update
Oh, I understand it now, thanks guys. i did not realize its trying to invoke the map definition as a function.
@ondrej.l.cermak you can find out these things from the repl:
user=> (macroexpand '#({}))
(fn* [] ({}))
Thanks 🙂
Btw one more thing, how would you then transform it, most efficiently to something like this? I cant find solution other that iterate over the map more than once 😕
({:data({...}), :logged_at 10.02.2018}
{:data({...}), :logged_at 10.02.2018}
{:data({...}), :logged_at 12.02.2018}
{:data({...}), :logged_at 10.02.2018}
{:data({...}), :logged_at 12.02.2018}
{:data({...}), :logged_at 16.02.2018}
{:data({...}), :logged_at 18.02.2018}
{:data({...}), :logged_at 16.02.2018})
{ :dates ["10.02.2018" "12.02.2018" "16.02.2018" "18.02.2018"]
:dates_count [{ :data [3 2 2 1]}]}
I tried group-by , but its kinda unfortunate solution
@sundarj well the point of it is to have unique dates, sorted from lowest to highest and then in the :dates_count
i need to have the count of theese dates in the same order. It is for frontend graph plugin, which accepts data exactly like this:
{ :dates ["10.02.2018" "12.02.2018" "16.02.2018" "18.02.2018"]
:dates_count [{ :data [3 2 2 1]}]}
ah, i see. yeah i think the sorting has to happen in a separate step to the building of that map
well, i can sort it out when querying from the database. I just need the order of the dates and date-count be the same, so the count would truly represent the count of that given date in the sequence of maps
But I seem to be unable to do it. My knowledge in clojure is very limitted 😕
well if you conj
the corresponding data to the different vectors at the same time, the indexes will match up, right?
I never used conj, I always used assoc
or merge
. So im thinking how to add teh logic of uniquness and counting to the code you posted
what is in the :data field of the map doesnt really matter to me
Hi, I'm trying to suppress logging output from a third-party library while I'm at the REPL. I've tried multiple variations of with-binding, binding, alter-var-root, even (System/setOut (java.io.NullPrintStream.))
and still I get output
Say, as an exercise as much as anything else, would it be possible to construct a thing that looked like a map (and maybe implemented the IMap protocol or whatever it's called), but would always return a constant value when accessed with (get m :keyword)
or (:keyword m)
? Like (constantly 43)
but with a map interface?
Like a map with conceptually infinite keys?
Sure, probably the smallest such interface you can implement is clojure.core.ILookup @timgilbert
Ah, right you are, thanks @tbaldridge:
(def m (reify clojure.lang.ILookup
(valAt [o k] 42)
(valAt [o k n] 42)))
=> #'user/m
(:foo m)
=> 42
(get m :foo)
=> 42
ancient but still useful wizardry: https://gist.github.com/semperos/3835392
Hey folks, I’m writing a functino that creates a spectrogram but the biggest slow down at the moment is the process of actually slicing up the data. partition is by far taking the most time. I was curious if there was a better way of writing this function
getting numerics to be fast in clojure is non-trivial, the fastest thing in terms of developer time is likely writing something in java
but if it must be clojure, you can use *warn-on-boxed*
that .multiply call is still going to be slow - you probably want to hint it
and that window function is going to be very slow
That is a matter of my own ignorance, I understand the words you are saying but I don’t know
@gganley that raised-cos (and the hamming derived from it) is still going to be extremely slow, and if you must write it in clojure *warn-on-boxed*
will at least tell you what the slowest parts are http://insideclojure.org/2014/12/15/warn-on-boxed/
not only that, but the partitioned data coming in is not a counted type, so it’s literally iterating through to count the items in an O(n) manner every time the inner function is called
I mean line 3
(of the window function paste)
that is using segment, generated by partition, which is not a counted type
I think the window function is not the issue since it is the same running time w/ or w/o the windowing functino
are you forcing the result in the benchmark? because I find that surprising
not in a nested way though - depends on the shape
it forces the “spine”, but you need nested doall if there are nested lazy results
(doall (map doall result))
Unfortunately I need to go to a meeting but this is illuminating, I’ll be back but thank you for the help
can someone explain me how is this possible?
(identical? Double/POSITIVE_INFINITY Double/POSITIVE_INFINITY)
;; => false
@carocad identical? is explicitly asking if two objects in the vm are the same object
making singleton objects for every double-precision floating point value would be a huge waste of memory, in practice only some numerics (integral types between -128 and 127 inclusive AFAIK) are cached and shared as singleton objects
what clojure does when you use a double in a form is to put it in a box of type Double
(except for rare circumstances where it can optimize because it knows the value can be used unboxed)
so in the case of identical? here you are putting the same value in a box twice, and asking if it’s the same exact box (and the answer is no, it was put into two different boxes)
@noisesmith thanks for the explanation. It does makes sense from the point of view of Clojure. However I still find it confusing. If I do
(identical? "foo" "foo")
then I get true. I would expect it to be false base on your explanation 😞
strings and numbers are handled differently
is there a reason you need object identity and not just equality?
@noisesmith do you know of a way to bypass this for static final
numbers? I would not like to use the slower =
I’ll repeat what I told gganley - if you need speed with numbers your most efficient thing in terms of developer time is likely to write some java
I don’t know about caching number instances - someone in #clojure-dev might have some clever suggestions
also I have a hunch == should be faster with floats than =, not absolutely certain though
@noisesmith You are right, I will stick with = in Clojure and once I get to the Java code I will use the strict comparison. Thanks for the help
I find that as I use better code folding / code jumping tools, I tend to have fewer modules with more lines of code (some going up as high as 5k lines of code / file). Is there fundamentally any downsides to *.cljc files with line count in the thousands ?
You can eventually hit method size limits in the init class if you aot
Also, as it stands now, Cursive can get sluggish with several thousands of lines in one file
is this limit at 65535 ? https://stackoverflow.com/questions/4342072/what-is-the-maximum-number-of-methods-a-java-class-can-have
That’s not the issue - every var has some setup bytecode that goes in the class initializer and eventually you’ll hit the max method size
I want to create a terminal program like HTOP where it’s constantly updating and redrawing as opposed to printing new lines to stdout. How can that be achieved in Clojure? Is there a way to output to the terminal that can be erased later?
Check out the Unix/Linux program 'watch' in case it simplifies your life dramatically -- basically runs any other program that outputs to stdandard out, with a configurable period.
redrawing the screen each time.
I see things for lantern (apparently a java ncurses-like lib) which have bindings written in clojure
Yeah, I’ve used Lanterna for clojure-y ncurses-y stuff. Kinda nice. https://sjl.bitbucket.io/clojure-lanterna/
Ah thanks a ton @chris & @manutter51 I’ve been trying to learn what the terminology is for that feature. I’ll definitely give laterna a shot.
is there a better way to write:
(defn foo [obj]
(fn [& args]
(let [[t1 t2 t3 t4 t5 t6 t7 t8 t9] args]
(case (count args)
0 (.go obj)
1 (.go obj r1)
2 (.go obj t1 t2)
3 (.go obj t1 t2 t3)
4 (.go obj t1 t2 t3 t4)
5 (.go obj t1 t2 t3 t4 t5)
6 (.go obj t1 t2 t3 t4 t5 t6)
7 (.go obj t1 t2 t3 t4 t5 t6 t7)
8 (.go obj t1 t2 t3 t4 t5 t6 t7 t8)
9 (.go obj t1 t2 t3 t4 t5 t6 t7 t8 t9)))))
I’m with a interesting problem:
(loop
I'm building a bot that will query some external resource ( rateLimit 20 requests/s ) and do some computation with the results.
(recur))
I’m monitoring the application and after some time the application stop to work. there is no exception, nothingis this happening inside a future?
the default behavior of futures is to stash the exception and only throw if you deref the future
you can use either C-\
or jstack
to see the stack traces of all currently running threads, that can verify what’s actually still executing
if it’s a future you never plan to deref, the best bet is (future (try .... (catch Exception e (log/error e))))
because without that logging, you’ll never see the error
@noisesmith thanks sir, you were correct !
cool- yeah I have had exactly this problem in my own project - it’s a good idea to put a try/catch with a log message in any future that you don’t dereference
it has an optional :throw-exceptions false
config though