Fahad/CodeSamples/SequenceComprehensions.scala
author Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
Sun, 09 Nov 2014 19:25:10 +0000
changeset 44 a751aa1ee4f7
child 46 79336e47e14d
permissions -rw-r--r--
Code Samples


package Main

object ComprehensionsTest extends App {
  def even(from: Int, to: Int): List[Int] =
    for (i <- List.range(from, to) if i % 2 == 0) yield i

  Console.println(even(0, 20))
}

object ComprehensionsTest2 extends App {
  def foo(n: Int, v: Int) =
    for (
      i <- 0 until n;
      j <- i + 1 until n if i + j == v
    ) yield Pair(i, j);

  foo(20, 32) foreach {
    case (i, j) =>
      println("(" + i + "," + j + ")")
  }
}

object ComprehensionTest3 extends App {
  for (
    i <- Iterator.range(0, 20);
    j <- Iterator.range(i + 1, 20) if i + j == 32
  ) println("(" + i + ", " + j + ")")
}