equal
deleted
inserted
replaced
178 // Or, A topic of endless "fun"(?) |
178 // Or, A topic of endless "fun"(?) |
179 |
179 |
180 |
180 |
181 // input type: String |
181 // input type: String |
182 // output type: Int |
182 // output type: Int |
183 Integer.parseInt("123456") |
183 Integer.parseInt("123u456") |
184 |
184 |
185 /* Note, in the previous lectures I did not show the type consraint |
185 /* Note, in the previous lectures I did not show the type consraint |
186 * I <% Seq[_] , which means that the input type I can be |
186 * I <% Seq[_] , which means that the input type I can be |
187 * treated, or seen, as a sequence. */ |
187 * treated, or seen, as a sequence. */ |
188 |
188 |
239 } |
239 } |
240 |
240 |
241 val NumParser = RegexParser("[0-9]+".r) |
241 val NumParser = RegexParser("[0-9]+".r) |
242 def StringParser(s: String) = RegexParser(Regex.quote(s).r) |
242 def StringParser(s: String) = RegexParser(Regex.quote(s).r) |
243 |
243 |
244 println(NumParser.parse_all("12345")) |
244 NumParser.parse_all("12u345") |
245 println(NumParser.parse_all("12u45")) |
245 println(NumParser.parse_all("12u45")) |
246 |
246 |
247 |
247 |
248 // convenience |
248 // convenience |
249 implicit def string2parser(s: String) = StringParser(s) |
249 implicit def string2parser(s: String) = StringParser(s) |
264 def ~ (r: String) = |
264 def ~ (r: String) = |
265 new SeqParser[String, String, String](s, r) |
265 new SeqParser[String, String, String](s, r) |
266 } |
266 } |
267 |
267 |
268 |
268 |
269 val NumParserInt = NumParser ==> (s => s.toInt) |
269 val NumParserInt = NumParser ==> (s => 2 * s.toInt) |
270 |
270 |
271 NumParser.parse_all("12345") |
271 NumParser.parse_all("12345") |
272 NumParserInt.parse_all("12345") |
272 NumParserInt.parse_all("12345") |
273 NumParserInt.parse_all("12u45") |
273 NumParserInt.parse_all("12u45") |
274 |
274 |
286 lazy val T: Parser[String, Int] = |
286 lazy val T: Parser[String, Int] = |
287 (F ~ "*" ~ T) ==> { case ((x, y), z) => x * z } | F |
287 (F ~ "*" ~ T) ==> { case ((x, y), z) => x * z } | F |
288 lazy val F: Parser[String, Int] = |
288 lazy val F: Parser[String, Int] = |
289 ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } | NumParserInt |
289 ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } | NumParserInt |
290 |
290 |
291 println(E.parse_all("1+3+4")) |
291 |
292 println(E.parse_all("4*2+3")) |
292 println(E.parse_all("4*2+3")) |
293 println(E.parse_all("4*(2+3)")) |
293 println(E.parse_all("4*(2+3)")) |
294 println(E.parse_all("(4)*((2+3))")) |
294 println(E.parse_all("(4)*((2+3))")) |
295 println(E.parse_all("4/2+3")) |
295 println(E.parse_all("4/2+3")) |
296 println(E.parse_all("(1+2)+3")) |
296 println(E.parse_all("(1+2)+3")) |
329 // |
329 // |
330 // http://scalapuzzlers.com |
330 // http://scalapuzzlers.com |
331 // |
331 // |
332 // http://www.latkin.org/blog/2017/05/02/when-the-scala-compiler-doesnt-help/ |
332 // http://www.latkin.org/blog/2017/05/02/when-the-scala-compiler-doesnt-help/ |
333 |
333 |
334 List(1, 2, 3) contains "your mom" |
334 List(1, 2, 3).contains("your mom") |
335 |
335 |
336 // I like best about Scala that it lets me often write |
336 // I like best about Scala that it lets me often write |
337 // concise, readable code. And it hooks up with the |
337 // concise, readable code. And it hooks up with the |
338 // Isabelle theorem prover. |
338 // Isabelle theorem prover. |
339 |
339 |