I have a function that wants to pull a value out of a map based on a prioritized order. Currently I'm doing it as a nested if structure which is horrible. I have to believe there is a better way.
While this works is there a better way?
(defn filter-relatives [relatives]
(if(contains? relatives :self)
(relatives :self)
(if(contains? relatives :north)
(relatives :north)
(if(contains? relatives :west)
(relatives :west)
(if(contains? relatives :east)
(relatives :east)
(relatives :south)
)
)
)
)
)
)
)
(some relatives [:self :north :west :east :south])
What about:
(defn filter-relatives [relatives ordered-filters]
(first (filter identity (map relatives ordered-filters))))
Sample run:
user=> (filter-relatives {:a 1 :b 2 :c 3} [:z :b :a])
2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With