3 ms·
Well, it's asking more, but not necessary a lot, especially in term of the thorough job this particular author did in the remainder of the article. Running thr
by efsavage 15y ago
Well, it's asking more, but not necessary a lot, especially in term of the thorough job this particular author did in the remainder of the article.
Running through elements in a list is obviously common, but that's not what was done here, he was checking for a particular validation that just so happens to have been easy in Clojure and messy in Java.
Here's an example that I think is a little more realistic, but not so complicated that it requires a lot of work. It doesn't use anything outside of the standard SDK, and yet it illustrates some i/o, data structures, error handling and return values, and useful logging, all in a pretty small amount of code that you could definitely see in a real project:
https://gist.github.com/975368 https://gist.github.com/975368
This is clearly not Java at it's best, the finally block shows how some basic scoping can get verbose, and the nested try/catch isn't great, but it's tough to improve it without refactoring some of the messy bits out, which would really only help with organization, not verbosity.
26 lines. I expect Clojure or most other languages to do it in fewer lines, but I'd be surprised if they can do it in half and still account for the requirements. My overall point here is that you could probably read a file into a list in some languages in a couple of lines, but when you start adding some reality in, they pack on some pounds.
If someone wants to prove me wrong, please do, you might have a convert :)
- cema 15y agoIt will be shorter, maybe not by half. But keep in mind that the "reality" you are talking about is in some aspects an artifact of the language in which the rest of the software system has been constructed. For instance, some edge cases (such as special treatment of nulls, some exceptions) happen to be less important in Clojure and other "Java.next()" languages. I do hope someone will post an equivalent Clojure program. Unfortunately, my knowledge of Java is limited, and I have not touched Clojure in many months now, so cannot do it myself. Sorry.
- mkoch 15y agoHere's my stab at it: https://gist.github.com/976020 https://gist.github.com/976020 I think the biggest advantage of Clojure over Java for code like this is how easy it is to use lazy sequences and higher-order functions. In this example there isn't much to be gained in the error-handling and logging; however, if there were other similar functions it might be easier to factor out some of the common patterns in Clojure, since functions like the predicate to ignore a line can be easily passed through. Or, for example, you could write a custom version of with-open or line-seq to do the desired logging behind-the-scenes.
- efsavage 15y agoThanks, that's a very helpful illustration and gives a good idea of where it ends up. It's actually more readable than most of the other examples I've seen which I think err on the side of being too simple or too clever.