1

Finding difficult understanding following control structure

scala> def twice(op: Double => Double, x: Double) = op(op(x))

twice: (op: (Double) => Double,x: Double)Double

scala> twice( _ + 1,5)

res0: Double = 7.0
Rustem Suniev
  • 1,149
  • 9
  • 8
Amit
  • 119
  • 7

1 Answers1

3
op: Double => Double

is a function that takes a Double and returns a Double as a result. twice is a method takes a number, calls op on it, and then calls op on the result.

_ + 1

is a function that takes a value and adds one to it.

You can probably figure out the rest from there.

Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
  • Maybe my brain wasn't working..got confused w.r.t placeholder..but got it now...Thanks – Amit Feb 04 '11 at 16:19