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 993837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:29:10+00:00 2026-05-16T06:29:10+00:00

Array programming languages (also known as vector or multidimensional languages) generalize operations on scalars

  • 0

Array programming languages (also known as vector or multidimensional languages) generalize operations on scalars to apply transparently to vectors, matrices, and higher dimensional arrays.

Is it possible to achieve this kind of code reuse in Scala?

  • 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-16T06:29:10+00:00Added an answer on May 16, 2026 at 6:29 am

    It’s entirely possible (I’ve done so myself) with appropriate implicits, though the result is not always as utterly seamless as a language that has been designed from the ground up that way.

    For example, suppose you want to treat arrays of integers as vectors and want to be able to add them to each other and to scalars. You have to define the operation yourself–Scala can’t guess what + should mean on an array. (Good thing, too, because * doesn’t usually have the obvious element-by-element meaning on matrices, and it means something else again when it comes to convolution!) You can

    class ArraysAdd(a: Array[Int]) {
      def +(i: Int) = a.map(_ + i)
      def +(b: Array[Int]) = {
        if (b.length==a.length) (a,b).zipped.map(_ + _).toArray
        else throw new IllegalArgumentException
      }
    }
    class ScalarAddsToArray(i: Int) {
      def +(a: Array[Int]) = a.map(_ + i)
    }
    implicit def array2addable(a: Array[Int]) = new ArraysAdd(a)
    implicit def scalar2arrayaddable(i: Int) = new ScalarAddsToArray(i)
    

    and then you can do math on arrays of integers:

    scala> Array(1,2,3) + 5
    res2: Array[Int] = Array(6, 7, 8)
    
    scala> Array(1,7) + Array(3,2)
    res3: Array[Int] = Array(4, 9)
    
    scala> 4 + Array(-2,-3,-1)
    res4: Array[Int] = Array(2, 1, 3)
    

    If you want to cover all data types it gets trickier (you need to use generics and Numeric at least–or write a code generator to cover the cases you need), and for efficiency you might want to create a custom “matrix” class instead of sticking with raw arrays that you keep wrapping with extra functionality.

    This covers basic operations. For mathematical functions like math.sqrt, if you don’t mind typing a map sqrt instead of sqrt(a) then you don’t need to do anything. Otherwise, you can overload them all yourself; tedious, but it then lets you use them transparently.

    Or you could use a library that has already done much of this for you. (Scalala is the best candidate I know of for matrices.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand what a JavaScript array is because traditional programming languages define
I have programming experience with higher-level languages, and have started coding in plain C
Possible Duplicate: Sizeof an array in the C programming language? I have an array
Possible Duplicate: Sizeof an array in the C programming language? I'm trying to write
I am newbie for iOS programming. I'm trying to create an array of arrays
I want to do something like in PHP, Python and most other programming languages:
In a lot of programming languages and their micro-optimizations, I've seen that declaring the
Possible Duplicate: Sizeof an array in the C programming language? I have been working
Many programming languages allow trailing commas in their grammar following the last item in
According to the Wikipedia (http://en.wikipedia.org/wiki/Buffer_overflow) Programming languages commonly associated with buffer overflows include C

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.