How to implement generic function in Scala with two argument types?
NickName:Buck Ask DateTime:2015-06-11T10:29:29

How to implement generic function in Scala with two argument types?

I'd like to implement a function in Scala that computes the dot product of two numeric sequences as follows

val x = Seq(1,2,3.0)
val y = Seq(4,5,6)
val z = (for (a <- x; b <- y) yield a*b).sum
scala> z  : Double = 90.0

val x = Seq(1,2,3)
val y = Seq(4,5,6)
val z = (for (a <- x; b <- y) yield a*b).sum
scala> z  : Int = 90

Notice that if the two sequences are of different types, the result is an Double. If the two sequences are of the same type (e.g. Int), the result is an Int.

I came up with two alternatives but neither meets the requirement as defined above.

Alternative #1:

def dotProduct[T: Numeric](x: Seq[T], y: Seq[T]): T = (for (a <- x; b <- y) yield implicitly[Numeric[T]].times(a, b)).sum

This returns the result in the same type as the input, but it can't take two different types.

Alternative #2:

def dotProduct[A, B](x: Seq[A], y: Seq[B])(implicit nx: Numeric[A], ny: Numeric[B]) = (for (a <- x; b <- y) yield nx.toDouble(a)*ny.toDouble(b)).sum

This works for all numeric sequences. However, it always return a Double, even if the two sequences are of the type Int.

Any suggestion is greatly appreciated.

p.s. The function I implemented above is not "dot product", but simply sum of product of two sequences. Thanks Daniel for pointing it out.

Alternative #3 (slightly better than alternatives #1 and #2):

def sumProduct[T, A <% T, B <% T](x: Seq[A], y: Seq[B])(implicit num: Numeric[T]) = (for (a <- x; b <- y) yield num.times(a,b)).sum

sumProduct(Seq(1,2,3), Seq(4,5,6))  //> res0: Int = 90
sumProduct(Seq(1,2,3.0), Seq(4,5,6))  //> res1: Double = 90.0
sumProduct(Seq(1,2,3), Seq(4,5,6.0))  // Fails!!!

Unfortunately, the View Bound feature (e.g. "<%") will be deprecated in Scala 2.10.

Copyright Notice:Content Author:「Buck」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30770552/how-to-implement-generic-function-in-scala-with-two-argument-types

More about “How to implement generic function in Scala with two argument types?” related questions

How to implement generic function in Scala with two argument types?

I'd like to implement a function in Scala that computes the dot product of two numeric sequences as follows val x = Seq(1,2,3.0) val y = Seq(4,5,6) val z = (for (a &lt;- x; b &lt;- y) yield a*b).sum

Show Detail

How to implement generic function that receives differents types scala

import org.apache.spark.{ SparkConf, SparkContext } import org.apache.spark.rdd.RDD class BaseType(val a: String) extends Serializable { override def toString = "(" + a + ")" }

Show Detail

Scala cast to generic type (for generic numerical function)

I'm trying to implement a generic function that wraps a mathematical Java function. For simplicity, we can assume that the Java function (Java 7) takes one parameter and returns a result, both of t...

Show Detail

Scala generic types

I there, I'm trying to combine the Command and Chain of responsibility patterns with Scala style. Basically, I would like to have one Executor who pass the command trough the chain and return the r...

Show Detail

Infer generic types on scala shell

Is it possible to use the scala shell to infer the type of a generic function? I was trying to undestand the type of the function Future.traverse (scala 2.10). The complete generic type is def

Show Detail

Scala convert tuple of types to tuple of generic of types

Say I have type a defined as a tuple of types type a = (Int, String, Int) and I define generic class Foo as class Foo[A]{} is there any method is Scala (preferably native Scala) that can conver...

Show Detail

Minimal, generic case class `copy` function in Scala 3

In Scala (both 2 and 3), the copy method of case classes is synthetic, and so cannot be generalized using simple type clauses. In Scala 2, this led to generically processing case classes being done...

Show Detail

Scala: type inference of generic and it's type argument

Lets assume I have instance of arbitrary one-argument generic class (I'll use List in demonstration but this can me any other generic). I'd like to write generic function that can take instances (...

Show Detail

Generic function and index types

I'm looking to implement a generic function which accepts a single type argument "T" which is an object. It uses keyof T to specify the first argument to the function "property". Now the part I can...

Show Detail

Scala Argument Types of Anonymous Function in Overload

There are quite a few questions about this error message on SO, but none of them seem to be about this issue. The argument types of an anonymous function must be fully known. (SLS 8.5) The offend...

Show Detail