Posts
Erik's Ponderings
Cancel

Scala array comparison (without a PhD)

Scala collections and equality Scala collections follow a simple set of general equality rules1. Two collections are equal if: they are in the same overall category (Seq, Set, or Map) they c...

Map.map vs. Map.mapValues

I just hit a bug caused by a misunderstanding of how Map.mapValues works. Consider: val original = Map("a" -> 1, "b" -> 2) val modified = original map { case (k, v) => k -> (v + 1) } ...

Implicit conversion to the Unit type in Scala

In Scala, you have a few ways to express that a function returns the Unit type.1 A common way is to use the function syntax and specify a result type of Unit: def foo: Unit = ... Like for any re...

Getting to know CanBuildFrom (without a PhD)

Recently I needed to write a pretty simple function: given a sequence of (name, value) pairs, return a sequence of (name, some collection of all the values having that name) pairs. Here is a simple...

More iterators goodness

Following-up on the previous post about iterators, say we have a clean Scala API with Option instead: class Foo { def parent: Option[Foo] = … } def findBar(foo: Foo) = Option[Bar] = … It turns ...

Scala iterators and Iterator.iterate

There is a common programming pattern where you navigate a hierarchy in order to find an element satisfying a certain condition. I have written code like this in Java probably a million times: cla...

Scala tip: getOrElseUpdate

I found myself recently looking at the following code which I wrote when I was getting started with Scala: def getModelState(modelPrefixedId: String) = modelStates.get(modelPrefixedId) match { ...

Scala tip: import renames

In Java, you often write things like this, which are rather verbose: URLEncoder.encode(...) URLDecoder.decode(...) Since Java 1.5 you can use static imports, but then you are stuck with the orig...

Scala partial functions (without a PhD)

If you have done some Scala for a while, you know about pattern matching and match/case. Things like: value match { case Some(value) => … case None => … } But there is another use of t...

Continuations in Scala (without a PhD)

With @avernet we have been thinking lately about continuations, for a few reasons: Continuations pop up on the web as a concept that could help with event-based programming Scala has a contin...