Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verbose notation when using map() on Lists of tuples

Tags:

I like to have my code quite naively readable.

If I set up a simple list of tuples:

scala> val a = List(6, 8, 10)
a: List[Int] = List(6, 8, 10)

scala> val b = a zipWithIndex
b: List[(Int, Int)] = List((6,0), (8,1), (10,2))

I'd like to map() on the List, but I find the ._1 ._2 syntax a bit hard-to-read:

scala> val c = b map ( a => if(a._1 > 8) a._1 else a._2 )           
c: List[Int] = List(0, 1, 10)

To 'name' the tuple, I've used:

scala> val c = b map ( { case (num, i) => if(num > 8) num else i } )
c: List[Int] = List(0, 1, 10)

Two questions:

1) Is there a more concise way to name the tuple members?

2) Is there a considerable performance hit for my version above (it is used in moderately performance-critical code).

Thanks.

like image 343
flend Avatar asked Apr 02 '12 15:04

flend


2 Answers

b map Function.tupled((num, i) => if(num > 8) num else i)

avoids pattern matching and for-expressions so should be reasonably performant. I'd normally just use case as you did though.

like image 107
Luigi Plinge Avatar answered Sep 23 '22 20:09

Luigi Plinge


In this case you might find the equivalent for-comprehension syntax more readable, but it's really a matter of taste...

for {(num, i) <- b} yield if(num >8) num else i

FWIW, I've tried benchmarking the map with and without pattern matching and I got pretty much the same execution time.

Code I've used:

object bench extends scala.testing.Benchmark {
    var b:List[(Int, Int)] = _

    override def setUp {
        val a = (1000000 to 2000000).toList
        b = a zipWithIndex
    }

    def run = b map ( a => if(a._1 > 8) a._1 else a._2 )
 }

I've also created another application with a bench1 object which has only the version of map with the pattern matching instead of the ._1 and ._2. Results on my oldish netbook (scala 2.9.1, xubuntu 11.10):

$ scala bench 10 
bench$   750    758 731 721 733 736 725 743 735 736
$ scala bench1 10
bench1$  774    772 740 724 745 730 711 739 740 740
like image 21
Paolo Falabella Avatar answered Sep 22 '22 20:09

Paolo Falabella