LondonMassive
LondonMassive

Reputation: 457

How to call a function on every element in a list in Scala?

I am trying to create a recursive function that takes a List as a parameter. In this function there is another List, which contains data I want to pass into the recursive function. I for every element in the list I want to call the function.

I have tried the following:

list2.foreach(foo(_::list1))

I get the following error:

knight1.scala:78: error: type mismatch;
 found   : List[Any]
 required: Main.$anon.CW8a.Pos
    (which expands to)  (Int, Int)
    list2.foreach(foo(_::list1))
                       ^

Both list1 and list2 are of the type List[(Int, Int)].

Upvotes: 0

Views: 1507

Answers (1)

Benjamin Erichsen
Benjamin Erichsen

Reputation: 108

In Scala you need to be more explicit. You are misusing the underscore operator.

list2.foreach(elt => foo(elt::list1))

Upvotes: 0

Related Questions