Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't need to sort the entire collection in order to determine the top N elements. However, I don't believe that this functionality is supplied by the raw library, so you would have to roll you own, possibly using the <em>pimp-my-library</em> pattern.</p> <p>For example, you can get the nth element of a collection as follows:</p> <pre><code> class Pimp[A, Repr &lt;% TraversableLike[A, Repr]](self : Repr) { def nth(n : Int)(implicit ord : Ordering[A]) : A = { val trav : TraversableLike[A, Repr] = self var ltp : List[A] = Nil var etp : List[A] = Nil var mtp : List[A] = Nil trav.headOption match { case None =&gt; error("Cannot get " + n + " element of empty collection") case Some(piv) =&gt; trav.foreach { a =&gt; val cf = ord.compare(piv, a) if (cf == 0) etp ::= a else if (cf &gt; 0) ltp ::= a else mtp ::= a } if (n &lt; ltp.length) new Pimp[A, List[A]](ltp.reverse).nth(n)(ord) else if (n &lt; (ltp.length + etp.length)) piv else new Pimp[A, List[A]](mtp.reverse).nth(n - ltp.length - etp.length)(ord) } } } </code></pre> <p>(This is not very functional; sorry)</p> <p>It's then trivial to get the top <code>n</code> elements:</p> <pre><code>def topN(n : Int)(implicit ord : Ordering[A], bf : CanBuildFrom[Repr, A, Repr]) ={ val b = bf() val elem = new Pimp[A, Repr](self).nth(n)(ord) import util.control.Breaks._ breakable { var soFar = 0 self.foreach { tt =&gt; if (ord.compare(tt, elem) &lt; 0) { b += tt soFar += 1 } } assert (soFar &lt;= n) if (soFar &lt; n) { self.foreach { tt =&gt; if (ord.compare(tt, elem) == 0) { b += tt soFar += 1 } if (soFar == n) break } } } b.result() } </code></pre> <p>Unfortunately I'm having trouble getting this pimp to be discovered via this implicit:</p> <pre><code>implicit def t2n[A, Repr &lt;% TraversableLike[A, Repr]](t : Repr) : Pimp[A, Repr] = new Pimp[A, Repr](t) </code></pre> <p>I get this:</p> <pre><code>scala&gt; List(4, 3, 6, 7, 1, 2, 8, 5).topN(4) &lt;console&gt;:9: error: could not find implicit value for evidence parameter of type (List[Int]) =&gt; scala.collection.TraversableLike[A,List[Int]] List(4, 3, 6, 7, 1, 2, 8, 5).topN(4) ^ </code></pre> <p>However, the code actually works OK:</p> <pre><code>scala&gt; new Pimp(List(4, 3, 6, 7, 1, 2, 8, 5)).topN(4) res3: List[Int] = List(3, 1, 2, 4) </code></pre> <p>And</p> <pre><code>scala&gt; new Pimp("ioanusdhpisjdmpsdsvfgewqw").topN(6) res2: java.lang.String = adddfe </code></pre>
    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