HACKER Q&A
📣 scotty79

Do you know any computer language where collections are unified?


In most languages creating array, list, set, mapping, usually requires different code to create them and to access them.

Do you know any language where you create generic collection and just specify what you require of it?

For example, you might specify that you require collection to be indexed with numbers, and have quick access to elements by their index. Or have ability to add element at the end quickly or have the ability to find next element quickly. Or have elements with quick parent access, or quick children access, or quick arbitrary inserts or you want collection to be indexed multidimensionally.

Language itself would pick the right algorithms for your collection for example picking fixed length immutable array (for the purposes of iteration), but also set (for quickly checking presence of element).

Creation, access and writing should be kept as similar as possible with different algorithms.

This system should of course be extensible. So that you could implement fast access to elements by creating and updating various task specific indexes into your data and have them available though built in language syntax.

The only thing that comes to my mind that somewhat attempted this is C++ template library, which offers various collections and iterators while trying to keep the interface mostly algorithm agnostic.


  👤 thesuperbigfrog Accepted Answer ✓
Clojure sequences (https://clojure.org/reference/sequences) provide some of what you are looking for:

"Clojure uses the ISeq interface to allow many data structures to provide access to their elements as sequences.

The seq function yields an implementation of ISeq appropriate to the collection.

Seqs differ from iterators in that they are persistent and immutable, not stateful cursors into a collection.

As such, they are useful for much more than foreach - functions can consume and produce seqs, they are thread safe, they can share structure etc."

Seqs do not handle the "here is my requirement, pick a collection for me" feature you want, but rather "here is a collection, run this algorithm"


👤 dataangel
In Rust in theory you could express each capability a collection can have as a trait, impl the relevant traits for each container type, then write generic code that specifies it requires e.g. `NumberIndexable + FastAppend`. But AFAIK there is no way to ask the compiler to find the best available container type satisfying those traits; it would enforce anything you pass into the function satisfies them but wouldn't deduce it for you.

You could definitely do it with C++ template metaprogramming and type traits.


👤 mooreds
PHP lets you treat arrays and hashes exactly the same (or at least it used to) but I don't know if it switches the underlying data structures/algos based on usage.

👤 AnimalMuppet
There's a few different aspects to your question.

> For example, you might specify that you require collection to be indexed with numbers...

You can't hide that, because in at least some of the uses of that collection, you're going to index it by numbers.

Other than that kind of detail, though, I think you can create collections that you can use without knowing the details in Haskell. To a lesser degree, you can in C++.

The last detail is creation. You might be able to create a "collection factory" to create it for you. But I'm not sure that it's any different to say "CollectionFactory.createCollection(FAST_FIND)" than it is to say "new HashMap()". I mean, the former means that you don't have to know that a HashMap is the collection you're looking for, so that's something. It's not all that much, though.


👤 Noe2097
Surprisingly, one "language" actually fits pretty well (most of) your requirements: SQL :)

👤 paxswill
Core Foundation (Apple’s C library) sort of uses this with its “array” type: https://ridiculousfish.com/blog/posts/array.html

👤 rurban
Lua and PHP as the biggest ones. They both unified tables and arrays.

But you can do that with every library by yourself.


👤 ttymck
I'm probably wrong, but I feel like the Scala standard library is pretty close to what you are describing.

👤 robbedpeter
Lua metatables might fit the bill.