Content deleted Content added
GreenC bot (talk | contribs) Add {{reflist-talk}} to #java functions are unused in Comparison Java and Scala with classes (via reftalk bot) |
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags |
||
Line 251:
Here is ''modN'':
<
def modN(n: Int)(x: Int) = ((x % n) == 0)
</syntaxhighlight>
Here are the expressions that use ''modN'' to create a function argument:
<
println(filter(nums, modN(2)))
println(filter(nums, modN(3)))
</syntaxhighlight>
It is clear that we have a value binding, which is what happens for partial functions, not currying. [[User:Nxavar|Nxavar]] ([[User talk:Nxavar|talk]]) 18:15, 5 November 2014 (UTC)
Line 268:
::The example function in the book is:
<
def sum(f: Int => Int): (Int, Int) => Int = {
def sumF(a: Int, b: Int): Int =
Line 274:
sumF
}
</syntaxhighlight>
::It takes a function with one argument and it embeds it to another function which is then returned. The above example is not currying because the returned function is not a single argument function. However, it shows that Scala has syntactic support for currying.
Line 280:
::This is how a curried function for addition would look like in Scala:
<
def add(a: Int): (Int) => Int = {
def increment(b: Int): Int =
Line 288:
var c = add(5)(6) // c = 11
</syntaxhighlight>
::In conclusion, Scala supports currying, although none of the examples mentioned here are about currying. [[User:Nxavar|Nxavar]] ([[User talk:Nxavar|talk]]) 19:59, 5 November 2014 (UTC)
:::While there is a difference in Scala between
<
def foo1(a: Int)(b: Int): Int = a + b
def foo2(a: Int): (Int) => Int = (b: Int) => a + b
val foo3: Int => Int => Int = (a: Int) => (b: Int) => a + b
</syntaxhighlight>
for all these goes that
<
val c: Int = foo1(5)(6) //or foo2(5)(6) or foo3(5)(6)
</syntaxhighlight>
is perfectly legal and does what you expect. The difference shows up at
<
val tst1: Int => Int => Int = foo1 _
val tst2: Int => Int => Int = foo2 _
val tst3: Int => Int => Int = foo3
</syntaxhighlight>
where methods need eta expansion to be converted to functions. Technically speaking only foo3 is a (curried) function, the other two are methods. Calling foo2 a curried function, but not foo1 doesn't seem right. [[User:Martijn Hoekstra|Martijn Hoekstra]] ([[User talk:Martijn Hoekstra|talk]]) 14:48, 13 November 2014 (UTC)
|