Clojure - why is the "do" statement necessary for grouping? - clojure

I understand that a do block will execute statements in sequence and return the value of the last one. I'd thought that if I didn't need the return value the do would be unnecessary. Not considering the return value, then, I don't understand the different behavior of these 2 functions:
with "do":
(defn x []
(if true
(do (println "a") (println "b"))))
=> (x)
a
b
nil
without "do":
(defn x []
(if true
((println "a") (println "b"))))
=> (x)
a
b
NullPointerException user/x (NO_SOURCE_FILE:3)
What is the cause of the NullPointer in the second example?

Because
(function argument)
...is function-call syntax,
((println "a") (println "b"))
...expects (println "a") to return a function, which it can call with the result of (println "b") passed as its first argument.
Instead, (println "a") has no return value (returns nil, aka null), and trying to call this null value as a function gives you your NullPointerException.
By the way, the idiomatic thing to do here would be to replace your if with a when, which will expand to implicitly include a do:
(defn x []
(when true
(println "a")
(println "b")))

Related

Does def in clojure save the return value of a function or will the function get evaluated each time?

Let's say I define a function
(defn func [a] a)
Then I define
(def funcUsage (func 5))
If I now use funcUsage twice, does the function func get called twice or is the return value stored into funcUsage?
I.e
(println funcUsage)
(println funcUsage)
Would that be equivalent to
(println (func 5))
(println (func 5))
?
It seems like that in my program. Does def store the value of an evaluated function?
When you evaluate (def funcUsage (func 5)), func is called once and value 5 is bound to symbol funcUsage.
When you evaluate
(println funcUsage)
(println funcUsage)
you only print value of symbol, func is not called again. So, these calls:
(println funcUsage)
(println funcUsage)
(func is not called) and
(println (func 5))
(println (func 5))
(func is called twice) are not equivalent.
You can also test it, if you add some side effect for func like this: (defn func [a] (println "I am called...") a) or (defn func [a] (Thread/sleep 1000) a).
The def form creates a Clojure Var. Each Var is like a global variable that points to a value. In your case, (func 5) will be called once and the result is saved in the Var funcUsage.
You can find more information in this list of documentation. Be sure to see the Clojure CheatSheet and the books Brave Clojure and Getting Clojure.

A less cumbersome pattern for checking function parameters for nil in Clojure without throwing an assertion?

This was asked here, but the answers are all unacceptable.
I'm trying to apply some defensive programming techniques to clojure and I'm finding some things cumbersome.
Like checking function parameters:
(defn my-tolower [s]
(if (nil? s) nil
(.toLowerCase s)
))
Is there a cleaner way to do this?
I'm aware of :pre, but that throws an exception.
It seems you simply want some->, no?
(defn my-tolower [s]
(some-> s .toLowerCase))
(my-tolower "HELLO") => "hello"
(my-tolower nil) => nil
or just inline it without the wrapper function:
(some-> "HELLO" .toLowerCase) => "hello"
(some-> nil .toLowerCase) => nil
Since nil is falsey you could use when:
(when s (.toLowerCase s))
if you want the test you could use some? instead of nil?:
(if (some? s) (.toLowerCase s))
there are other aproaches too:
fnil, probably what I'd do
clojure.core/fnil
([f x] [f x y] [f x y z])
Takes a function f, and returns a function that calls f, replacing
a nil first argument to f with the supplied value x. Higher arity
versions can replace arguments in the second and third
positions (y, z). Note that the function f can take any number of
arguments, not just the one(s) being nil-patched.
provide a default value for a nil accepting fn. /
(let [f (fnil str/lower-case "")]
(f nil))
""
or catching the NPE
(let [f str/lower-case]
(try (f nil)
(catch NullPointerException ne nil)))
""
or just str
(.toLowerCase (str nil))
""
alternativly defprotocol and extend-protocol for nil maybe

Return the first non empty/blank value?

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"])
(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"]))
If you are so lucky to have "empty values" represented by nil and/or false you could use:
(or nil false "2" "3")
Which would return "2".
An equivalent to your JavaScript example would be:
(let [final (or path callback "")]
(println final))
If you want the first non blank string of a sequence you can use something like this:
(first (filter #(not (clojure.string/blank? %)) ["" "2" "3"]))
This will return 2
What i don't understand is your first example using the some function, you said that it should return nil but the first non blank string is "1".
This is how you would use the some function:
(some #(when-not (empty? %) %) ["" "foo" ""])
"foo"
(some #(when-not (empty? %) %) ["bar" "foo" ""])
"bar"
As others have pointed out, filter is another option:
(first (filter #(not (empty? %)) ["" "" "foo"])
"foo"
A third option would be to use recursion:
(defn first-non-empty [& x]
(let [[y & z] x]
(if (not (empty? y))
y
(when z (recur z)))))
(first-non-empty "" "bar" "")
"bar"
(first-non-empty "" "" "foo")
"foo"
(first-non-empty "" "" "")
nil
I used empty? instead of blank? to save on typing, but the only difference should be how whitespace is handled.
It was difficult for me to tell exactly what you wanted, so this is my understanding of what you are trying to do.
In my case, I wanted to find if an item in one report was missing in a second report. A match returned nil, and a non-match returned the actual item that did not match.
The following functions wind up comparing the value of a mapped value with a key.
Using something like find-first is probably what you want to do.
(defn find-first
"This is a helper function that uses filter, a comparision value, and
stops comparing once the first match is found. The actual match
is returned, and nil is returned if comparision value is not matched."
[pred col]
(first (filter pred col)))
(defn str-cmp
"Takes two strings and compares them. Returns 0 if a match; and nil if not."
[str-1 str-2 cmp-start-pos substr-len]
(let [computed-str-len (ret-lowest-str-len str-1 str-2 substr-len)
rc-1 (subs str-1 cmp-start-pos computed-str-len)
rc-2 (subs str-2 cmp-start-pos computed-str-len)]
(if (= 0 (compare rc-1 rc-2))
0
nil)))
(defn cmp-one-val
"Return nil if first key match found,
else the original comparision row is returned.
cmp-row is a single sequence of data from a map. i
cmp-key is the key to extract the comparision value.
cmp-seq-vals contain a sequence derived from
one key in a sequence of maps.
cmp-start and substr-len are start and stop
comparision indicies for str-cmp."
[cmp-row cmp-key cmp-seq-vals cmp-start substr-len]
(if (find-first #(str-cmp (cmp-key cmp-row) %1 cmp-start substr-len) cmp-seq-vals)
nil
cmp-row))

Clojure not nil check

In Clojure nil? checks for nil. How does one check for not nil?
I want to do the Clojure equivalent of the following Java code:
if (value1==null && value2!=null) {
}
Follow-up: I was hoping for a not nil check instead of wrapping it with not. if has a if-not counterpart. Is there such a counterpart for nil??
After Clojure 1.6 you can use some?:
(some? :foo) => true
(some? nil) => false
This is useful, eg, as a predicate:
(filter some? [1 nil 2]) => (1 2)
Another way to define not-nil? would be using the complement function, which just inverts the truthyness of a boolean function:
(def not-nil? (complement nil?))
If you have several values to check then use not-any?:
user> (not-any? nil? [true 1 '()])
true
user> (not-any? nil? [true 1 nil])
false
If you are not interested in distinguishing false from nil, you can just use the value as the condition:
(if value1
"value1 is neither nil nor false"
"value1 is nil or false")
In Clojure, nil counts as false for the purposes of conditional expressions.
As a result (not x) works actually works exactly the same as as (nil? x) in most cases (with the exception of boolean false). e.g.
(not "foostring")
=> false
(not nil)
=> true
(not false) ;; false is the only non-nil value that will return true
=> true
So to answer your original question you can just do:
(if (and value1 (not value2))
...
...)
condition: (and (nil? value1) (not (nil? value2)))
if-condition: (if (and (nil? value1) (not (nil? value2))) 'something)
EDIT:
Charles Duffy provides correct custom definition for not-nil?:
You want a not-nil? Easily done: (def not-nil? (comp not nil?))
If you want your test to return true when given false, then you need one of the other answers here. But if you just want to test that returns a truthy value whenever it's passed something other than nil or false, you can use identity. For example, to strip nils (or falses) from a sequence:
(filter identity [1 2 nil 3 nil 4 false 5 6])
=> (1 2 3 4 5 6)
You can try when-not :
user> (when-not nil (println "hello world"))
=>hello world
=>nil
user> (when-not false (println "hello world"))
=>hello world
=>nil
user> (when-not true (println "hello world"))
=>nil
user> (def value1 nil)
user> (def value2 "somevalue")
user> (when-not value1 (if value2 (println "hello world")))
=>hello world
=>nil
user> (when-not value2 (if value1 (println "hello world")))
=>nil
If you want a not-nil? function, then I'd suggest just defining it as follows:
(defn not-nil?
(^boolean [x]
(not (nil? x)))
Having said that it is worth comparing the usage of this to the obvious alternative:
(not (nil? x))
(not-nil? x)
I'm not sure that introducing an extra non-standard function is worth it for saving two characters / one level of nesting. It would make sense though if you wanted to use it in higher order functions etc.
One more option:
(def not-nil? #(not= nil %))

Single predicate to test for "self-evaluating" atoms in Clojure

At the home site of Clojure, there is the following statement:
Strings, numbers, characters, true,
false, nil and keywords evaluate to
themselves.
Is there a single combined predicate that tests for any of these, combining string?, number?, char?, true?, false?, nil?, and keyword?. Should I just use (complement symbol?)?
Maybe I'm missing something, but you could use the following to test for any of those conditions and return true if one is true:
(defn self-eval?
[x]
(or (string? x)
(number? x)
(char? x)
(keyword? x)
(true? x)
(false? x)
(nil? x)))
It's easy enough to write a macro that asks "does the given expression evaluate to itself". In fact this is a good example of tasks that can only be done with a macro because they need to see the argument both evaluated and unevaluated.
(defmacro selfp [a] `(= ~a (quote ~a)))
#'user/selfp
user> (selfp 1)
true
user> (selfp +)
false
user> (selfp [1 2])
true
user> (selfp '(+ 1 2 3))
false
While strings, numbers, characters, keywords, and the booleans are all self-evaluating, other things such as [1 2] are as well,so this may not be a useful test in general.
Another option is to create a function that uses a map:
(defn myclassifier? [x]
(let [types-I-care-about #{java.lang.Sring ...}]
(if (types-I-care-about (type x))
true
false)))
Another option which may have better performance is to use java's dynamism:
(extend-type Object
IMyClassifier
(myclassifier? [x]
(let [c (.getClass x)]
(if (types-I-care-about (type c))
(do
(extend-type (.getClass x)
IMyClassifier
(myclassifier? [x] true))
true)
false))))
where types-I-care-about is a set of types you care about.