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-- |
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 | 15 |
|
16 |
package Main |
|
17 |
||
18 |
object ComprehensionsTest extends App { |
|
19 |
def even(from: Int, to: Int): List[Int] = |
|
20 |
for (i <- List.range(from, to) if i % 2 == 0) yield i |
|
21 |
||
22 |
Console.println(even(0, 20)) |
|
23 |
} |
|
24 |
||
25 |
object ComprehensionsTest2 extends App { |
|
26 |
def foo(n: Int, v: Int) = |
|
27 |
for ( |
|
28 |
i <- 0 until n; |
|
29 |
j <- i + 1 until n if i + j == v |
|
30 |
) yield Pair(i, j); |
|
31 |
||
32 |
foo(20, 32) foreach { |
|
33 |
case (i, j) => |
|
34 |
println("(" + i + "," + j + ")") |
|
35 |
} |
|
36 |
} |
|
37 |
||
38 |
object ComprehensionTest3 extends App { |
|
39 |
for ( |
|
40 |
i <- Iterator.range(0, 20); |
|
41 |
j <- Iterator.range(i + 1, 20) if i + j == 32 |
|
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 |
} |