user13404388
user13404388

Reputation:

Combine Subtract and Filter in Scala

I am trying to write a function in Scala which takes two integer lists xs and ys and does the following:

I am trying to do this using the list functions from the Scala Standard Library

object Solution {
  def combineSubAndFilter(xs: List[Int], ys: List[Int]) = ???
}

Upvotes: 0

Views: 132

Answers (1)

Tomer Shetah
Tomer Shetah

Reputation: 8529

The answer, as suggested in the comments by @Luis is:

def combineSubAndFilter(xs: List[Int], ys: List[Int]) = xs.zipAll(ys, 0, 0).collect { case (x,y) if (x > y) => x-y }

From zipAll documentation:

Returns a $coll formed from this $coll and another iterable collection by combining corresponding elements in pairs. If one of the two collections is shorter than the other, placeholder elements are used to extend the shorter collection to the length of the longer.

Code run can be found at Scastie.

Upvotes: 4

Related Questions