And this example is suppoed to support Clojure's readability?
You'd btw probably write it as.
(->> (range)
(map #(repeat % (odd? %)))
(apply concat)
(take 1000)
If you want it more readable.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.
In Clojure, readability is obtained through standardization. Every Clojure programmer instantly knows what the functions in the standard library do. Because there's not much syntax in a Lisp, code is very easy to read once one knows the functions.
"filter" is incredibly useful, and most modern languages (including Java 8) have it built in.
"even?" is sometimes useful, and languages are kind of hit-and-miss about it, but its usually small enough to inline, and the biggest cost of not having it built-in is probably things like people using modulus rather than first-bit testing in Java which, IIRC, produces somewhat slower code for that check.
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
Logic programming for clojure. https://github.com/clojure/core.logic
Declarative UI building, with dataflow semantics. 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.
I have always wanted to look at building something again in one of the popular FPLs.
So, again, python:
[i % 2 == 0 for i in range(1000)]
(If you insist on Java, its fairly straightforward, though not a one-liner AFAIK, using Java 8 generators.)To compare it with what your code produces.
[true false false true true true false false false false ...]
[true false true false true false true false true false ...]
Edited for more clarity.
from itertools import chain, repeat
chain.from_iterable([repeat(i % 2 != 0, i) for i in range(1,1000)])
In Java 8, I think the solution with streams would be fairly straightforward, something like: IntStream.range(1,1001).flatMap(i -> Stream<Boolean>.generate( () -> (x & 1) != 0 ).limit(i))So, I think I need to take a break from comment-box programming for a while; its clearly not working out for me today.
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.
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/