79 points · by bozhidar · 15y ago · 37 comments
public boolean hasUpperCase(String word) {
if (word == null) {
return false;
}
int len = word.length();
for (int i = 0; i < len; i++) {
if (Character.isUpperCase(word.charAt(i))) {
return true;
}
}
return false;
}
Where it should be this: public boolean hasUpperCase(String word) {
return word != null && word.matches(".*[A-Z]+.*");
}
It casts doubt on the other examples. I'm not saying Java isn't a verbose language that can get tedious, but let's use some decent examples. (defn has-uppercase? [string]
(some #(Character/isUpperCase %) string))
This code can deal with any sequence of characters: String, Array, PersistentList, PersistentVector, Cons, LazySequence, etc. Your example only deals with String.Whether or not that is truly representative of java code is perhaps another question but there is certainly a body of similar accepted code out there eg http://commons.apache.org/lang/api-2.5/src-html/org/apache/c....
http://www.jarvana.com/jarvana/view/org/apache/axis2/axis2-k...
But i don't know whats less performant, your reimplementation with regexes or the clojure sample.
The Regex version, while similarly terse would be massively slower.
I agree the example is a bit odd, but it does serve the purpose of comparing equivalent code, which I believe is the whole point.
It does do the job of showing that simple (and by inference (?) not so simple) algorithms can be more succinctly expressed in Clojure than in Java. (not that that is particularly difficult)
> It does do the job of showing that simple (and by inference (?) not so simple)
> algorithms can be more succinctly expressed in Clojure than in Java. (not that
> that is particularly difficult)
My point was not that Java is/can be as concise as Clojure (or Ruby or Python or some other version of Java or whatever) but that if you are focusing on comparing things, don't use examples that are so easy to write off. It is not clear to me, as a person experienced with Java but not with Clojure, that the Clojure method deals with many types of character sequences, nor is it clear to me that it's faster or slower than a regex match. If the argument solely lies on lines of code, my mind will think "is he comparing code optimized for performance from one language to code optimized for conciceness from another?" Is that ability to traverse sequences some bona-fide intelligence on the languages part or just a mess of instanceof expressions that really only work for a few expressions?This just struck me because I've been evaluating a lot of languages lately (Clojure is up soon) and it's frustrating to wade through people talking about languages using examples that are ignorant of all but their own, or contrived solely to make a point. Demonstrate the language in how powerful it is, how efficiently it runs, how gracefully it handles errors, how many lines of code it takes to solve specific problems, not standard utility ones that could easily be tucked into some library somewhere and it doesn't really matter if its 2 lines or 20.
class Person {
String name;
int age;
Person(String name, int age) {this.name = name; this.age = age}
}
This is still more verbose than the (defrecord person [name age]) but still, I agree with the dodgy examples used.Disclaimer : I don't know Clojure or similar and therefore I could be missing something. I did a quick search for defining private attributes in Clojure but didn't find any good hits.
That said, I'm really excited about Clojure. I got back into programming via Ruby and now Clojure is getting me back into data structures.
Nobody writes code to see if a string has an uppercase character, that code was written long ago. Regex may be something that Java imported, but it's right there in the JDK so I have no reason not to use it. How do the two languages look when you're validating email addresses against a whitelist of domains from a config file or URLs for parameters based on some business rules? How do you handle an I/O error when you're halway through writing a file? These are the small problems we know we'll have to do over and over again, but not be able to stash in a standard library (or use someone else's). Pointing out that JavaBeans are silly doesn't hold much weight either, I use lombok :)
I've seen MUCH better writing here on HN.
But hey, downvote away.
public boolean hasUpperCase(String word) {
if (word == null) {
return false;
}
int len = word.length();
for (int i = 0; i < len; i++) {
if (Character.isUpperCase(word.charAt(i))) {
return true;
}
}
return false;
}
or
public boolean hasUpperCase(String word) {
if (null != word)
return any(charactersOf(word), new Predicate() {
public boolean apply(Character c) {
return isUpperCase(c);
}
})
else
return false;
}I believe Kawa is also currently a bit friendlier on Android as Clojure has some overhead. (More at http://dev.clojure.org/display/design/Android+Support)