Skip to content

Instantly share code, notes, and snippets.

@gkthiruvathukal
Created February 24, 2016 05:04
Show Gist options
  • Save gkthiruvathukal/13bf540d1eaf5781a380 to your computer and use it in GitHub Desktop.
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 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.
@gkthiruvathukal
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment