Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you were told was true about "for comprehensions", but the problem with your question is that you've mixed up "for comprehensions" with "anonymous functions". </p> <p>"For comprehension" in Scala is a syntactic sugar for a series of <code>.flatMap</code>, <code>.map</code> and <code>.filter</code> applications. Since you are testing reduction algorithms and since it's impossible to implement a reduction algorithm using those three functions, your test cases are incorrect.</p> <p>Here's an example of a "for comprehension":</p> <pre><code>val listOfLists = List(List(1,2), List(3,4), List(5)) val result = for { itemOfListOfLists &lt;- listOfLists itemOfItemOfListOfLists &lt;- itemOfListOfLists } yield (itemOfItemOfListOfLists + 1) assert( result == List(2,3,4,5,6) ) </code></pre> <p>The compiler desugars the Comprehension part to the following:</p> <pre><code>val result = listOfLists.flatMap( itemOfListOfLists =&gt; itemOfListOfLists.map( itemOfItemOfListOfLists =&gt; itemOfItemOfListOfLists + 1 ) ) </code></pre> <p>Then it desugars the Anonymous Function syntax:</p> <pre><code>val result = listOfLists.flatMap( new Function1[List[Int], List[Int]] { override def apply(itemOfListOfLists: List[Int]): List[Int] = itemOfListOfLists.map( new Function1[Int, Int] { override def apply(itemOfItemOfListOfLists: Int): Int = itemOfItemOfListOfLists + 1 } ) } ) </code></pre> <p>From the desugarred code it's now evident that the <code>Function1[Int, Int]</code> class gets instantiated every time the <code>apply(itemOfListOfLists: List[Int]): List[Int]</code> method gets called. That happens for every entry of <code>listOfLists</code>. So the more complex your comprehension is the more instantiations of the <code>Function</code> objects you get.</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. 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.
    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