This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
- # admin-announcements (19)
- # announcements (1)
- # beginners (14)
- # boot (244)
- # cider (2)
- # clojure (23)
- # clojure-dev (23)
- # clojure-poland (55)
- # clojure-russia (118)
- # clojure-uk (4)
- # clojurescript (143)
- # core-async (31)
- # core-logic (1)
- # cursive (30)
- # datascript (2)
- # datomic (3)
- # emacs (7)
- # hoplon (40)
- # ldnclj (8)
- # off-topic (2)
- # om (64)
- # reagent (10)
- # ring (1)
- # yada (71)
Is there already a core function that does this?
(defn when* [f] #(when % (f %))) (map (when* count) [nil "foo" nil "quux"]) => (nil 3 nil 4)
@spei: count is just an example, of course. my code needs to do something on each of the elements of the input collection, but that thing can only be done if the elements are valid. then it needs to return the results in the same order as the input, with nil for invalid inputs.
@ghadi: yeah, definitely related. maybe
fwhen would be a better name for my utility function.
its operating assumption is to change the source data to a zero value, rather than changing the function
without going into too much detail, I have this:
(defn get-scored-intent [component psn urls] (->> (map #(when-not (intent-blacklisted? %) %) urls) (map (when* (partial tag-intent psn))) (map (when* p/make-raw-json-document)) (#(p/multi-get (:intent-bucket component) % :timeout 500)) (map (when* p/content))))
so blacklisted URLs are replaced with nil, and the rest of the stuff passes the nil through.
Are you just wanting to apply a function to entries in a collection if a predicate returns true? This all seems rather complex for such a simple use case. Have a
defn that contains the predicate and use it in the anonymous function in the
i agree because your functions look almost exactly like the way you would write them with normal "when" anyway
when* just makes it a little less messy.
(map (when* p/make-raw-json-document)) instead of
(map #(when % (p/make-raw-json-document %))). I agree that it's maybe not worth it when there is not already a
fwhen or something like that.
@akiva thanks. @ljosa I would've written it about the same way but the anonymous function in
map would've been
(fn [x] (when x (f x)), which doesn't look bad. perhaps refactoring your functions to include a check is better.
@ljosa: Yeah, I can see that. But it’s one of those things where if it aids in clarity, it might be worth the extra abstraction.
And, on top of that, it’s also an issue of not spinning off a verb if it’s not used a lot. Otherwise, an anonymous function or a
letfn might be better. Otherwise, everyone’s going to have to look to see the difference between
then your map function is just
(defn make-raw-json-doc [x] (when x (do-something x))
(map make-raw-json-doc coll)
You could take it one level up by making a flexible higher-order function (such as a
do-if) but then
comp around it to give it a specific name like
make-raw-json-doc that wraps a predicate and action.