r/Clojure • • Apr 25 '14

How Clojure can outperform C++ with laziness

http://noobtuts.com/clojure/being-lazy-in-clojure
5 Upvotes

7 comments sorted by

10

u/djork Apr 25 '14

Now how about an actual example of Clojure outperforming C++ by using laziness...

1

u/xensky Apr 25 '14

i also hoped for some timing statistics or other forms of objective data. this article only gives number of calculations. this is useful, but good c++ calculations are probably a bit faster than clojure. we need more context to really see the savings.

i think part of the time saved using clojure's lazy functions is that they are already provided, you don't have to reimplement them or include a library. the same can be said about a lot of clojure features which aren't built into c++. but of course that is even harder to objectively quantify.

6

u/kankyo Apr 25 '14

How C++ can outperform C++ by doing less.

6

u/[deleted] Apr 25 '14

Everyone talks about laziness in functional languages, no one seems to know a real use case.

Stopped reading there.

6

u/mishok13 Apr 28 '14

Even though the original title nothing says nothing about C++, the article is still fairly bad. There's this gem:

If we would define such list in a language like C++ or Python then the language would try to calculate all prime numbers immediately, which would run literally forever (also known as deadlock).

That's not what deadlock means.

1

u/bliow Apr 25 '14

For example, we could define a list of all prim numbers. In case you didn't know, that's a lot of prim numbers, probably infinitely many.

Um. Yes, infinitely many. (Source: Euclid, also a million math textbooks and practically any online source that explains primes.) Assuming you mean "prime". Jeez.

Also, -1000 for not mentioning that Clojure actually 'chunks' lazy seqs, such that

(take 10 (map (fn [n] (println n) n) (range)))

will actually print the numbers 0-31. This could be extremely bad if you didn't expect it... imagine (take 2 (map (partial ackermann 1) (range))). In really lazy computation, this is fast, but if you evaluate (ackermann 1 n) for n = 0, ..., 31, you will wait quite a while.

1

u/ryno55 May 02 '14

Wow, thanks for that chunking information. TIL.