This page is no longer maintained — Please continue to the home page at www.scala-lang.org

How to use generic type in aggregation function

3 replies
veaven
Joined: 2011-08-11,
User offline. Last seen 39 weeks 5 days ago.

Hi scala-user,

 

I have this aggregation function which will sum all Int input arguments:

case class Seed(var total: Int ) {}

def createSeed = Seed(0)

def sum(seed:Seed, value:Int) = seed.total + value

 

I also want to sum all Double values, so I have to have a new class:

case class Seed(var total: Double ) {}

def createSeed = Seed(0.0)

def sum(seed:Seed, value:Double) = seed.total + value

 

Then, I want to make *Seed * to be generic so that I can reuse it for all numeric types, e.g. int, double, long

case class Seed[T:Numeric](var total: T ) {

  def createSeed()(implicit num: Numeric[T]) = Seed(num.zero)

  def sum(seed:Seed[T], value:T) = seed.total + value

}

 

However, it does not work in my scala 2.9.1.

 

How can I define a generic sum function to calculate any numeric value?

 

David

 

Yinwei David Liu   
Morgan Stanley | Cross-Technology Services   
Kerry Parkside | 1155 Fang Dian Road, Pudong New Area   
201204 Shanghai   
Phone: +86 21 2035-2552   
Yinwei [dot] Liu [at] MorganStanley [dot] com   
   

NOTICE: Morgan Stanley is not acting as a municipal advisor and the opinions or views contained herein are not intended to be, and do not constitute, advice within the meaning of Section 975 of the Dodd-Frank Wall Street Reform and Consumer Protection Act. If you have received this communication in error, please destroy all electronic and paper copies and notify the sender immediately. Mistransmission is not intended to waive confidentiality or privilege. Morgan Stanley reserves the right, to the extent permitted under applicable law, to monitor electronic communications. This message is subject to terms available at the following link: http://www.morganstanley.com/disclaimers. If you cannot access these links, please notify us by reply message and we will send the contents to you. By messaging with Morgan Stanley you consent to the foregoing.
Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: How to use generic type in aggregation function


On Wed, Jan 11, 2012 at 11:59 AM, Liu, Yinwei David <Yinwei [dot] Liu [at] morganstanley [dot] com> wrote:

Hi scala-user,

 

I have this aggregation function which will sum all Int input arguments:

case class Seed(var total: Int ) {}

def createSeed = Seed(0)

def sum(seed:Seed, value:Int) = seed.total + value


scala> def aggregationFunction(intInputArguments: Int*) = intInputArguments.sum
aggregationFunction: (intInputArguments: Int*)Int

scala> aggregationFunction(1, 3, 5)
res5: Int = 9

Like that?

Cheers,

 

 

I also want to sum all Double values, so I have to have a new class:

case class Seed(var total: Double ) {}

def createSeed = Seed(0.0)

def sum(seed:Seed, value:Double) = seed.total + value

 

Then, I want to make *Seed * to be generic so that I can reuse it for all numeric types, e.g. int, double, long

case class Seed[T:Numeric](var total: T ) {

  def createSeed()(implicit num: Numeric[T]) = Seed(num.zero)

  def sum(seed:Seed[T], value:T) = seed.total + value

}

 

However, it does not work in my scala 2.9.1.

 

How can I define a generic sum function to calculate any numeric value?

 

David

 

Yinwei David Liu   
Morgan Stanley | Cross-Technology Services   
Kerry Parkside | 1155 Fang Dian Road, Pudong New Area   
201204 Shanghai   
Phone: +86 21 2035-2552   
Yinwei [dot] Liu [at] MorganStanley [dot] com   
   

NOTICE: Morgan Stanley is not acting as a municipal advisor and the opinions or views contained herein are not intended to be, and do not constitute, advice within the meaning of Section 975 of the Dodd-Frank Wall Street Reform and Consumer Protection Act. If you have received this communication in error, please destroy all electronic and paper copies and notify the sender immediately. Mistransmission is not intended to waive confidentiality or privilege. Morgan Stanley reserves the right, to the extent permitted under applicable law, to monitor electronic communications. This message is subject to terms available at the following link: http://www.morganstanley.com/disclaimers. If you cannot access these links, please notify us by reply message and we will send the contents to you. By messaging with Morgan Stanley you consent to the foregoing.



--
Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: How to use generic type in aggregation function

On Wed, Jan 11, 2012 at 08:59, Liu, Yinwei David
wrote:
> Hi scala-user,
>
>
>
> I have this aggregation function which will sum all Int input arguments:
>
> case class Seed(var total: Int ) {}
>
> def createSeed = Seed(0)
>
> def sum(seed:Seed, value:Int) = seed.total + value
>
>
>
> I also want to sum all Double values, so I have to have a new class:
>
> case class Seed(var total: Double ) {}
>
> def createSeed = Seed(0.0)
>
> def sum(seed:Seed, value:Double) = seed.total + value
>
>
>
> Then, I want to make *Seed * to be generic so that I can reuse it for all
> numeric types, e.g. int, double, long
>
> case class Seed[T:Numeric](var total: T ) {
>
>   def createSeed()(implicit num: Numeric[T]) = Seed(num.zero)
>
>   def sum(seed:Seed[T], value:T) = seed.total + value
>
> }
>
>
>
> However, it does not work in my scala 2.9.1.
>
>
>
> How can I define a generic sum function to calculate any numeric value?

Well, there's an ambiguous implicit, but, fixing that, it works for me.

scala> import scala.math.Numeric.Implicits._
import scala.math.Numeric.Implicits._

scala> case class Seed[T:Numeric](var total: T ) {
| def createSeed() = Seed(implicitly[Numeric[T]].zero)
| def sum(seed:Seed[T], value:T) = seed.total + value
| }
defined class Seed

veaven
Joined: 2011-08-11,
User offline. Last seen 39 weeks 5 days ago.
RE: How to use generic type in aggregation function

Thanks Daniel, adding * scala.math.Numeric.Implicits._* fixed the issue.

David

-----Original Message-----
From: Daniel Sobral [mailto:dcsobral [at] gmail [dot] com]
Sent: Wednesday, January 11, 2012 8:35 PM
To: Liu, Yinwei David (ISGT)
Cc: scala-user [at] googlegroups [dot] com
Subject: Re: [scala-user] How to use generic type in aggregation function

On Wed, Jan 11, 2012 at 08:59, Liu, Yinwei David
wrote:
> Hi scala-user,
>
>
>
> I have this aggregation function which will sum all Int input arguments:
>
> case class Seed(var total: Int ) {}
>
> def createSeed = Seed(0)
>
> def sum(seed:Seed, value:Int) = seed.total + value
>
>
>
> I also want to sum all Double values, so I have to have a new class:
>
> case class Seed(var total: Double ) {}
>
> def createSeed = Seed(0.0)
>
> def sum(seed:Seed, value:Double) = seed.total + value
>
>
>
> Then, I want to make *Seed * to be generic so that I can reuse it for all
> numeric types, e.g. int, double, long
>
> case class Seed[T:Numeric](var total: T ) {
>
> def createSeed()(implicit num: Numeric[T]) = Seed(num.zero)
>
> def sum(seed:Seed[T], value:T) = seed.total + value
>
> }
>
>
>
> However, it does not work in my scala 2.9.1.
>
>
>
> How can I define a generic sum function to calculate any numeric value?

Well, there's an ambiguous implicit, but, fixing that, it works for me.

scala> import scala.math.Numeric.Implicits._
import scala.math.Numeric.Implicits._

scala> case class Seed[T:Numeric](var total: T ) {
| def createSeed() = Seed(implicitly[Numeric[T]].zero)
| def sum(seed:Seed[T], value:T) = seed.total + value
| }
defined class Seed

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland