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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     1
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     2
package Main
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     3
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     4
object ComprehensionsTest extends App {
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     5
  def even(from: Int, to: Int): List[Int] =
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     6
    for (i <- List.range(from, to) if i % 2 == 0) yield i
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     7
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     8
  Console.println(even(0, 20))
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     9
}
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    10
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    11
object ComprehensionsTest2 extends App {
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    12
  def foo(n: Int, v: Int) =
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    13
    for (
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    14
      i <- 0 until n;
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    15
      j <- i + 1 until n if i + j == v
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    16
    ) yield Pair(i, j);
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    17
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    18
  foo(20, 32) foreach {
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    19
    case (i, j) =>
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    20
      println("(" + i + "," + j + ")")
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    21
  }
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    22
}
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    23
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    24
object ComprehensionTest3 extends App {
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    25
  for (
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    26
    i <- Iterator.range(0, 20);
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    27
    j <- Iterator.range(i + 1, 20) if i + j == 32
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    28
  ) println("(" + i + ", " + j + ")")
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    29
}