r/ProgrammingLanguages • • Sep 04 '26

What are your favourite data structure operations?

My language workbench is to the stage where I can implement new operators and overloads very quickly, and I've started stealing syntax I like from JavaScript and C++.
Since I have one universal data structure, I can just add all the new operators to it.
I wanted to get some more good ones though, from different fields, different languages.
Oh and if you want to suggest something just as a challenge I might give it a shot and show you how I do it.

Edit: data structure was the wrong word, I have one data structure substrate/storage model that I am currently furnishing one major aspect of, it's not the one universal data structure, more like my swiss army knife API over the memory model.

10 Upvotes

35 comments sorted by

View all comments

21

u/B_A_Skeptic Sep 04 '26

reduce, because you can make map, flatmap, and filter out of it.

8

u/ApothecaLabs Sep 04 '26

I suppose since the question says 'data structures' I'll just agree quietly, with a footnote for flavor:

This statement doesn't necessarily hold true in general - in Haskell, map ('Functor') and reduce ('Foldable') are separate type classes, because there can be infinite types that are mappable but not reducible, such as the the list of all positive integers which is mappable but not summable, and there are reducible types that are not mappable eg such as contravariant types. In general, to make map out of reduce, you need to assume the data type is constructible, which to be fair, is most plain old data types in most languages.

3

u/Background_Class_558 Sep 05 '26

that's the literal recursor of the type. you can make anything with it for that reason, it's sorta its "core".

2

u/ExplodingStrawHat Sep 05 '26

Yup! In fact (assuming purity and all), every function out of the type factors uniquely through said recursor.

1

u/Potato871 Sep 04 '26

Oh? I'm going to have to look into that one, I just did filter by iterating backwards and removing at the index if it matched.