7 ms·
Clojure allows me to express thoughts that I wouldn't know how to express in for, while, if, variables and objects. And on readability, what do you prefer?
by j-pb 11y ago
Clojure allows me to express thoughts that I wouldn't know how to express in for, while, if, variables and objects.
And on readability, what do you prefer?
List<Integer> even = new List()
for(int i = 0; i < 100; i++){
if((i%2)==0){
even.add(i);
}
}
return even;
vs.
(filter even? (range 0 100))
- incepted 11y agoOf course, dynamically typed languages will win on one liners against Java but in the long run, the absence of types can really bring an entire code base (and project) down, which is why Clojure is hardly used at all and why Java is everywhere. Besides, Clojure doesn't even win in the one line department against modern JVM languages (e.g. Kotlin).
- arohner 11y ago> which is why Clojure is hardly used at all and why Java is everywhere. Surely that has nothing to do with Java being 20 years old, and Clojure 1.0 being 6 years old? > Clojure doesn't even win in the one line department against modern JVM languages (e.g. Kotlin). Example, please.
- incepted 11y ago> Surely that has nothing to do with Java being 20 years old, and Clojure 1.0 being 6 years old? Lisp has been around for 50+ years, hasn't really helped its popularity much. But I'm willing to bet with you that in 14 years from now, Clojure will have even fewer users than it has today. > > Clojure doesn't even win in the one line department against modern JVM languages (e.g. Kotlin). > Example, please. Here it is in Kotlin: (0 .. 20).filter { it % 2 == 0 }
- j-pb 11y agoI'd say clojure won that one liner.
- huahaiy 11y ago> Lisp has been around for 50+ years, hasn't really helped its popularity much. But I'm willing to bet with you that in 14 years from now, Clojure will have even fewer users than it has today. Computer science has too short a history to make any sound arguments from history. However, if you insists on arguing from history, let's look at the long term trend. Would you agree that the trend of programming language is progressing to higher and higher level of abstraction and further and further away from the machine details? From machine code, to assembly language, to C, to Java, and now FP languages, this trend of mainstream programming language evolution is very clear. In this sense, if you take Lisp as a pioneer of function programming, it was simply ahead of its time. If you are implying that Lisp syntax is somehow an impediment to its popularity. That may well be. It's a personal preference issue. Some people like more syntax in their language, some people like less. If you are in the later camp, Lisp is surely very attractive. For example, in your kotlin one-liner, there are just too much syntax to understand "()", "..", ".", "{}", etc. Life is too short, I just don't want to learn all these idiosyncrasy invented by someone just for the sake of being different. I wished I had knew Lisp sooner, certainly not having programmed for 20 years. I am sure you will lose that bet.
- dragonwriter 11y ago> Would you agree that the trend of programming language is progressing to higher and higher level of abstraction and further and further away from the machine details? That's...less than clear. The extreme high level of abstraction in PLs is consistently increasing, or at least non-decreasing, over time, but the mean/median used in actual programming may not be.
- huahaiy 11y agoWith some minor assumptions, my assertion would be correct. Yours needs a lot of strong assumptions to be mathematically feasible. Basically, you are saying, when the max of a sequence is increasing, the mean/median stays or even decreases. That would require that the frequencies on the lower end to increase much faster than that on the higher end, which is certainly not the case. For example, the number of C programmers is growing faster than the number of Java programmer today? No one would think that's correct.
- dkersten 11y agoI still find the clojure version (ever so slightly) simpler. Mostly because this has magic implicit it variable. In real code, this of course wouldn't matter, though.
- jiyinyiyong 11y agoThere will have fewer users according to Google Trend: https://www.google.com/trends/explore#q=java&cmpt=q&tz=Etc%2FGMT-8 https://www.google.com/trends/explore#q=java&cmpt=q&tz=Etc%2... And Clojure, not many is interested in it: https://www.google.com/trends/explore#q=clojure&cmpt=q&tz=Etc%2FGMT-8 https://www.google.com/trends/explore#q=clojure&cmpt=q&tz=Et... Actually I'm on the Clojure side. News comer will always pick a language which is simpler, more expressive, newer. Java is no longer such a language comparing to others.
- yeukhon 11y agoBut what is even? I am sure functional programming has its natural appealing in areas like recursion, but this example doesn't seem too convincing for winning readability, I can always write this in python filter(even, range(0, 101))) where even is a function I wrote. Please give an example for "Clojure allows me to express thoughts that I wouldn't know how to express in for, while, if, variables and objects." if you can.
- j-pb 11y agoWe were talking about java here. ^^ So python is offering a lot more already with it somewhat first class functions. What would you guess even? does, btw? But sure, let's make things a bit harder. As below, let's produce a sequence of the form [true false false true true true false false false false ...] of exact length 1000 without computing any more than exactly 1000 elements. (take 1000 (mapcat (fn [i] (repeat i (odd? i))) (range))) Of course the really interesting stuff is hard to do in one liners. But I think one of the more mind bending things is probably core.async. Go style coroutines and channels implemented as a library on top of the language. Which allows for concurrent programming inside clojurescript which is compiled to JS and thus inherently single-threaded. http://swannodette.github.io/2013/08/02/100000-processes/ http://swannodette.github.io/2013/08/02/100000-processes/
- yeukhon 11y agoMy point was "even" and "filter" can be built by someone, just to be fair about the whole readability. If "even" is already built into the language, then that's a given. Readability and language understanding are not separable. I for one have a hard time actually understand what the code does above (although I can guess it). But I am not sure how ^ translate into [true false false true true true false false false false ...] sequence because, I thought we were calculating oddity of 1000 numbers. [1 2 3 4 5 6 7 8 9 10 ...] ==> [T F T T F T F T T T ] Perhaps the lack of understanding Clojure syntax to comprehend what the code is doing.
- j-pb 11y agoIt's part of the std lib. But yeah you're basically on the same page as me. Java doesn't have the ability to be extended in the ways that clojure or python can. Python also allows for functional programming to a certain degree, but clojure is a lot more powerful when it comes to language extensions. Threads as a library (for js environments) for clojure. https://github.com/clojure/core.async https://github.com/clojure/core.async Logic programming for clojure. https://github.com/clojure/core.logic https://github.com/clojure/core.logic Declarative UI building, with dataflow semantics. http://reagent-project.github.io http://reagent-project.github.io Additionally immutability makes your life a lot easier. Parallel computing in clojure is a no brainer, you just change your map to pmap, and that's it.
- deleted 11y ago[deleted]
- toong 11y agoThat's not a fair comparison. What about: IntStream.range(0, 100).filter(i -> i % 2 == 0).collect(Collectors.toList()); Disclaimer: I'm not writing Java nowadays, so this is my quick prototype from a quick google-session, it probably won't compile :D Edit: looks like I'm second, my google-session took too long :-) Maybe bonus points for C#: Enumerable.Range(0, 100).Where(i => i % 2 == 0);
- pmelendez 11y agoTrue... although that is to me even less readable than the first Java version. The clojure version still wins by a large distance in my book because of its elegance.
- yogthos 11y agoThat's better, but Clojure still has less noise, and in my experience that adds up significantly in the long run.
- highCs 11y ago> IntStream.range(0, 100).filter(i -> i % 2 == 0).collect(Collectors.toList()); This has been added to java to catchup with functional programming languages.
- toong 11y agoI know those are functional concepts, but it feels more like trying to catch up with C# (LINQ) to me.
- skizm 11y agoOne hand picked example from someone who prefers closure, which only works because there is a keyword "even".
- deleted 11y ago[deleted]