Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shaking nil from false in Clojure

Tags:

clojure

I want the following outcome: "1" -> true "0" -> false nil -> nil

How can this function be improved?

#(when-not (nil? %) (if %  "1" "0"))
like image 749
Odinodin Avatar asked Sep 06 '26 07:09

Odinodin


1 Answers

Your requirement translates directly into a map and maps are also callable as functions in Clojure, so {1 true, 0 false} is the function you want.

({1 true, 0 false} 1) ;;=> true
({1 true, 0 false} 0) ;;=> false
({1 true, 0 false} nil) ;;=> nil
like image 139
Michiel Borkent Avatar answered Sep 08 '26 05:09

Michiel Borkent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!