Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 1.58 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to find minimum and maximum of cartesian co-ordinates in Scala
  2. override def toString:String =
  3.    {
  4.         val output:StringBuilder = new StringBuilder();
  5.         val minOfRowColumn = for
  6.         {
  7.           cell <- aliveCells
  8.           row = cell.row
  9.           column = cell.column
  10.         } yield if( row < column ) row else column
  11.  
  12.         val min = minOfRowColumn.min
  13.  
  14.         val maxOfRowColumn = for
  15.         {
  16.           cell <- aliveCells
  17.           row = cell.row
  18.           column = cell.column
  19.         } yield if( row > column ) row else column
  20.  
  21.         val max = maxOfRowColumn.max
  22.  
  23.         var row = min;
  24.         var column = min;
  25.  
  26.         while(row <= max)
  27.         {
  28.           while(column <= max)
  29.           {
  30.             if(aliveCells.contains(Cell(row,column)))
  31.             {
  32.               output.append('X')
  33.             }
  34.             else
  35.               output.append('-')
  36.             column = column + 1
  37.           }
  38.           output.append("n");
  39.           column = min
  40.           row = row + 1
  41.         }
  42.  
  43.  
  44.         //remove the last new line addded.
  45.         val indexOfNewLine = output.lastIndexOf("n");
  46.         if( -1 != indexOfNewLine )
  47.         output.delete(indexOfNewLine,output.length());
  48.  
  49.         return output.toString();
  50.    }
  51.        
  52. override def toString = {
  53.   val min = aliveCells.iterator.flatMap(c => Seq(c.row, c.column)).min
  54.   val max = aliveCells.iterator.flatMap(c => Seq(c.row, c.column)).max
  55.  
  56.   (min to max) map { row =>
  57.     (min to max) map (col => if (aliveCells(Cell(row, col))) "X" else "-") mkString
  58.   } mkString ("n")
  59. }
  60.        
  61. val minC = aliveCells.iterator.map(_.column).min