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

How to use scala.math.Numeric?

5 replies
Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Hi folks,

In my ongoing constant folding quest, I have Add, Subtract, Multiply and Divide functions which I'd like to use on all the varieties of boxed Java primitive, so it would sure be nice to only write each arithmetic method once, and rely on a couple of "glue" method that render the aforementioned boxed Java primitives as Numeric[_] and back again.

This is pretty much what I'm trying to do:

class InfixOperatorFunction[T <: Number](val operator: Operator, val operands: Expression[T]*)

case class Add[T <: Number](override val operands: Expression[T]*)
    extends InfixOperatorFunction[T](Operator.ADD, operands)
{

    def doEval = args.map(MAGIC_HAPPENS_HERE(_.eval)).reduceLeft(_ + _)
}
Any tips? :)

Thanks!

-0xe1a


Stefan Langer
Joined: 2009-10-23,
User offline. Last seen 42 years 45 weeks ago.
Re: How to use scala.math.Numeric?
You can always provide your own implicit conversions for your methods.

-Stefan

2010/4/17 Alex Cruise <alex@cluonflux.com>
Hi folks,

In my ongoing constant folding quest, I have Add, Subtract, Multiply and Divide functions which I'd like to use on all the varieties of boxed Java primitive, so it would sure be nice to only write each arithmetic method once, and rely on a couple of "glue" method that render the aforementioned boxed Java primitives as Numeric[_] and back again.

This is pretty much what I'm trying to do:

class InfixOperatorFunction[T <: Number](val operator: Operator, val operands: Expression[T]*)

case class Add[T <: Number](override val operands: Expression[T]*)
    extends InfixOperatorFunction[T](Operator.ADD, operands)
{

    def doEval = args.map(MAGIC_HAPPENS_HERE(_.eval)).reduceLeft(_ + _)
}
Any tips? :)

Thanks!

-0xe1a



Johannes Rudolph 2
Joined: 2010-02-12,
User offline. Last seen 42 years 45 weeks ago.
Re: How to use scala.math.Numeric?

You have to require an implicit parameter of Numeric[T] to access the
arithmetic methods. However, I didn't manage to make the implicit
conversions to Numeric to kick in automatically. What you can do,
though, is to call the actual implementation of the operator yourself
like this:

scala> case class Add[T](numbers: T*)(implicit x: scala.math.Numeric[T]) {
| def eval = numbers.reduceLeft(x.plus(_,_))
| }
defined class Add

scala> Add(5,3,6,42,8)
res4: Add[Int] = Add(WrappedArray(5, 3, 6, 42, 8))

scala> res4.eval
res5: Int = 64

Is that, what you wanted to do?

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: How to use scala.math.Numeric?

On Sun, Apr 18, 2010 at 06:43:47PM +0200, Johannes Rudolph wrote:
> You have to require an implicit parameter of Numeric[T] to access the
> arithmetic methods. However, I didn't manage to make the implicit
> conversions to Numeric to kick in automatically.

The implicit is in the Numeric so it must be imported. This is
suboptimal but I don't see much to be done about it at this point.

scala> case class Add[T](numbers: T*)(implicit x: scala.math.Numeric[T]) { import x._ ; def eval = numbers.reduceLeft(_ + _) }
defined class Add

scala> Add(5L, 10L, 15L) eval
res0: Long = 30

Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: How to use scala.math.Numeric?
Reminds me of a discussion a long time ago where someone wanted syntactic sugar forfoo { x =>  import x._  ...}if I remember correctly.It's not the exact same scenario of course.

On Sun, Apr 18, 2010 at 2:32 PM, Paul Phillips <paulp@improving.org> wrote:
On Sun, Apr 18, 2010 at 06:43:47PM +0200, Johannes Rudolph wrote:
> You have to require an implicit parameter of Numeric[T] to access the
> arithmetic methods. However, I didn't manage to make the implicit
> conversions to Numeric to kick in automatically.

The implicit is in the Numeric so it must be imported.  This is
suboptimal but I don't see much to be done about it at this point.

scala> case class Add[T](numbers: T*)(implicit x: scala.math.Numeric[T]) { import x._ ; def eval = numbers.reduceLeft(_ + _) }
defined class Add

scala> Add(5L, 10L, 15L) eval
res0: Long = 30

--
Paul Phillips      | The most dangerous man to any government is the man who
Analgesic          | is able to think things out [...] Almost inevitably he
Empiricist         | comes to the conclusion that the government he lives under
slap pi uphill!    | is dishonest, insane, intolerable.   -- H. L. Mencken

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: How to use scala.math.Numeric?


On Tue, Apr 20, 2010 at 8:42 PM, Naftoli Gugenheim <naftoligug@gmail.com> wrote:
Reminds me of a discussion a long time ago where someone wanted syntactic sugar forfoo { x =>  import x._  ...}if I remember correctly.It's not the exact same scenario of course.

Ah, ye olde CurlyImport?
 

On Sun, Apr 18, 2010 at 2:32 PM, Paul Phillips <paulp@improving.org> wrote:
On Sun, Apr 18, 2010 at 06:43:47PM +0200, Johannes Rudolph wrote:
> You have to require an implicit parameter of Numeric[T] to access the
> arithmetic methods. However, I didn't manage to make the implicit
> conversions to Numeric to kick in automatically.

The implicit is in the Numeric so it must be imported.  This is
suboptimal but I don't see much to be done about it at this point.

scala> case class Add[T](numbers: T*)(implicit x: scala.math.Numeric[T]) { import x._ ; def eval = numbers.reduceLeft(_ + _) }
defined class Add

scala> Add(5L, 10L, 15L) eval
res0: Long = 30

--
Paul Phillips      | The most dangerous man to any government is the man who
Analgesic          | is able to think things out [...] Almost inevitably he
Empiricist         | comes to the conclusion that the government he lives under
slap pi uphill!    | is dishonest, insane, intolerable.   -- H. L. Mencken




--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall

Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang

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