How can I extend Scala collections using the argmax method?

I would like to add the argMax method to all collections where this makes sense . How to do it? Use implicits?

+3
source share
7 answers

In Scala 2.8, this works:

val list = List(1, 2, 3)
def f(x: Int) = -x
val argMax = list max (Ordering by f)

As pointed out by mkneissl , this does not return a set of maximum points. Here is an alternative implementation that makes and tries to reduce the number of calls f. If calls are fnot that important, see mkneissl's answer . Also, note that his answer is curry, which provides an excellent conclusion.

def argMax[A, B: Ordering](input: Iterable[A], f: A => B) = {
  val fList = input map f
  val maxFList = fList.max
  input.view zip fList filter (_._2 == maxFList) map (_._1) toSet
}

scala> argMax(-2 to 2, (x: Int) => x * x)
res15: scala.collection.immutable.Set[Int] = Set(-2, 2)
+4
source

argmax ( Wikipedia)

def argMax[A,B](c: Traversable[A])(f: A=>B)(implicit o: Ordering[B]): Traversable[A] = {
  val max = (c map f).max(o)
  c filter { f(_) == max }
}

,

implicit def enhanceWithArgMax[A](c: Traversable[A]) = new {
  def argMax[B](f: A=>B)(implicit o: Ordering[B]): Traversable[A] = ArgMax.argMax(c)(f)(o)
}

:

val l = -2 to 2
assert (argMax(l)(x => x*x) == List(-2,2))
assert (l.argMax(x => x*x) == List(-2,2))

(Scala 2.8)

+4

, "pimp my library", . ( N.B. , , ):


trait PimpedList[A] {
   val l: List[A]

  //example argMax, not meant to be correct
  def argMax[T <% Ordered[T]](f:T => T) = {error("your definition here")}
}

implicit def toPimpedList[A](xs: List[A]) = new PimpedList[A] { 
  val l = xs
}

scala> def f(i:Int):Int = 10
f: (i: Int) Int

scala> val l = List(1,2,3)
l: List[Int] = List(1, 2, 3)

scala> l.argMax(f)
java.lang.RuntimeException: your definition here
    at scala.Predef$.error(Predef.scala:60)
    at PimpedList$class.argMax(:12)
        //etc etc...
+2

API Scala, Pimp my Library. , . , Vector3 :

class Vector3 (val x: Float, val y: Float, val z: Float)

, , - : 2.5f * v. * Float, :

implicit def scaleVector3WithFloat(f: Float) = new {
    def *(v: Vector3) = new Vector3(f * v.x, f * v.y, f * v.z)
}

, ( new { ... }), *.

, , - :

implicit def argMaxImplicit[A](t: Traversable[A]) = new {
    def argMax() = ...
}
+1

-. , Traversable, Traversable. , . - , , .

object RichTraversable {
  implicit def traversable2RichTraversable[A](t: Traversable[A]) = new RichTraversable[A](t)
}

class RichTraversable[A](t: Traversable[A]) { 
  def argMax[That, C](g: A => C)(implicit bf : scala.collection.generic.CanBuildFrom[Traversable[A], A, That], ord:Ordering[C]): That = {
    var minimum:C = null.asInstanceOf[C]
    val repr = t.repr
    val builder = bf(repr)
    for(a<-t){
      val test: C = g(a)
      if(test == minimum || minimum == null){
        builder += a
        minimum = test
      }else if (ord.gt(test, minimum)){
        builder.clear
        builder += a
        minimum = test
      }
    }
    builder.result
  }
}

Set(-2, -1, 0, 1, 2).argmax(x=>x*x) == Set(-2, 2)
List(-2, -1, 0, 1, 2).argmax(x=>x*x) == List(-2, 2)
+1

, @Daniel, Sets.

def argMax[A, B: Ordering](input: GenIterable[A], f: A => B) : GenSet[A] = argMaxZip(input, f) map (_._1) toSet

def argMaxZip[A, B: Ordering](input: GenIterable[A], f: A => B): GenIterable[(A, B)] = {
  if (input.isEmpty) Nil
  else {
    val fPairs = input map (x => (x, f(x)))
    val maxF = fPairs.map(_._2).max
    fPairs filter (_._2 == maxF)
  }
}

, (B, Iterable [A]), .

0

Based on the other answers, you can easily combine everyone's strengths (minimal challenges f(), etc.). Here we have an implicit conversion for all Iterables (so they can simply be called .argmax()transparently) and a standalone method, if for some reason this is preferred. ScalaTest tests for download.

class Argmax[A](col: Iterable[A]) {
  def argmax[B](f: A => B)(implicit ord: Ordering[B]): Iterable[A] = {
    val mapped = col map f
    val max = mapped max ord
    (mapped zip col) filter (_._1 == max) map (_._2)
  }
}

object MathOps {
  implicit def addArgmax[A](col: Iterable[A]) = new Argmax(col)

  def argmax[A, B](col: Iterable[A])(f: A => B)(implicit ord: Ordering[B]) = {
    new Argmax(col) argmax f
  }
}

class MathUtilsTests extends FunSuite {
  import MathOps._

  test("Can argmax with unique") {
    assert((-10 to 0).argmax(_ * -1).toSet === Set(-10))
    // or alternate calling syntax
    assert(argmax(-10 to 0)(_ * -1).toSet === Set(-10))
  }

  test("Can argmax with multiple") {
    assert((-10 to 10).argmax(math.pow(_, 2)).toSet === Set(-10, 10))
  }
}
0
source

Source: https://habr.com/ru/post/1750221/


All Articles