Scala function literals and placeholder syntax on zipped collections

540 Views Asked by At

I'm currently learning Scala and have struggled with using placeholder syntax on zipped collections. For example, I want to filter the zipped array from items where l2[i] >= l1[i]. How can I do this using an explicit function literal or the placeholder syntax? I have tried:

scala> val l = List(3, 0, 5) zip List(1, 2, 3)
l: List[(Int, Int)] = List((3,1), (4,2), (5,3))

scala> l.filter((x, y) => x > y)
<console>:9: error: missing parameter type
Note: The expected type requires a one-argument function accepting a 2-Tuple.
      Consider a pattern matching anonymous function, `{ case (x, y) =>  ... }`
              l.filter((x, y) => x > y)
                        ^
<console>:9: error: missing parameter type
              l.filter((x, y) => x > y)

scala> l.filter((x:Int, y:Int) => x > y)
<console>:9: error: type mismatch;
     found   : (Int, Int) => Boolean
     required: ((Int, Int)) => Boolean
                  l.filter((x:Int, y:Int) => x > y)

Trying the placeholder syntax:

scala> l.filter(_ > _)
      <console>:9: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$greater(x$2))
  Note: The expected type requires a one-argument function accepting a 2-Tuple.
  Consider a pattern matching anonymous function, `{ case (x$1, x$2) =>  ... }`
        l.filter(_ > _)
            ^
<console>:9: error: missing parameter type for expanded function ((x$1: <error>, x$2) => x$1.$greater(x$2))
        l.filter(_ > _)

So it seems to require a function on a Pair:

scala> l.filter(_._1 > _._2)
<console>:9: error: missing parameter type for expanded function ((x$1, x$2) => x$1._1.$greater(x$2._2))
Note: The expected type requires a one-argument function accepting a 2-Tuple.
      Consider a pattern matching anonymous function, `{ case (x$1, x$2) =>  ... }`
              l.filter(_._1 > _._2)
                       ^
<console>:9: error: missing parameter type for expanded function ((x$1: <error>, x$2) => x$1._1.$greater(x$2._2))
              l.filter(_._1 > _._2)

So what am I doing wrong? Is match way the only one? Thanks for the help.

2

There are 2 best solutions below

0
On BEST ANSWER

As you found out filter over List requires a function with just one parameter (in your case a Tupple2 aka "Pair"):

def filter(p: (A) => Boolean): List[A]

And this (x, y) => x > y means a function with two parameters (A, B) => Boolean. So you went in the right direction in using _1 and _2, but each use of _ represents a new parameter so _._1 > _._2 is actually equivalent to (x, y) => x._1 > y._2. Thus if you want to use the same argument more than once you cannot use underscore:

l.filter(p => p._1 > p._2)

Another possibility is to use pattern matching to expand the Tupple2:

l.filter{ case (x, y) => x > y }

Alternatively, you can use zipped which will return a Tuple2Zipped and its filter accepts a function with two parameters (simplified signature below):

def filter(f: (El1, El2) => Boolean): (List[El1], List[El2])

You can use the zipped this way:

scala> val l = (List(3, 0, 5), List(1, 2, 3)) zipped
l: scala.runtime.Tuple2Zipped[Int,List[Int],Int,List[Int]] = scala.runtime.Tuple2Zipped@8dd86903

scala> l.filter(_ > _)
res1: (List[Int], List[Int]) = (List(3, 5),List(1, 3))

scala> l.filter(_ > _).zipped.toList
res2: List[(Int, Int)] = List((3,1), (5,3))

The filter of Tuple2Zipped returns a pair of lists instead of a list of pairs, so to convert to the second form you can use .zipped.toList.

0
On

Use this:

l.filter { case (x, y) => x > y }

or

l.filter(x => x._1 > x._2)

Also in Scala type information does not flow from a function's body to its arguments.