1

I'm a newbie to scala(fxnl programming), though I put _ in context in a few places like the below

list.map(_ * 1) 

I couldn't completely understand this statement

val story = (catch _) andThen (eat _)

though I can infer from the calling

story(new Cat, new Bird)

that underscore serves as placeholders to the the argument positions, I want to understand the concept behind it.

Eugene Loy
  • 12,224
  • 8
  • 53
  • 79
Somasundaram Sekar
  • 5,244
  • 6
  • 43
  • 85

1 Answers1

4

Statement

val story = (catch _) andThen (eat _)

is incorrect - because catch is keyword.

But this is correct:

scala> def caught(x:Int) = x + 8
caught: (x: Int)Int

scala> def eat(x:Int) = x + 3
eat: (x: Int)Int

scala> val story = (caught _) andThen (eat _) // story is function.
story: Int => Int = <function1>

scala> story(90) // You put 90 - parameter for caught (first _). It returns 90 + 8 and put it to eat (second _). eat function return 98 + 3
res0: Int = 101
Andrzej Jozwik
  • 14,331
  • 3
  • 59
  • 68