Note that there are some explanatory texts on larger screens.

plurals
  1. POScala method definition named parameters vs. unnamed
    text
    copied!<p>i'm new to Scala and i'm struggling sometimes with method signatures. Lets take this code, i'm especially interested in naming the parameters to do further operations on them.</p> <pre><code>def divide(xs: List[Int]): List[Int] = { val mid = xs.length/2 (xs take mid, xs drop mid) } </code></pre> <p>Here I defined the input list named as "xs", I've seen this convention on many web pages. But in university we had another method signature definition method (I am missing the name, sorry) in which we didn't name the input parameter(s) but pattern matching takes place:</p> <pre><code> def mylength: List[Any] =&gt; Int = { case Nil =&gt; 0 case x::xs =&gt; mylength(xs)+1 } </code></pre> <p>In this case, it is very trivial to identify the input parameter because there is just a single one. How could I use the same style as in the code below with 2 or more input parameters in the coding style shown above? </p> <pre><code> def myConcat(xs: List[Any], ys: List[Any]) = { xs ++ ys } </code></pre> <p>Sorry for my English. I didn't find anything on google because I didn't relly have a clue what terms to search for...</p> <p>Edit: I have to stick to an interface. I make another example with which you could help me. myAppend1 and myAppend2 shall behave the same way, putting a new element in the front of the list.</p> <pre><code> def myAppend1(xs: List[Any], y: Any): List[Any] = { y :: xs } </code></pre> <p>My problem is now the naming of my inputs in myAppend2...</p> <pre><code> def myAppend2: List[Any] =&gt; Any =&gt; List[Any] = { /* how can i do this here, when no names y/xs are given?*/ } </code></pre>
 

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