Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1060131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:16:47+00:00 2026-05-16T18:16:47+00:00

Newbie Scala Question: Say I want to do this [Java code] in Scala: public

  • 0

Newbie Scala Question:

Say I want to do this [Java code] in Scala:

public static double[] abs(double[] r, double[] im) {
  double t[] = new double[r.length];
  for (int i = 0; i < t.length; ++i) {
    t[i] = Math.sqrt(r[i] * r[i] + im[i] * im[i]);
  }
  return t;
}  

and also make it generic (since Scala efficiently do generic primitives I have read). Relying only on the core language (no library objects/classes, methods, etc), how would one do this? Truthfully I don’t see how to do it at all, so I guess that’s just a pure bonus point question.

I ran into sooo many problems trying to do this simple thing that I have given up on Scala for the moment. Hopefully once I see the Scala way I will have an ‘aha’ moment.

UPDATE:
Discussing this with others, this is the best answer I have found so far.

def abs[T](r: Iterable[T], im: Iterable[T])(implicit n: Numeric[T]) = {
   import n.mkNumericOps                                                   
   r zip(im) map(t => math.sqrt((t._1 * t._1 + t._2 * t._2).toDouble))          
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:16:48+00:00Added an answer on May 16, 2026 at 6:16 pm

    Doing generic/performant primitives in scala actually involves two related mechanisms which scala uses to avoid boxing/unboxing (e.g. wrapping an int in a java.lang.Integer and vice versa):

    • @specialize type annotations
    • Using Manifest with arrays

    specialize is an annotation that tells the Java compiler to create “primitive” versions of code (akin to C++ templates, so I am told). Check out the type declaration of Tuple2 (which is specialized) compared with List (which isn’t). It was added in 2.8 and means that, for example code like CC[Int].map(f : Int => Int) is executed without ever boxing any ints (assuming CC is specialized, of course!).

    Manifests are a way of doing reified types in scala (which is limited by the JVM’s type erasure). This is particularly useful when you want to have a method genericized on some type T and then create an array of T (i.e. T[]) within the method. In Java this is not possible because new T[] is illegal. In scala this is possible using Manifests. In particular, and in this case it allows us to construct a primitive T-array, like double[] or int[]. (This is awesome, in case you were wondering)

    Boxing is so important from a performance perspective because it creates garbage, unless all of your ints are < 127. It also, obviously, adds a level of indirection in terms of extra process steps/method calls etc. But consider that you probably don’t give a hoot unless you are absolutely positively sure that you definitely do (i.e. most code does not need such micro-optimization)


    So, back to the question: in order to do this with no boxing/unboxing, you must use Array (List is not specialized yet, and would be more object-hungry anyway, even if it were!). The zipped function on a pair of collections will return a collection of Tuple2s (which will not require boxing, as this is specialized).

    In order to do this generically (i.e. across various numeric types) you must require a context bound on your generic parameter that it is Numeric and that a Manifest can be found (required for array creation). So I started along the lines of…

    def abs[T : Numeric : Manifest](rs : Array[T], ims : Array[T]) : Array[T] = {
        import math._
        val num = implicitly[Numeric[T]]
        (rs, ims).zipped.map { (r, i) => sqrt(num.plus(num.times(r,r), num.times(i,i))) }
        //                               ^^^^ no SQRT function for Numeric
    }
    

    …but it doesn’t quite work. The reason is that a “generic” Numeric value does not have an operation like sqrt -> so you could only do this at the point of knowing you had a Double. For example:

    scala> def almostAbs[T : Manifest : Numeric](rs : Array[T], ims : Array[T]) : Array[T] = {
     | import math._
     | val num = implicitly[Numeric[T]]
     | (rs, ims).zipped.map { (r, i) => num.plus(num.times(r,r), num.times(i,i)) }
     | }
    almostAbs: [T](rs: Array[T],ims: Array[T])(implicit evidence$1: Manifest[T],implicit     evidence$2: Numeric[T])Array[T]
    

    Excellent – now see this purely generic method do some stuff!

    scala> val rs = Array(1.2, 3.4, 5.6); val is = Array(6.5, 4.3, 2.1)
    rs: Array[Double] = Array(1.2, 3.4, 5.6)
    is: Array[Double] = Array(6.5, 4.3, 2.1)
    
    scala> almostAbs(rs, is)
    res0: Array[Double] = Array(43.69, 30.049999999999997, 35.769999999999996)
    

    Now we can sqrt the result, because we have a Array[Double]

    scala> res0.map(math.sqrt(_))
    res1: Array[Double] = Array(6.609841147864296, 5.481788029466298, 5.980802621722272)
    

    And to prove that this would work even with another Numeric type:

    scala> import math._
    import math._
    scala> val rs = Array(BigDecimal(1.2), BigDecimal(3.4), BigDecimal(5.6)); val is =     Array(BigDecimal(6.5), BigDecimal(4.3), BigDecimal(2.1))
    rs: Array[scala.math.BigDecimal] = Array(1.2, 3.4, 5.6)
    is: Array[scala.math.BigDecimal] = Array(6.5, 4.3, 2.1)
    
    scala> almostAbs(rs, is)
    res6: Array[scala.math.BigDecimal] = Array(43.69, 30.05, 35.77)
    
    scala> res6.map(d => math.sqrt(d.toDouble))
    res7: Array[Double] = Array(6.609841147864296, 5.481788029466299, 5.9808026217222725)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scala/Android newbie question. I'm trying to rewrite this simple code from Java to Scala.
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
Newbie Java question - On all the posts for Auto Scaling the Text view,
Scala Newbie alert: basically I'm trying to do something like this: where I pattern
I am a Scala newbie and I need to port part of a java
Here is a newbie scala question. I have a list of people with Person
I am a very new bie for scala. So I download the source code
Scala newbie here Trying (1).+(2) returns a Int value of 3, so far so
Newbie question on drools. I am trying to access the following java class in
Newbie question: I want to print the name and value of every environment variable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.