Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of doubles in scala

Tags:

scala

I just want a quick way to create an array (or vector) of doubles that doesn't come out as type NumericRange. Ive tried

val ys = Array(9. to 1. by -1.)

But this returns type Array[scala.collection.immutable.NumericRange[Double]]

Is there a way to coerce this to regular type Array[Double]?

like image 748
Rorschach Avatar asked Oct 22 '13 06:10

Rorschach


People also ask

How to create array of integers in Scala?

Create Array with RangeUse of range() method to generate an array containing a sequence of increasing integers in a given range. You can use final argument as step to create the sequence; if you do not use final argument, then step would be assumed as 1.

What is the difference between list and array in Scala?

The Scala List class holds a sequenced, linear list of items. Following are the point of difference between lists and array in Scala: Lists are immutable whereas arrays are mutable in Scala. Lists represents a linked list whereas arrays are flat.


2 Answers

scala> (9d to 1d by -1d).toArray
res0: Array[Double] = Array(9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0)
like image 153
Brian Avatar answered Nov 04 '22 01:11

Brian


I think it slightly more concise and readable:

Array(9d to 1 by -1 : _*)
res0: Array[Double] = Array(9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0)
like image 35
Eddie Jamsession Avatar answered Nov 04 '22 02:11

Eddie Jamsession