author | Christian Urban <urbanc@in.tum.de> |
Thu, 25 Oct 2018 00:50:58 +0100 | |
changeset 588 | a4646557016d |
parent 586 | 451a95e1bc25 |
child 590 | c6a1e19e9801 |
permissions | -rw-r--r-- |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
import scala.language.implicitConversions |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
import scala.language.reflectiveCalls |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
|
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
4 |
/* Note, in the lectures I did not show the type consraint |
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
5 |
* I <% Seq[_] , which means that the input type I can be |
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
6 |
* treated, or seen, as a sequence. */ |
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
7 |
|
461 | 8 |
abstract class Parser[I <% Seq[_], T] { |
9 |
def parse(ts: I): Set[(T, I)] |
|
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
|
461 | 11 |
def parse_all(ts: I) : Set[T] = |
360
c6c574d2ca0c
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
185
diff
changeset
|
12 |
for ((head, tail) <- parse(ts); |
c6c574d2ca0c
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
185
diff
changeset
|
13 |
if (tail.isEmpty)) yield head |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
|
461 | 16 |
class SeqParser[I <% Seq[_], T, S](p: => Parser[I, T], |
17 |
q: => Parser[I, S]) extends Parser[I, (T, S)] { |
|
18 |
def parse(sb: I) = |
|
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
for ((head1, tail1) <- p.parse(sb); |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
(head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
|
461 | 23 |
class AltParser[I <% Seq[_], T](p: => Parser[I, T], |
24 |
q: => Parser[I, T]) extends Parser[I, T] { |
|
25 |
def parse(sb: I) = p.parse(sb) ++ q.parse(sb) |
|
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
|
461 | 28 |
class FunParser[I <% Seq[_], T, S](p: => Parser[I, T], |
29 |
f: T => S) extends Parser[I, S] { |
|
30 |
def parse(sb: I) = |
|
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
for ((head, tail) <- p.parse(sb)) yield (f(head), tail) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
|
183
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
178
diff
changeset
|
34 |
// atomic parsers |
461 | 35 |
case class CharParser(c: Char) extends Parser[String, Char] { |
177
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
172
diff
changeset
|
36 |
def parse(sb: String) = |
462 | 37 |
if (sb != "" && sb.head == c) Set((c, sb.tail)) else Set() |
177
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
172
diff
changeset
|
38 |
} |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
172
diff
changeset
|
39 |
|
588 | 40 |
import scala.util.matching.Regex |
41 |
case class RegexParser(reg: Regex) extends Parser[String, String] { |
|
42 |
def parse(sb: String) = reg.findPrefixMatchOf(sb) match { |
|
43 |
case None => Set() |
|
44 |
case Some(m) => Set((m.matched, m.after.toString)) |
|
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
|
588 | 48 |
val NumParser = RegexParser("[0-9]+".r) |
49 |
def StringParser(s: String) = RegexParser(s.r) |
|
50 |
||
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
|
360
c6c574d2ca0c
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
185
diff
changeset
|
52 |
// convenience |
462 | 53 |
implicit def string2parser(s: String) = StringParser(s) |
54 |
implicit def char2parser(c: Char) = CharParser(c) |
|
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
implicit def ParserOps[I<% Seq[_], T](p: Parser[I, T]) = new { |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
def || (q : => Parser[I, T]) = new AltParser[I, T](p, q) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
implicit def StringOps(s: String) = new { |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
def || (q : => Parser[String, String]) = new AltParser[String, String](s, q) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
def || (r: String) = new AltParser[String, String](s, r) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
def ~[S] (q : => Parser[String, S]) = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
new SeqParser[String, String, S](s, q) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
def ~ (r: String) = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
new SeqParser[String, String, String](s, r) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
|
588 | 72 |
val c = new CharParser('c') |
73 |
lazy val cn = c ==> (c => c.toInt) |
|
74 |
val f = (c: Char) => c.toInt |
|
75 |
||
76 |
c.parse("cb") |
|
77 |
(c ==> f).parse("cb") |
|
78 |
||
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
79 |
// a parse palindromes |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
lazy val Pal : Parser[String, String] = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
(("a" ~ Pal ~ "a") ==> { case ((x, y), z) => x + y + z } || |
531 | 82 |
("b" ~ Pal ~ "b") ==> { case ((x, y), z) => x + y + z } || "a" || "b" || "") |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
|
586 | 84 |
Pal.parse_all("abaaaba") |
85 |
||
531 | 86 |
println("Palindrome: " + Pal.parse_all("abaaaba")) |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
|
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
88 |
// well-nested parenthesis parser |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
lazy val P : Parser[String, String] = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
"(" ~ P ~ ")" ~ P ==> { case (((u, x), y), z) => "{" + x + "}" + z } || "" |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
91 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
P.parse_all("(((()()))())") |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
P.parse_all("(((()()))()))") |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
P.parse_all(")(") |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
95 |
P.parse_all("()") |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
|
360
c6c574d2ca0c
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
185
diff
changeset
|
97 |
// arithmetic expressions |
586 | 98 |
|
367
04127a5aad23
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
366
diff
changeset
|
99 |
lazy val E: Parser[String, Int] = |
470
8df654e9eb4e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
467
diff
changeset
|
100 |
(T ~ "+" ~ E) ==> { case ((x, y), z) => x + z } || |
8df654e9eb4e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
467
diff
changeset
|
101 |
(T ~ "-" ~ E) ==> { case ((x, y), z) => x - z } || T |
8df654e9eb4e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
467
diff
changeset
|
102 |
lazy val T: Parser[String, Int] = |
8df654e9eb4e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
467
diff
changeset
|
103 |
((F ~ "*" ~ T) ==> { case ((x, y), z) => x * z } || F) |
367
04127a5aad23
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
366
diff
changeset
|
104 |
lazy val F: Parser[String, Int] = |
04127a5aad23
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
366
diff
changeset
|
105 |
("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParser |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
106 |
|
586 | 107 |
|
470
8df654e9eb4e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
467
diff
changeset
|
108 |
println(E.parse("4*2+3")) |
586 | 109 |
println(E.parse_all("4/2+3")) |
367
04127a5aad23
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
366
diff
changeset
|
110 |
println(E.parse("1 + 2 * 3")) |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
111 |
println(E.parse_all("(1+2)+3")) |
531 | 112 |
println(E.parse_all("1+2+3")) |
113 |
||
586 | 114 |
|
115 |
||
531 | 116 |
// no left-recursion allowed, otherwise will loop |
117 |
lazy val EL: Parser[String, Int] = |
|
118 |
((EL ~ "+" ~ EL) ==> { case ((x, y), z) => x + z} || |
|
119 |
(EL ~ "*" ~ EL) ==> { case ((x, y), z) => x * z} || |
|
120 |
("(" ~ EL ~ ")") ==> { case ((x, y), z) => y} || |
|
121 |
NumParser) |
|
122 |
||
123 |
//println(E.parse_all("1+2+3")) |
|
124 |
||
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
125 |
|
462 | 126 |
// a repetition parser |
127 |
||
128 |
def RepParser[I <% Seq[_], T](p: => Parser[I, T]): Parser[I, List[T]] = { |
|
129 |
p ==> { case x => x :: Nil } || |
|
130 |
p ~ RepParser(p) ==> { case (x, y) => x :: y } |
|
131 |
} |
|
132 |
||
133 |
||
134 |
// a repetition parser |
|
135 |
lazy val R : Parser[String, List[Char]] = RepParser('a') |
|
136 |
println(R.parse_all("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) |
|
137 |
||
138 |
||
139 |
||
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
140 |
// non-ambiguous vs ambiguous grammars |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
141 |
lazy val S : Parser[String, String] = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
142 |
("1" ~ S ~ S) ==> { case ((x, y), z) => x + y + z } || "" |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
143 |
|
531 | 144 |
S.parse("1" * 15) |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
145 |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
146 |
lazy val U : Parser[String, String] = |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
147 |
("1" ~ U) ==> { case (x, y) => x + y } || "" |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
148 |
|
531 | 149 |
U.parse("1" * 15) |
150 |
||
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
151 |
U.parse("11") |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
152 |
U.parse("11111") |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
153 |
U.parse("11011") |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
154 |
|
531 | 155 |
U.parse_all("1" * 100) |
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
156 |
U.parse_all("1" * 100 + "0") |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
157 |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
158 |
lazy val UCount : Parser[String, Int] = |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
159 |
("1" ~ UCount) ==> { case (x, y) => y + 1 } || |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
160 |
"" ==> { (x) => 0 } |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
161 |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
162 |
UCount.parse("11111") |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
163 |
UCount.parse_all("11111") |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
164 |
|
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
165 |
|
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
166 |
|
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
167 |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
168 |
// Single Character parser |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
169 |
lazy val One : Parser[String, String] = "1" |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
170 |
lazy val Two : Parser[String, String] = "2" |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
171 |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
172 |
One.parse("1") |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
173 |
One.parse("111") |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
174 |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
175 |
(One ~ One).parse("111") |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
176 |
(One ~ One ~ One).parse("111") |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
177 |
(One ~ One ~ One ~ One).parse("1111") |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
178 |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
179 |
(One || Two).parse("111") |
467
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
180 |
|
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
181 |
|
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
182 |
for (x <- List(1, 2, 3, 4)) println(x) |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
183 |
for (x <- List(1, 2, 3, 4); if (2 < x)) yield (x.toString + x.toString) |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
184 |
for (x <- List("2", "1", "3", "4", "1")) yield (x + x + x) |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
185 |
|
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
186 |
(1, "one", '1')._3 |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
187 |
for ((x, y) <- List((1, "one"), (2, "two"), (3, "three"), (4,"many")); if (y == "many")) |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
188 |
yield (x.toString + y) |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
189 |
|
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
190 |
|
531 | 191 |
|
192 |
// Example section for lazy evaluation |
|
467
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
193 |
def square(n: Int) = { |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
194 |
n * n |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
195 |
} |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
196 |
|
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
197 |
square(4 + 3 + 5) |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
198 |
|
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
199 |
def bar(): Int = { |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
200 |
bar() |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
201 |
3 |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
202 |
} |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
203 |
|
531 | 204 |
//would loop |
205 |
square(bar()) |
|
467
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
206 |
|
531 | 207 |
// lazy |
467
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
208 |
def foo(n: => Int) = { |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
209 |
print("finished") |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
210 |
} |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
211 |
|
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
212 |
foo(bar()) |
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
213 |