- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
How to use scala.math.Numeric?
Sat, 2010-04-17, 00:37
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:
Thanks!
-0xe1a
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]*)Any tips? :)
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(_ + _)
}
Thanks!
-0xe1a
Sun, 2010-04-18, 17:57
#2
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?
Sun, 2010-04-18, 19:47
#3
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
Tue, 2010-04-20, 19:47
#4
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 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
Tue, 2010-04-20, 20:07
#5
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
-Stefan
2010/4/17 Alex Cruise <alex@cluonflux.com>