user3663882
user3663882

Reputation: 7357

Combining partial functions

I came from Java and would like to combine two partial functions like this:

def sum(a: Int, b: Int, c: Int) : Int = a + b + c

I want write something like this:

val l = List(1, 2, 3)
l.foreach(println _  sum (1, _ : Int, 3) ) // It's supposed to apply
                                  // the partial sum(1, _: Int, 3) 
                                  // and println computed value.

But it refuses to compile. Is there a way to fix it concisely?

Upvotes: 0

Views: 375

Answers (2)

Victor Moroz
Victor Moroz

Reputation: 9225

There are few problems with what you were trying to do. First of all, println is an overloaded method, so when you are trying to eta-expand it to a function like this println _, Scala is taking the shortest form, which is the one with 0 arguments:

scala> println _
res0: () => Unit = $$Lambda$1048/48841776@58ec44ec

So you need to be more specific:

scala> println(_: Int)
res1: Int => Unit = $$Lambda$1064/1621799037@1e8bddc4

Now that we have correct function there is one more thing to do: to add compose and proper parentheses (additional parentheses wrap anonymous functions):

scala> l.foreach((println(_: Int)) compose  (sum (1, _: Int, 3)))
5
6
7

or in the other direction:

scala> l.foreach((sum (1, _: Int, 3)) andThen (println(_: Int)))
5
6
7

Upvotes: 3

pedrofurla
pedrofurla

Reputation: 12783

Assuming I reading what you want correctly (and even the code it's a huge assumption), here a snippet that might achieve it:

scala> def sum(a: Int, b: Int, c: Int) : Int = a + b + c
sum: (a: Int, b: Int, c: Int)Int

scala> val sum13 = sum(1, _ : Int, 3)
sum13: Int => Int = <function1>

scala> val printInt = println( _ : Int )
printInt: Int => Unit = <function1>

scala> List(1,2,4) foreach { printInt compose sum13 }
5
6
8

Notice the compose. Another way would be explicitly compose x => printInt(sum13(x)).

Upvotes: 2

Related Questions