Task

Produce a spiral array.



A   spiral array   is a square arrangement of the first    N2   natural numbers,   where the

numbers increase sequentially as you go around the edges of the array spiraling inwards.



For example, given   5,   produce this array:

 0  1  2  3  4
15 16 17 18  5
14 23 24 19  6
13 22 21 20  7
12 11 10  9  8



Related tasks

   Zig-zag matrix 
   Identity_matrix
   Ulam_spiral_(for_primes)


class Folder(){
  var dir = (1,0)
  var pos = (-1,0)
  def apply(l:List[Int], a:Array[Array[Int]]) = {
    var (x,y) = pos  //start position
    var (dx,dy) = dir //direction
    l.foreach {e => x = x + dx; y = y + dy; a(y)(x) = e }  //copy l elements to array using current direction
    pos = (x,y)
    dir = (-dy, dx) //turn
  }
}
def spiral(n:Int) = { 
  def dup(n:Int) = (1 to n).flatMap(i=>List(i,i)).toList
  val folds = n :: dup(n-1).reverse  //define fold part lengths
 
  var array = new Array[Array[Int]](n,n)
  val fold = new Folder()
 
  var seq = (0 until n*n).toList  //sequence to fold
  folds.foreach {len => fold(seq.take(len),array); seq = seq.drop(len)}
  array
}