There should be no significant difference between your code samples.
case Queue((thing, stuff), _*) actually translated by the compiler to a call to the head ( apply(0) ) method. You can use scalac -Xprint:patmat to examine this:
<synthetic> val p2: (String, String) = o9.get.apply(0); if (p2.ne(null)) matchEnd6(doThing(queue.tail))
The cost of head and the cost of headOption almost the same.
The head , tail and dequeue can call reverce in the internal List Queue (with a cost of O(n) ). In both code samples there will be no more than 2 reverce calls. You should use dequeue like this to get at most one reverce call:
def doThing(queue: Queue[(String, String)]): Queue[(String, String)] = if (queue.isEmpty) queue else queue.dequeue match { case (e, q) => doThing(q) }
You can also replace (thing, stuff) with _ . In this case, the compiler will only generate a call to lengthCompare without head or tail :
if (o9.get != null && o9.get.lengthCompare(1) >= 0)
source share