This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-04-10
Channels
- # announcements (4)
- # beginners (116)
- # boot (4)
- # calva (63)
- # cider (8)
- # clara (20)
- # cljdoc (10)
- # cljsrn (69)
- # clojure (115)
- # clojure-austin (1)
- # clojure-dev (4)
- # clojure-finland (1)
- # clojure-italy (3)
- # clojure-nl (6)
- # clojure-russia (10)
- # clojure-uk (84)
- # clojurescript (28)
- # cursive (14)
- # data-science (1)
- # datascript (1)
- # datomic (11)
- # duct (3)
- # emacs (13)
- # figwheel-main (11)
- # fulcro (4)
- # graphql (6)
- # jackdaw (2)
- # jobs (23)
- # jobs-rus (1)
- # kaocha (11)
- # lein-figwheel (13)
- # leiningen (55)
- # luminus (14)
- # lumo (22)
- # off-topic (121)
- # pathom (19)
- # re-frame (6)
- # reagent (3)
- # reitit (22)
- # remote-jobs (10)
- # ring-swagger (1)
- # shadow-cljs (67)
- # slack-help (5)
- # spacemacs (1)
- # sql (18)
- # vim (28)
- # yada (2)
The arrow I've probably heard requested most frequently is a version of cond->
that threads the value through the tests too
@michael.gaare https://github.com/worldsingles/commons/blob/master/src/ws/clojure/extensions.clj (pulled out of our code base at work) [worldsingles/ws-commons "0.1.1"]
@seancorfield oh nice, I like that one
And heavily production-tested 🙂
@aryyya.xyz Have you asked in #emacs ?
(It's been too long since I used Emacs to be able to remember, I'm afraid)
@seancorfield No I have not, will do. How can I discover what channels exist on this workspace? Also, what editor are you using for Clojure if you don’t mind me asking?
Depending on the Slack client, there should be a label on the left Channels
which you can click to browse all the hundreds of channels...
@seancorfield Ah ok I just found that, thanks!
...but it's a fair rule of thumb to assume "There's a channel for that!" and just use the switcher (control-k) and start typing 🙂
(and there's #slack-help if you need to ask more detailed questions about how-to-slack 🙂 )
I switched from Emacs to Atom (+ ProtoREPL) after using Emacs for years... and at the end of last year I switched to Atom + Chlorine.
I talk a bit about that journey here http://corfield.org/blog/2018/12/19/atom-chlorine/
@seancorfield Ok, I’ve been using VS Code which has decent integration with Clojure and the REPL. Why did you switch? I find Emacs might be so complicated that I’m spending too much time configuring it and not enough time learning.
@seancorfield I’ll check that out.
Yup. That. 🙂
I've tried VSCode several times. Calva is coming along nicely. But I just prefer Atom.
(I have an Insider build of VS Code which I dip into from time to time)
My workflow these days is to fire up Cognitect's REBL with a Socket REPL and then connect Atom/Chlorine to it and run them side-by-side, so I can "inspect" values directly into REBL, and take advantage of tap>
for debugging.

I’ll check these things out. I tried out Atom before. VS Code seems very similar to me. I don’t mind either of them.
Part of what I love about Chlorine is that it's written in ClojureScript and I can hack on it live while I'm editing my work projects 🙂
That’s the beauty of this stack. I’m still a bit of a beginner, haven’t tried ClojureScript yet.
We tried cljs at work about four years ago and it was a pretty rough experience back then so we decided to move forward with JS and React.js on our front end for the time being. But things have changed a lot and cljs is much, much more mature these days so we might look at it again for some projects in future.
(we've been doing Clojure in production since 2011)
Online dating. World Singles Networks.
We have dozens of dating websites, all powered by Clojure on the back end and React.js on the front end.
Is there a better way of writing this function?
(defn lazy-distinct
"return a lazy sequence of coll that terminates
when a repeated value is encountered"
[coll]
(->> coll
(reductions (fn [[seen _] x]
(if (contains? seen x)
(reduced :end)
[(conj seen x) x]))
[#{} :start])
;; drop :start and :end markers
(drop 1)
(drop-last 1)
(map second)))
That seems very complicated... What's with the start/end markers? Aren't you just going to return the original sequence up to the first item that you've already seen?
(reduce (fn [[seen coll] x] (if (seen x) (reduced coll) [(conj seen x) (conj coll x)])) [#{} []] coll)
It's not lazy but...
i.e. the iterator might be infinite but if it does ever cycle back to a repeated value I want it to terminate
Hmm... interesting... So you'd have to track a potentially near-infinite set of "seen" values?
In which case, why do you care whether it's lazy or eager?
Oh, gotcha.
You can use clojure.core’s distinct transducer, except return a reduced value when a repeated value is encountered.
(defn take-unique []
(fn [rf]
(let [seen (volatile! #{})]
(fn
([] (rf))
([result] (rf result))
([result input]
(if (contains? @seen input)
(rf (reduced input))
(do (vswap! seen conj input)
(rf result input))))))))
(sequence (take-unique) coll)
Or this lazy-seq
-based version with a helper:
user=> (defn ld [coll seen] (cond (empty? coll) [] (seen (first coll)) [] :else (lazy-seq (cons (first coll) (ld (rest coll) (conj seen (first coll)))))))
#'user/ld
user=> (defn lazy-distinct [coll] (ld coll #{}))
#'user/lazy-distinct
sequence
of a transducing function is a good approach (but a bit more code that a simple lazy seq).
One day all processes will be made that way (transducers) 🙂
I'm still finding it hard to gain intuition about what transducers are "doing" under the hood
or rather what mental model to use when thinking about them - even though I can follow the logic it's still seems like this magical passing around of higher-order-functions
comp
of transducers behaving in a "backwards" manner being one of the more confusing things
Yeah, they definitely take some getting used to. Especially since it's very hard to exercise all three arities.
I can't shake off the feeling that its confusing design to use different arities for different lifecycle moments
I understand that its the most efficient way to work with the jvm and is thefore a good pragmatic choice
but transducers would be easier-to-grasp if they were defined using a record
(defrecord Transducer [init-val iterate-fn wrapup-fn])
IMHO
@qythium I found it useful to watch the Transducers talk Rich gave at Strange Loop a couple of times.
I also found it helpful to understand IReduceInit
first, then move on to transducers.
Hi there! I solving "Sum of the first nth term of Series" from http://codewars.com on clojure, and when I write solution I recived like "time out execution error". I solve the same problem on haskell and take execute time, it's about 0.166972266s on 25000 value of nth series. This is a clojure and haskell code that does the same thing.
clojure
(defn series-sum [n]
(->> (map #(/ 1 %) (range 1 (java.lang.Integer/MAX_VALUE) 3))
(take n)
(reduce +)
double
(format "%.2f")))
haskell
import Text.Printf
series :: [Double]
series = map (1/) [1, 4 ..]
seriesSum :: Integer -> String
seriesSum n = printf "%.2f" $ sum $ take (fromInteger n) series
Can anyone tell me, who I can solve this problem on clojure faster? Thank you in advance.As example on clojure with 3000 of nth series execute time is 7971.811645 msecs versus 0.002140344s on haskell
Yes. It helped. I guess I do not know something. 😯
Rational arithmetic being the default for division in Clojure has bitten a lot of people, you are not the first one, unfortunately.
(defn series-sum
[n]
(->> (range 1 (java.lang.Integer/MAX_VALUE) 3)
(transduce (comp (map #(/ 1.0 %)) (take n))
+
0.0)
(format "%.2f")))
^ should be slightly fasterafter warmup it clocks at 1.2 ms on my laptop for 25k vs 3.6 for the slightly modified version of yours
If run enough times, first solution is 0.4ms vs 0.1ms for @U050SC7SV solution.
of course. but how said one haskeller - better way do make an unefficient programs is to write code on lazy languages as on the strict ones and do the opposit way 🙂
anyway naive recursion in haskell in this task will be slower an take a HUGE amount of ram )
but you can always add strict evaluation thru $! or any else, but it is needed to remember )
For those interested, this compiles pretty much to identical Java code, and takes ~20us.
(defn series-sum [n]
(loop [i 1, j 1, acc 0.0]
(if (< i n)
(recur (unchecked-inc-int i) (unchecked-add-int j 3) (unchecked-add acc (/ 1.0 j)))
(format "%.2f" acc))))
(decompile
(defn series-sum [n]
(loop [i 1, j 1, acc 0.0]
(if (< i n)
(recur (unchecked-inc i) (unchecked-add j 3) (unchecked-add acc (/ 1.0 j)))
(format "%.2f" acc)))))
public static Object invokeStatic(final Object n) {
long i = 1L;
long j = 1L;
double acc = 0.0;
while ((i, n)) {
final long n2 = i + 1L;
final long n3 = j + 3L;
acc += Numbers.divide(1.0, j);
j = n3;
i = n2;
}
return ((IFn)const__8.getRawRoot()).invoke((Object)"%.2f", (Object)acc);
}
and about haskell you can uncomment and compare time and memory peak 😉 https://rextester.com/KONQV13764
The API Docs link at the bottom of of spec-alpha2 README is broken. Is this a known bug? https://github.com/clojure/spec-alpha2
yeah, I've never got around to building it
typically it builds off releases and we haven't done one
@cfeckardt I generated it, waiting on github to publish, but should eventually show up at https://clojure.github.io/spec-alpha2/
Thanks @alexmiller
Perhaps only certain routes https://clojurians-log.clojureverse.org/datomic/2017-09-19 for example
@plexus you are maintaining that right?
He is not in this channel, but I think through the magic of ambigous slack options I have maybe summoned him :’). Some parts of clojurians slack log appears to be down.
This was raised in #slack-help and I tagged him there as well.
Thanks!
when do I need requiring-resolve
? I am loading some namespaces lazily (for performance, I don’t need them every instance of the program).
are there likely to be multiple threads doing same ns at the same time?
I thought it was a convenience for the lazy-loaded require/resolve pattern
nice - but now I see the thread safety thing too, which is nice
it has the added benefit of using the (private) serialized-require
which is a temporary crutch until we can better fix require
eventually, serialized-require
should just revert to require
then, just require
I’m hitting the locking issue with GraalVM again when using requiring-resolve, which can be patched, but if I don’t have to 🙂
no need to
hmm, I think this lazy require approach isn’t working at all with GraalVM since it needs eval basically right. so I’ll scratch that idea
yeah, I was wondering how that worked :)
can you use the var method via interop?
ahh right, which means the whole "last minute lazy require" thing is pointless anyway, I think