Created
February 24, 2016 05:04
-
-
Save gkthiruvathukal/13bf540d1eaf5781a380 to your computer and use it in GitHub Desktop.
Trying to figure out the best way to create homogenous, n-dimensional arrays in Scala. Array.ofDim() not proving to be ideal.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/ This takes forever and seems to use a lot more memory than expected | |
Array.ofDim[Double](2048, 2048, 2048) | |
// This seems to work faster and uses less memory. | |
def get1d(d1 : Int, init : Double) = Stream.continually(init) take d1 toArray | |
def get2d(d1 : Int, d2 : Int, init : Double) = Stream.continually( get1d(d2, init) ) take d1 toArray | |
def get3d(d1 : Int, d2 : Int, d3 : Int, init : Double) = Stream.continually( get2d(d2, d3, init) ) take d1 toArray | |
// More testing is needed. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nope. This doesn't work. A list is created as a result, which means that you are paying at least O(n) as we take more elements in the Stream. Then it is converted to an array. Too bad.