r/Clojure • • Sep 04 '25

Is this a weird way to solve 4clojure #21 Spoiler

This seems pretty different than the most of the other solutions listed, and I was curious if I'm thinking about things the wrong way as someone who's coming from more of a procedural programming background.

12 Upvotes

16 comments sorted by

View all comments

Show parent comment

1

u/Traditional-Eye-1905 Sep 04 '25

Do you have an example of something that makes this distinction clear? I've read this before, and don't think I really get what Rich is trying to say. Why isn't the approach taken with 'nth' in the same category as "using polymorphism to get better performance by leveraging some implementation specifics"? Couldn't leveraging implementation specifics also transition between different algorithmic complexities, depending on the implementation?

3

u/alexdmiller Sep 04 '25

The big picture is that the op itself should give you some performance expectation that is true for all collections that it works on. A different school of thought (see Java) is to fill out the matrix of impls vs ops regardless of perf characteristics.

Most ops in the library fall into complexity classes of sublinear or linear time. nth ideally should imply indexed access and at least sublinear if not constant time access. However, it is implemented for sequential (linear time) collections as well. This blurring of classes is not done in many other places. Generally seq ops may require walking a seq so your expectation for all of those should be linear time average case. Anything working on keys of maps/sets/indexed should be sublinear (generally effectively constant for hashed colls and log time for sorted colls).

contains? is probably an illustrative example - this is "fast" on map keys and sets but cannot generally be implemented in sublinear time on sequential colls, so it isn't.

3

u/Traditional-Eye-1905 Sep 04 '25

I think I understand.

So the idea is that, just because the implementations are polymorphic, doesn't mean the "interface" the function "belongs to" (in a Java sense) has to be so broad.

Using the contains? example: that function is meant to act on lookupable things, not every possible collection (or seqs in general).

It's still polymorphic in impl because vectors and sets and maps all have a different (efficient) way to perform that operation, but if you've lifted that data structure to the seq abstraction, then you're no longer able to use contains? in the same way that in Java, if you treat a Set as an Iterable, you can't do much more than iterate over its elements.

nth arguably should have been more narrowly defined as an op that works only on things that have a notion of indexes, and if you wanted to get the nth element of a seq, then that should explicitly be done via things that make sense for seqs, like drop and first or something (which makes it very obvious that you're getting linear performance because you're doing a linear thing)

Have I got it right?

2

u/daveliepmann Sep 04 '25 edited Sep 05 '25

(Edit: see Alex Miller's answer.)

Rich calls the google groups discussion this was lifted from "exhausting" but it has some helpful context.

I think Rich is saying nth is in that category, but it's a member of the subcategory that is usually a bad idea. It's a counterexample because implemented it anyway and has mixed feelings about it.

My understanding of this topic isn't strong enough to fire off a comprehensive explanation. There are multiple intersecting concerns, among them that it's nice to have a seq API, it's nice for functions to be maximally performant on any accepted input, that's substantial work and complicates the seq API, and a major lesson from Common Lisp history was that functions that dramatically changed performance characteristics (i.e. different algorithmic complexity) based on "small" changes in inputs (whether size or type) was a major pain when maintaining and refactoring code.