weavejester / medley

A lightweight library of useful Clojure functions
Eclipse Public License 1.0
870 stars 67 forks source link

Add ?? macro. It's like `clojure.core/or` but for nil's. #58

Closed darkleaf closed 2 years ago

weavejester commented 3 years ago

Thank you for the patch, but I think this is too specific for Medley.

anthonygalea commented 3 years ago

@darkleaf Not sure what your use case is but have you considered:

(m/find-first some? ...)

If I'm reading your test cases correctly I think the only case the above doesn't cover is:

(is (= :ok (m/?? :ok (throw (ex-info "Fail" {})))))`
darkleaf commented 3 years ago

@anthonygalea I want to use ?? to set up lazy defaults:

(fn [x y z]
  (m/?? x (costly-get-x y z) (throw (ex-info "Can't get x" {:y y, :z z}))))

(fn [x y z]
  (m/?? x (costly-get-x y z) default-value))

(m/?? (get-in x [...])
      (my-func ...)
      (get-in y [...])
      (throw ...))