-2

Suppose I have the following code:

val someNumbers = List(-11, -10, -5, 0, 5, 10)

someNumbers.foreach( println _ )

val j = 10

(x: Int) => x + j

My question is Are partially applied functions and Closures orthogonal in Scala? This presentation seems to suggest that they are.


EDIT: 13 July 2014 [Changed code above]

hawkeye
  • 34,745
  • 30
  • 150
  • 304

2 Answers2

0

It's really easy to see what's being returned when you go into the repl:

scala> type IntPairPred = (Int, Int) => Boolean
defined type alias IntPairPred

scala> val gt: IntPairPred = _ > _
gt: IntPairPred = <function2>

scala> gt(2,3)
res3: Boolean = false

What gt is, is a function2 i.e. a function that takes 2 parameters

Here's another example:

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

scala> val a = fn(2,3)
a: () => Int = <function0>

What fn returns, is a function0 i.e. a function that doesn't take any parameters

Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
0

Sorry but your example doesn't seem to refer to partial application, at least to me.

You're just using some shortcut syntax to define regular functions.

Actually gt, ge, ... definitions are expanded to something pretty like

val gt: IntPairPred = (x: Int, y: Int) => x > y
val gt: IntPairPred = (x: Int, y: Int) => x >= y
//and so on...

Regular functions support modification of its arguments, but this is not what you're looking for, I presume.

To define a closure, you should define a partial function referring to a variable in the outer scope, like

var one = 1
val gt1 = gt(1, _: Int)

assert(gt1(0))  //ok
assert(!gt1(1)) //ok

one = 2 //weirdo!
assert(gt1(0))  //ok
assert(gt1(1)) //ok
assert(!gt1(2)) //ok

So the point is not in functions definition or partial application. The point is if, at definition time, you're using variables from the closing scope within your function. In this case your function is influenced by the variable you closed over.

Is this what you were after?

pagoda_5b
  • 7,333
  • 1
  • 27
  • 40