As an “extract” type parameter to instantiate another class

The following Scala code works:

object ReducerTestMain extends App {

  type MapOutput = KeyVal[String, Int]

  def mapFun(s:String): MapOutput = KeyVal(s, 1)

  val red = new ReducerComponent[String, Int]((a: Int, b: Int) => a + b)

  val data = List[String]("a", "b", "c", "b", "c", "b")

  data foreach {s => red(mapFun(s))}
  println(red.mem)
  // OUTPUT: Map(a -> 1, b -> 3, c -> 2)
}

class ReducerComponent[K, V](f: (V, V) => V) {
  var mem = Map[K, V]()

  def apply(kv: KeyVal[K, V]) = {
    val KeyVal(k, v) = kv
    mem += (k -> (if (mem contains k) f(mem(k), v) else v))
  }
}

case class KeyVal[K, V](key: K, value:V)

My problem: I would like to create an instance ReducerComponentas follows:

val red = new ReducerComponent[MapOutput, Int]((a: Int, b: Int) => a + b)

or even better:

val red = new ReducerComponent[MapOutput](_ + _)

This means a lot of things:

  • I would like to introduce a type check MapOutputtype KeyVal[K, C],
  • I want to enter a verification type of Cthe same type as in f,
  • I also need to “extract” Kto instantiate memand type check parameters from apply.

How much to ask? :) I wanted to write something like

class ReducerComponent[KeyVal[K,V]](f: (V, V) => V) {...}

, ReducerComponent, , , f MapOutput, V . KeyVal[K,V] , KeyVal[_,_].

, , , , , , ! , - , . ?

+4
2

factory:

case class RC[M <: KeyVal[_, _]](){
   def apply[K,V](f: (V,V) => V)(implicit ev: KeyVal[K,V] =:= M) = new ReducerComponent[K,V](f)
}

def plus(x: Double, y: Double) = x + y

scala> RC[KeyVal[Int, Double]].apply(plus)
res12: ReducerComponent[Int,Double] = ReducerComponent@7229d116

scala> RC[KeyVal[Int, Double]]()(plus)
res16: ReducerComponent[Int,Double] = ReducerComponent@389f65fe

, ReducerComponent . , K V M <: KeyVal[_, _].

P.S. f, (_: Double) + (_: Double). :

case class RC[M <: KeyVal[_, _]](){
   def factory[K,V](implicit ev: KeyVal[K,V] =:= M) = new {
      def apply(f: (V,V) => V) = new ReducerComponent[K,V](f)
   }
}

scala> RC[KeyVal[Int, Double]].factory.apply(_ + _)
res5: ReducerComponent[Int,Double] = ReducerComponent@3dc04400 


scala> val f = RC[KeyVal[Int, Double]].factory
f: AnyRef{def apply(f: (Double, Double) => Double): ReducerComponent[Int,Double]} = RC$$anon$1@19388ff6

scala> f(_ + _)
res13: ReducerComponent[Int,Double] = ReducerComponent@24d8ae83

Update. keyval - use type:

type KV[K,V] = KeyVal[K,V] //may be anything, may implement `type KV[K,V]` from some supertrait

case class RC[M <: KV[_, _]](){   
  def factory[K,V](implicit ev: KV[K,V] =:= M) = new {
    def apply(f: (V,V) => V) = new ReducerComponent[K,V](f)
  }
}

, apply - KeyVal[K,V].

KV :

class Builder[KV[_,_]] {
  case class RC[M <: KV[_, _]](){   
    def factory[K,V](implicit ev: KV[K,V] =:= M) = new {
      def apply(f: (V,V) => V) = new ReducerComponent[K,V](f)
    }
  }
}

scala> val b = new Builder[KeyVal]
scala> val f = b.RC[KeyVal[Int, Double]].factory
scala> f(_ + _)
res2: ReducerComponent[Int,Double] = ReducerComponent@54d9c993
+5

. :

, , :

trait KeyValAux {
  type K
  type V
  type KV = KeyVal[K, V]
}

factory ReducerComponent:

object ReducerComponent {
  def apply[T <: KeyValAux](f: (T#V, T#V) => T#V) =
    new ReducerComponent[T#K, T#V](f)
}

, . .

MapOutput KeyValAux (, ):

type MapOutput = KeyValAux { type K = String; type V = Int }

def mapFun(s:String): MapOutput#KV = KeyVal(s, 1)

val red = ReducerComponent[MapOutput](_ + _)

UPDATE

@dk14 , , :

trait OutputSpec[KK, VV] extends KeyValAux {
  type K = KK
  type V = VV
}

:

type MapOutput = OutputSpec[String, Int]

OutputSpec :

type OutputSpec[KK, VV] = KeyValAux { type K = KK; type V = VV }

.

+3

All Articles