This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-06-03
Channels
- # beginners (112)
- # boot (13)
- # cider (17)
- # cljsjs (2)
- # cljsrn (8)
- # clojure (57)
- # clojure-spec (2)
- # clojure-uk (5)
- # clojurescript (51)
- # cursive (4)
- # data-science (15)
- # datomic (1)
- # duct (17)
- # garden (4)
- # lein-figwheel (49)
- # midje (1)
- # nyc (1)
- # off-topic (8)
- # pedestal (1)
- # portkey (20)
- # re-frame (4)
- # reagent (27)
- # ring (1)
- # shadow-cljs (24)
- # spacemacs (7)
- # specter (3)
- # sql (5)
- # yada (5)
I found three ways on stackoverflow, they are (first (drop-while ...))
, reduce
with reduced
and some
with a #(when (pred %) %)
, I wonder if there's a better way?
I benchmarked the different ways of doing this:
(def coll (seq (into [] (range 10000))))
(def pred #(< 1000 %))
(defn filter+first [] (first (filter pred coll)))
(defn some+when [] (some #(when (pred %) %) coll))
(defn eduction+first [] (first (eduction (filter pred) (take 1) coll)))
(defn reduce+reduced [] (reduce #(when (pred %2) (reduced %2)) nil coll))
Here's the mean execution times:
{:filter "12.65µs"
:some "22.15µs"
:eduction "21.16µs"
:reduce "7.14µs"}
(edit: wraps coll
in (seq ..)
as @doglooksgood's context was chunked seqs)I'm quite surprised that this works 🙂
(random.namespace.which.does.not.exist/.split "ahoj" "ah")
;;=> ["", "oj"]
Can anyone point me to some documentation regarding the behavior of the leinigen project.clj flag :exclusions
. I'm encountering a gordian knot of dependency conflicts between ring and datomic cloud and in the process of trying to fix it have realized I only vaguely understand what actually is being "excluded".
@U3RGL6XNF I'd try to exclude jetty-io
dependency if that's the one causing you problems (addes as a comment to SO question). You can't exclude everything, because datomic doesn't bring the complete ring-jetty-adapter
(not sure just guessing from what you've shown us.
Moreover, the simplest option can be to just add explicit dependency on ring-jetty-adapter
with proper version
Thanks @U06BE1L6T. I will try those suggestions. I've tried so many different options here that I've started to lose track.
Yes, dependency conflicts are often tricky to deal with (and hidden until you actually try to run your application)
Yeah immensely frustrating. I love writing in clojure but this stuff is 😡
@U06BE1L6T this is a github repo showing what I've been seeing. Running lein ring server
causes the dep error that I can't resolve. https://github.com/ezmiller/datomic-ring-dep-conflict
Actually here's a better illustration on a branch: https://github.com/ezmiller/datomic-ring-dep-conflict/tree/exlusions-from-datomic-cloud
It seems to happen only when Datomic Cloud's d/client
is called.
just in case, be aware of the typo :exlcusions
-> :exclusions
Where are you seeing that?
there: Can anyone point me to some documentation regarding the behavior of the leinigen project.clj flag :exlcusions
. I'm encountering a gordian knot of dependency conflicts between ring and datomic cloud and in the process of trying to fix it have realized I only vaguely understand what actually is being "excluded".
Oh sorry. I thought you mean in the code somewhere. sorry about the typo.
I was thinking that it may be in your code.
@U3RGL6XNF When I ran lein ring server
I got java.lang.IllegalArgumentException Cannot open <nil> as a Reader.
I don't have db-config.edn
you are referencing in your example so I cannot really try it.
This is the writeup of the problem I've hit (my initial "solution" turned out not to be one): https://stackoverflow.com/questions/50331264/ssl-doesnt-have-a-valid-keystore-error-when-trying-to-connect-to-datomic-clou
@petterik great work! which should I choice if the pred function is heavy. use reduce + reduced?
@doglooksgood the pred function will be run the same amount of times for all examples, so it shouldn't matter. I'd choose filter+first
or some+when
when performance doesn't matter because it's easier to understand. The reduce+reduced
when perf matters
say I have a collection of 3-tuples, what's a good way to map over that and apply a different function to each tuple element?
(map (fn [f x] (f x)) [f1 f2 f3] tuple)
?
mapv
is probably a better choice
ah, a collection of tuples?
(->> [[:a :b :c] [:d :e :f]]
(map (partial interleave [str name keyword?]))
(map (partial partition 2)))
but then it's hard to map apply
across because the fn and args are in one collection, end up having to map (apply (first %) (rest %))
(for [[a b c] tuples]
[(str a) (name b) (keyword? c)])
like that?it's a bit harder to parametrize this snippet by functions as well, but i think it's the easiest to understand
or did they want (for [[a b c] tuples f [str name keyword]] [(f a) (f b) (f c)])
the fn I'm ultimately writing will look like: (clean-collection [fn1 fn2 fn3] some-coll)
, where some-coll
is a collection of 3-tuples (in this case)
and what does the result look like?
does fn1 only get called on the first item of some-coll, all of them?
it's positional, so [[:a :b :c] [:d :e :f]]
will end up like [[(fn1 :a) (fn2 :b) (fn3 :c)] [(fn1 :d) (fn2 :e) (fn3 :f)]]
oh (map #(%1 %2) fns coll)
oh, for each coll
(for [coll colls] (map #(%1 %2) fns coll))
i like that one
there's a dirty trick where deliver
is equivalent to #(%1 %2)
but don't use that, it's an implementation detail of promise
ohh, the #(%1 %2)
idiom, that's the kind of thing I was missing … I remember that from stuff like
(condp #(%1 %2) "thing"
keyword? "keyword!"
string? "string!")
(for [coll [[:a :b :c] [:d :e :f]]]
(map #(%1 %2) [str name keyword?] coll))
=> ((":a" "b" true) (":d" "e" true))
I think map inside for is nicer than mapv partial mapv