r/Clojure • • Aug 10 '20

Counting elements

Post image
112 Upvotes

17 comments sorted by

View all comments

0

u/[deleted] Aug 10 '20

[deleted]

1

u/clickrush Aug 11 '20

I get what you are saying but I don't think it is!

Clojure programmers are used to a vocabulary of generic data-manipulation from clojure.core.

Yes, you could wrap this in a function yourself but that is kind of besides the point. A Clojure programmer would either directly understand the first example or put the function in their knowledge-/tool-box. However everyone wrapping the second one would require every reader to read the specific documentation/source of that function rather than a general one.


However I say this completely ignoring the fact that there also might be a function/method that does the same in Java. Java has become more expressive and elegant since a while now. I'm personally out of touch with modern Java but it is not unusual that people criticize things that are not idiomatic or even true anymore.

10

u/stingraycharles Aug 11 '20

You’re missing the point. The point is that, perhaps coincidentally, frequencies is a function in the Clojure standard library, while it’s not in Java.

A more honest comparison would be to compare an algorithm that’s neither available in Clojure nor java streams and compare the implementations of those. It’s not even hard to show how much better Clojure is in these circumstances, so it’s completely unnecessary to do it this way.

1

u/LammdaMan Aug 12 '20

I agree with you to a point. This is comparing "apples and oranges" - a "built-in function" vs. "roll-your-own".

But I also think that in comparing two languages, two of the things that matter to me are (1) What is built-in?, and (2) When some functionality isn't built-in, how hard is it to create, and how nicely packaged is the result likely to be?

This example may bundle two separate things in a "misleading" manner, but at a practical level, both of those things matter.

Clojure is a nice language for creating and cleanly packaging "roll-your-own" solutions. But it also has a very nice, well designed, collection of built-in functionality in the core library.

I find plenty of times when the functionality provided by 'frequencies' is useful.

If I were reading Clojure code someone else wrote that used 'frequencies', I would quickly and easily understand what that line did.

If I were reading similar code in Java, it would either be multiple lines of somewhat harder to read code, or it would be a call to some custom function "similar to frequencies". But then I wouldn't be sure what that function did. Is there good documentation for it? Does it correctly handle "all the corner cases"? Is it correct at all? If I wanted to be certain of those things, I'd need to read the source of that custom function.

2

u/stingraycharles Aug 12 '20

Maybe, but I still believe it’s a highly cherry-picked example. I could probably cherry-pick some Python code as well, with a Python built-in function which is a bit more convoluted to express in Clojure.

I guess I’m just not a fan of this kind of stuff, I think Clojure shines at managing complexity, which only happens at scale. Showing off “we have this function built-in!” hardly provides any value to me, as I could as well wrap it in a Java function and call it a day.