Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The standard filter in <code>for-yield</code> is obtained with <code>x &lt;- y if f(x,..)</code>. Here is an example that uses a Partial Function.</p> <pre><code>val m: PartialFunction[Event, Event] = ev =&gt; ev.sport match { case "FOOTBALL" =&gt; new FootballEvent(ev) case "SOCCER" =&gt; new SoccerEvent(ev) }; for { ev &lt;- data if m.isDefindAt(ev) val x = m(ev) } yield x // or, without the temporary binding: for (ev &lt;- data if m.isDefindAt(ev)) yield m(ev) </code></pre> <p>Note the similarity with <a href="https://scala-lang.org/archives/downloads/distrib/files/nightly/docs/library/index.html#scala.collection.Traversable"><code>Traversable.collect</code></a><sup>1</sup> mentioned in the other answer, which has this signature <code>def collect[B](pf: PartialFunction[A, B]): CC[B]</code> and returns "a new collection resulting from applying the given partial function pf to each element on which it is defined and collecting the results".</p> <p>An alternative without <code>if</code> is a variation of bwroga's deleted answer:</p> <pre><code>for { ev &lt;- data; x &lt;- ev.sport match { case "FOOTBALL" =&gt; Some(new FootballEvent(ev)) case "SOCCER" =&gt; Some(new SoccerEvent(ev)) case _ =&gt; None } } yield x </code></pre> <p>This filters by subsequently iterating through None (i.e. "0 items") or Some (i.e. "1 item") after the initial map.</p> <hr> <p><sup>1</sup> If someone can tell me how to link to individual methods in the "new" scaladoc, I'd be most grateful.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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