Consider the following:
=> (even? (count []))
true
so far so good. Now consider (assume my-file is empty):
(odd? (count (str/split (slurp my-file) #"\|")))
true
err … why is the vector returned from an empty file not even (zero) ?
=>(str/split (slurp my-file) #"\|")
[""]
Ahh, can someone explain why an empty string is returned in this case?
I’m attempting to determine if there are an odd number of records in a file or not.
clojure.string/splitusesjava.util.regex.Pattern/splitto do the splitting. See this question about Java for explanation. Namely,splitreturns everything before the first match of your pattern as the first split, even if the pattern doesn’t match at all.The canonical way to test if a collection (list,array,map,string etc.) is empty or not is to call
seqon it, which will returnnilfor an empty collection.