This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-09-07
Channels
- # 100-days-of-code (1)
- # announcements (10)
- # aws (2)
- # beginners (134)
- # calva (25)
- # cider (29)
- # cljs-dev (43)
- # clojure (130)
- # clojure-dusseldorf (3)
- # clojure-italy (27)
- # clojure-nl (48)
- # clojure-spec (32)
- # clojure-uk (63)
- # clojurescript (75)
- # core-logic (5)
- # cursive (18)
- # datascript (2)
- # datomic (37)
- # emacs (5)
- # figwheel (13)
- # figwheel-main (55)
- # graphql (1)
- # java (7)
- # jobs (11)
- # jobs-discuss (19)
- # juxt (1)
- # leiningen (16)
- # luminus (10)
- # mount (3)
- # off-topic (40)
- # om (1)
- # onyx (1)
- # pedestal (7)
- # re-frame (40)
- # reagent (81)
- # ring (2)
- # shadow-cljs (32)
- # spacemacs (5)
- # testing (1)
- # tools-deps (48)
I think Lentes is the one most folks go to @aaron51 http://funcool.github.io/lentes/latest/#examples but I've never used it.
(my feeling is they're not used much in Clojure because we tend to work with get-in
, assoc-in
, and update-in
instead?)
lenses get a lot more useful when you have complex types for your lookups, and we don't really run into that with clojure - some more advanced lens features might be better emulated by specter
@noisesmith thanks, specter is what I was looking for! Not sure how that differs from lenses, yet
Nested #()
function is not allowed, how to change the nested #()
code?
@stardiviner pull the second one out to a named function. The threading macro does not play nice with anonymous functions (it threads via code transformations, not via result passing)
You can bind it to a name inside a let or letfn if you don’t want to expose it outside of the outer function
@dadair I use #(->> % :prelude first (format "....")) now. Seems works fine. I though must be function style in threading-macro.
Ah right, I didn’t look at the contents of the anonymous function. Yes in this case you don’t need to wrap the format call into a function as the prior “result” will already be passed as the last argument to the format call.
@stardiviner Or use (->> (format "\n#+begin_quote\n%s\n#+end_quote\n")
I find it's really nice to start with (-> ...)
and drop (->> ...)
in as needed.
(and, yes, in your case you can just use ->>
because everything except format
only takes one argument so the first argument is also the last argument)
@seancorfield you mean (-> % :prelude first (->> (format "...")) )
?
Right, perfectly fit
Jesus christ using clj-http
and cheshire
with some simple map commands is just blowing my mind
i think clj-http used cheshire under the hood so you can just supply json-parsing as an option to clj-http
Hi guys, so I have a snippet here, I was trying to hydrate a collection of objects concurrently using core.async, but it keeps freezing whenever I run it
Basically what I was aiming is I want to create a channel for each hydration process, run it, and retrieve it using map since I need the order of the objects to remain intact.
I also observe that the repl process didn't use any network connection at all, using nettop
Hi @henrik, thank you, you've been very helpful ever since I raise my questions about this yesterday. I notice that the main difference from your solution is that the go-blocks that does the "task" receives its arguments from a go channel, whereas mine from outside the go-blocks scope. Is that problematic? I thought that the value from outside the blocks will be retained because of closure.
@hawari.rahman17 Not really, a go-block will return immediately, but process whatever’s inside synchronously, and then return the results on a channel.
So in your solution, you could skip the channel and just put a bunch of go
blocks on a seq, then <!!
over them.
@hawari.rahman17 This is a go-only version of your code above.
Ah shoot, I think I know what my problem was, I wasn't doing recur properly: returning the rest of the objects on what should be the next object
Many thanks @henrik, what I thought was a core.async related problem turns out to be a basic clojure problem, haha
But yeah, as you can see, you can make do with a simple go
in some cases. Channels are useful when you want to hand off the stream of results to something else, like a separate handler.
Also, since putting something on a channel (without buffer) that already has something in it will block until something else removes that thing from the channel, you can have two (or more) asynchronous processes running in lock-step with each other.
The producer will wait until the consumer has taken the thing from the channel. It comes in handy at times.
I don't know why but it seems by using core.async
in my use case quite improved my throughput compared to my previous solution with pmap
That’s interesting. Are you constraining the number of simultaneous processes with core.async?
Hang on, I think pmap
uses real threads. By default, core.async would use lightweight threads.
Usually it takes about 1 secs to complete the whole process, now it's just about 600 msecs
That's what I thought as well, but I don't imagine the improvement will be this dramatic 😁
Time to refactor some old code then, I'd happily change one letter to this if that's what it takes for efficiency
core.async’s lightweight threads are of course multiplexed over a pool of actual threads
if you’re using pmap, and your process is taking 1 minute to exit, you might want to check that you are calling (shutdown-agents)
at the end
partition
function is too slow for my needs (Doing a challenge on hackerrank in Clojure :P)
How else would I be able to read data in a sliding window manner, like (parition k 1 coll)
but without partition. By using for?
@denisgrebennicov do you need an output collection? if no, doseq will be faster than for
my procedure was doing something like this
(->> arr
(sort)
(partition k 1)
(map (fn [coll] [(- (apply max coll) (apply min coll))]))
(flatten)
(apply min)))
therefore I am guessing that the partition part is the bottleneck. Assumption, but still
flatten is likely the biggest perf hit there, it's an ugly function
(defn maxMin [k arr]
(let [sorted-arr (vec (sort arr))]
(->> (for [i (range (- (count arr) k))
:let [coll (subvec sorted-arr i (+ i k))]]
(- (apply max coll) (apply min coll)))
(apply min))))
The solution which doesn't timeout is this
(defn maxMin [k arr]
(let [sorted-arr (vec (sort arr))]
(->> (for [i (range (- (inc (count arr)) k))
:let [coll (subvec sorted-arr i (+ i k))]]
(- (nth coll (dec k)) (nth coll 0)))
(apply min))))
Still, the question about: > What is the most idiomatic way to access nth element of the seq, list, vector > there are nth, get, and something else ?
hard to say, I think in general when I write programs that pull data out by index, I always get the index from the data, so it is never out of bounds
and I much prefer associative data like maps to indexed, so the usage of nth is pretty minimal
user=> (doc min-key)
-------------------------
clojure.core/min-key
([k x] [k x y] [k x y & more])
Returns the x for which (k x), a number, is least.
If there are multiple such xs, the last one is returned.
nil
user=>
@tobias.vetter they are discussing that in #clojure right now. I use open on my laptop
That can be slightly simplified to (map-indexed vector "abacbdcd")
Welcome! Always glad to see new folks trying out Clojure!
almost!
I like how on error it shows my actually input, and on success it shows the reader-expansion
earlier i was just showing you were always comparing against the empty string, not between two values
damn. You are right, thanks! Was thinking of writing my own cost function, like this
(defn str-cost
[str]
(->> str
(reverse)
(map-indexed (fn [idx val] (* (Math/pow 32 idx) (int val))))
(reduce +)))
You could also copy and paste the implementation of min-key, and change all < to corresponding calls to compare. min-key was written with numeric comparison in mind, not more general comparison.