Fahad/CodeSamples/SequenceComprehensions.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 22 Mar 2022 11:14:02 +0000
changeset 462 d9b672c4c0ac
parent 46 79336e47e14d
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 44
diff changeset
     1
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 44
diff changeset
     2
case class Twice(i: Int) {                              
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 44
diff changeset
     3
  def apply(x: Int, y: Int): Int = x * y
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 44
diff changeset
     4
  //def unapply(z: Int): Option[Int] = if (z % 2 == 0) Some(z/2) else None
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 44
diff changeset
     5
}
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 44
diff changeset
     6
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 44
diff changeset
     7
Twice(21) match {
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 44
diff changeset
     8
  case Twice(n) => println(n)
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 44
diff changeset
     9
}
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 44
diff changeset
    10
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 44
diff changeset
    11
Twice(21, 3) match {
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 44
diff changeset
    12
  case Twice(n) => println(n)
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 44
diff changeset
    13
}
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 44
diff changeset
    14
44
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    15
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    16
package Main
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
object ComprehensionsTest extends App {
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    19
  def even(from: Int, to: Int): List[Int] =
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    20
    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
    21
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    22
  Console.println(even(0, 20))
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
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    25
object ComprehensionsTest2 extends App {
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    26
  def foo(n: Int, v: Int) =
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    27
    for (
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    28
      i <- 0 until n;
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    29
      j <- i + 1 until n if i + j == v
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    30
    ) yield Pair(i, j);
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    31
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    32
  foo(20, 32) foreach {
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    33
    case (i, j) =>
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    34
      println("(" + i + "," + j + ")")
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    35
  }
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    36
}
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    37
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    38
object ComprehensionTest3 extends App {
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    39
  for (
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    40
    i <- Iterator.range(0, 20);
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    41
    j <- Iterator.range(i + 1, 20) if i + j == 32
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    42
  ) println("(" + i + ", " + j + ")")
46
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 44
diff changeset
    43
}