Friday, July 8, 2011

Scala Collections Rant

Scala already has knocks against it for being confusing. So, I'll just kick it while it's down.

My latest frustration is with the mess that it calls Collections.

All I wanted to do was remove an item from a list. A very simple operation in just about every other programming environment ever created. I understand and appreciate that removing an item from an immutable collection should return a new list. In fact, that's exactly what I wanted to do.

So, I dug a little deeper in the Scala doc for List. But wait which List? Immutable List, which is just List. Mutable? Parrallel List? DoublyLinkedList? ListBuffer? Ok, this isn't so bad, I understand there are needs for all these.

So, lets just stick with Immutable List. Which is just called List. Even though Mutable List is called MutableList. Lets over look that too. For now.

OK. Open the Scala Doc for ImmutableList, I mean just List.

Ah, there's my remove method. But wait it's deprecated. Hmm. OK, maybe I'll use a MutableList. But wait, there's no remove method. What, no remove method on a MutableList? That's strange. Maybe its named something else? Nope. Maybe its an operator? Nope. I can contains, get, and indexOf on a MutableList. I can + an element onto a MutableList. But I can't - an element? I can also add items with + :+ +: +=: += . But not one operator to remove? What's up with that? Are these guys high on monads?

Ah, then there is a handy suggestion to use the filter function to remove the item I don't want. Good idea, except maybe I want to remove a specific item at a specific spot. Sounds bad doesn't it.

But all this functionality IS available. I was just looking at the wrong List. Its in ListBuffer. Whew. I guess I'll use a ListBuffer then. But now, I've just lost my the ImmutableLikeness of my List.

Or maybe I could have used an Immutable ListSet, just ListSet. But there is a possibility that I might want duplicates in the List. So I want a remove(index) function just like the measly old Java Util List. Which by the way must be jealous that it only has 20 methods vs 250 methods for the Scala List on steroids.

I could roll my own function. Or use some combination of take, head tail, etc. Or filter. But come on, its just remove.

So, that lead me to think about the API. List and MutableList I thought would basically have the same interface. Right? ... Well, right?

This discussion could get really long now.

A List is a LinearSeqOptimized, Product, LinearSeq, LinearSeq, LinearSeqLike, Seq, Seq, SeqLike, GenSeq, GenSeqLike, PartialFunction, Function1, Iterable, Iterable, IterableLike, Equals, GenIterable, GenIterableLike, Traversable, Immutable, Traversable, GenTraversable, GenericTraversableTemplate, TraversableLike, GenTraversableLike, Parallelizable, TraversableOnce, GenTraversableOnce, FilterMonadic, HasNewBuilder, AnyRef, Any.

And a MutableList is a Serializable, Serializable, Builder, Growable,
LinearSeqOptimized, LinearSeq, LinearSeq, LinearSeqLike, Seq, SeqLike, Cloneable, Seq, SeqLike, GenSeq, GenSeqLike, PartialFunction, Function1, Iterable, Iterable,
IterableLike, Equals, GenIterable, GenIterableLike, Traversable, Mutable, Traversable, GenTraversable, GenericTraversableTemplate, TraversableLike, GenTraversableLike, Parallelizable, TraversableOnce, GenTraversableOnce, FilterMonadic, HasNewBuilder, AnyRef, Any

That must explain the difference. Wait what.

Among 31 other things, a List is also a Function1? And a List is a HasNewBuilder? Isn't HasNewBuilder a question? Shouldn't that be NewBuilderLike? Maybe Buildable?

A MutableList is a Growable. But not a Shrinkable? Heresy.

And when did -like replace -able? Is this the new Valley Girl Moon Unit Zappa naming convention?

And it opens the possibility for the Likeable trait.

I really enjoy the name TraversableLike.

Why not use -ish? Then we can have TraverableLikeIsh.

Maybe throw in an -ly occasionally too?

So a List is TraverableOnce. Doesn't that mean the second time I try to loop over it. My code will fail?

Sausage, Politicians.

Now lets add Scala Collections.