Last active
December 2, 2020 06:37
-
-
Save borkdude/533beb1b833828092c35 to your computer and use it in GitHub Desktop.
Clojure vs. Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Java pre version 8: | |
public class StringUtils { | |
public static boolean isBlank(String str) { | |
int strLen; | |
if (str == null || (strLen = str.length()) == 0) { | |
return true; | |
} | |
for (int i = 0; i < strLen; i++) { | |
if ((Character.isWhitespace(str.charAt(i)) == false)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} | |
Java 8: | |
boolean allBlank(String str) { | |
return str.chars().allMatch( c -> Character.isWhitespace(c)); | |
} | |
Clojure: | |
(defn blank? [s] (every? (fn [c] (Character/isWhitespace c)) s)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Re Clojure version, I think this could be simpler, like this: