Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return the first non empty/blank value?

Tags:

clojure

I have 2 bindings I'm calling path and callback.

What I am trying to do is to return the first non-empty one. In javascript it would look like this:

var final = path || callback || "";

How do I do this in clojure?

I was looking at the "some" function but I can't figure out how to combine the compjure.string/blank check in it. I currently have this as a test, which doesn't work. In this case, it should return nil I think.

(some (clojure.string/blank?) ["1" "2" "3"])

In this case, it should return 2

(some (clojure.string/blank?) ["" "2" "3"])
like image 402
Geuis Avatar asked Sep 06 '13 18:09

Geuis


1 Answers

(first (filter (complement clojure.string/blank?) ["" "a" "b"]))

Edit: As pointed out in the comments, (filter (complement p) ...) can be rewritten as (remove p ...):

(first (remove clojure.string/blank? ["" "a" "b"]))
like image 61
Alex Avatar answered Oct 01 '22 01:10

Alex