This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-09-03
Channels
- # admin-announcements (241)
- # beginners (53)
- # boot (134)
- # cider (20)
- # clara (3)
- # clojure (170)
- # clojure-argentina (13)
- # clojure-brasil (1)
- # clojure-canada (3)
- # clojure-italy (9)
- # clojure-nl (3)
- # clojure-russia (55)
- # clojurescript (115)
- # code-reviews (18)
- # cursive (8)
- # datomic (14)
- # events (8)
- # hoplon (51)
- # immutant (38)
- # jobs (8)
- # ldnclj (11)
- # melbourne (6)
- # off-topic (2)
- # om (5)
- # onyx (9)
- # re-frame (3)
- # reagent (8)
- # sneer-br (1)
- # sydney (1)
- # testing (14)
is there a clojure core function that finds elements inside an array? Something like (find-in coll pred)
I implemented my own, but I feel as if there is something in core that I am overlooking:
(defn find-in
"Finds an item in a collection.
It returns the first item whose pred returns true.
The predicate function receives the current item in the iteration."
[pred coll]
(reduce (fn [acc x]
(if (empty? acc)
(if (apply pred x)
x
acc)
acc))
{}
coll))
(comp (first (filter))
I think this can be more efficient if I use recursion, that way it exists as soon as it finds a result
Yeah instead of building up a list
You could use a loop
I guess when you find your value you break instead of calling recur
my solution:
(def my-list [{:name "A" :items [1 2 3]} {:name "B" :items [4 5 6]}])
(defn find-one
[pred coll]
(loop [acc {} colls coll]
(if (empty? colls)
acc
(if (apply pred [(first colls)])
(first colls)
(recur acc (rest colls))))))
(find-one #(= "A" (:name %)) my-list)
Do you need the "apply"?
Not 100% sure, but since filter returns a lazy seq wouldn't calling first on the result of filter only really calculate the function until the first result?
Since it's only going to be a single map I think you can remove it - if it makes comprehrension better
@nikp: you mean would it always only return the first one?
I don't think so cause you're rebinding the loop to the (rest coll)
@teamaverage: I think he is referring to the more simpler (comp first filter)
Oh right
Hi can anyone explain to me how to use Hiccup when inserting an image into the html? This is the function (image attr-map? src alt)
so my suggestions of (comp first filter) wouldn't work
I'm having trouble understanding what attr-map? is.
not sure how the {:class "img"} part works as doing a class is usually {:.img}
Hrm well I'm not sure as to the lazyness cause (filter odd? (range 10))
gives me the full list
e.g. 1 3 5 7 9
I would have thought it would have given me nothing until I tried to pull from that list
@teamaverage: I think filter works in chunks
aaaaaah that rings a bell
Chunk size of 32 or something right
So if I've got the above code and what you say about chunks is true but my list is greater than the chunk size, how do I get the rest of the list?
@teamaverage: don't worry about it - that's an underlying implementation detail that clojure takes care of
hell, I had to add a doall
the other day to avoid laziness where I needed side effects to be resolved
@meow: I try not to, just curious is all : )