Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The semantic difference has been explained fairly well in <a href="https://stackoverflow.com/a/8650639/1287554">the answer linked to by Plasty Grove</a>.</p> <p>In terms of functionality, there doesn't seem much of a difference, though. Let's look at some examples to verify that. First, a normal function:</p> <pre><code>scala&gt; def modN(n: Int, x: Int) = ((x % n) == 0) scala&gt; modN(5, _ : Int) res0: Int =&gt; Boolean = &lt;function1&gt; </code></pre> <p>So we get a partially applied <code>&lt;function1&gt;</code> that takes an <code>Int</code>, because we've already given it the first integer. So far so good. Now to currying:</p> <pre><code>scala&gt; def modNCurried(n: Int)(x: Int) = ((x % n) == 0) </code></pre> <p>With this notation, you'd naively expect the following to work:</p> <pre><code>scala&gt; modNCurried(5) &lt;console&gt;:9: error: missing arguments for method modN; follow this method with `_' if you want to treat it as a partially applied function modNCurried(5) </code></pre> <p>So the <strong>multiple parameter list</strong> notation doesn't really seem to create a curried function right away (assumingly to avoid unnecessary overhead) but waits for you to explicitly state that you want it curried (the notation has some <a href="https://stackoverflow.com/a/4684598/124257">other advantages</a> as well):</p> <pre><code>scala&gt; modNCurried(5) _ res24: Int =&gt; Boolean = &lt;function1&gt; </code></pre> <p>Which is exactly the same thing we got before, so no difference here, except for notation. Another example:</p> <pre><code>scala&gt; modN _ res35: (Int, Int) =&gt; Boolean = &lt;function2&gt; scala&gt; modNCurried _ res36: Int =&gt; (Int =&gt; Boolean) = &lt;function1&gt; </code></pre> <p>This demonstrates how partially applying a "normal" function results in a function that takes all parameters, whereas partially applying a function with multiple parameter lists creates a chain of functions, <strong>one per parameter list</strong> which, all return a new function:</p> <pre><code>scala&gt; def foo(a:Int, b:Int)(x:Int)(y:Int) = a * b + x - y scala&gt; foo _ res42: (Int, Int) =&gt; Int =&gt; (Int =&gt; Int) = &lt;function2&gt; scala&gt; res42(5) &lt;console&gt;:10: error: not enough arguments for method apply: (v1: Int, v2: Int)Int =&gt; (Int =&gt; Int) in trait Function2. Unspecified value parameter v2. </code></pre> <p>As you can see, because the first parameter list of <code>foo</code> has two parameters, the first function in the curried chain has two parameters. </p> <hr> <p>In summary, partially applied functions aren't really different form curried functions in terms of functionality. This is easily verified given that you can convert any function to a curried one:</p> <pre><code>scala&gt; (modN _).curried res45: Int =&gt; (Int =&gt; Boolean) = &lt;function1 scala&gt; modNCurried _ res46: Int =&gt; (Int =&gt; Boolean) = &lt;function1&gt; </code></pre> <hr> <h1>Post Scriptum</h1> <p><strong>Note:</strong> The reason that your example <code>println(filter(nums, modN(2))</code> works without the underscore after <code>modN(2)</code> seems to be that the Scala compiler simply assumes that underscore as a convenience for the programmer.</p> <hr> <p><strong>Addition:</strong> As @asflierl has correctly pointed out, Scala doesn't seem to be able to infer the type when partially applying "normal" functions:</p> <pre><code>scala&gt; modN(5, _) &lt;console&gt;:9: error: missing parameter type for expanded function ((x$1) =&gt; modN(5, x$1)) </code></pre> <p>Whereas that information is available for functions written using multiple parameter list notation:</p> <pre><code>scala&gt; modNCurried(5) _ res3: Int =&gt; Boolean = &lt;function1&gt; </code></pre> <p><a href="https://stackoverflow.com/q/8063325/124257">This answers</a> shows how this can be very useful.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload