This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-09-05
Channels
- # bangalore-clj (4)
- # boot (196)
- # chestnut (1)
- # cider (4)
- # clara (3)
- # cljs-dev (23)
- # cljsjs (28)
- # cljsrn (1)
- # clojure (79)
- # clojure-art (1)
- # clojure-berlin (1)
- # clojure-hk (17)
- # clojure-italy (5)
- # clojure-korea (1)
- # clojure-russia (21)
- # clojure-spec (5)
- # clojure-uk (26)
- # clojurescript (125)
- # core-async (1)
- # cursive (23)
- # datomic (7)
- # emacs (17)
- # hoplon (51)
- # jobs (2)
- # leiningen (2)
- # om (11)
- # om-next (26)
- # onyx (39)
- # pedestal (9)
- # proton (4)
- # re-frame (43)
- # reagent (1)
- # ring (2)
- # slack-help (12)
- # sydney (4)
- # test-check (9)
- # yada (40)
månin' y'all
hey folks - quick question. Is there a function like fnil
which wraps a function so it returns nil if the first parameter is nil?
I’m finding I have to do a lot of (map #(when % (foo %)) things)
- where foo
throws a NPE if I pass it nil, and things
includes some nils
I’d like something like map (when-not-nil foo) things)
where when-not-nil
would just short-circuit if a thing was foo.
(defn nilf
[f x & args]
(when x
(apply f x args)))
If it doesn't exist in core, this works for me on a quick test ^^thanks - yeah, it’s easy to write, just hoping for something idiomatic I’d missed 🙂
i suspect the more idiomatic thing is to have foo
not throw NPEs, usually by making heavy use of core fns
@adebesin - I want something that doesn’t call foo
if it would call it with a nil.
@glenjamin - true, but foo
is in someone else’s code 🙂 (and it’s a thin wrapper around Java code anyway)
@korny - in that example, what do you do with the leftover nils in the result collection?
I keep them - let the caller handle them. So if foo
were inc
then (map (nilf inc) [1 2 3 nil 4 5])
would return (2 3 4 nil 5 6)
ideally.
anyway, I have to get to a train - thanks for the thoughts folks, it’s really a trivial thing I can do myself, was just hoping for an idiomatic shortcut to save me some keystrokes
@korny: I've looked before. I reached same conclusion as you. This is why many utility libraries exist I guess 😃
=> (map #(some-> % inc) [1 2 3 nil 4])
(2 3 4 nil 5)