progs/lecture5.scala
changeset 242 e6b34f617915
parent 240 b8cdaf51ffef
child 247 50a3b874008a
equal deleted inserted replaced
241:c650a91db720 242:e6b34f617915
    18 
    18 
    19 // say we have a pretty expensive operation
    19 // say we have a pretty expensive operation
    20 def peop(n: BigInt): Boolean = peop(n + 1) 
    20 def peop(n: BigInt): Boolean = peop(n + 1) 
    21 
    21 
    22 val a = "foo"
    22 val a = "foo"
    23 val b = "foo"
    23 val b = "bar"
    24 
    24 
    25 if (a == b || peop(0)) println("true") else println("false")
    25 if (a == b || peop(0)) println("true") else println("false")
    26 
    26 
    27 // this is called lazy evaluation
    27 // this is called lazy evaluation
    28 // you delay compuation until it is really 
    28 // you delay compuation until it is really 
    45   s.head #:: generatePrimes(s.tail.filter(_ % s.head != 0))
    45   s.head #:: generatePrimes(s.tail.filter(_ % s.head != 0))
    46 
    46 
    47 val primes = generatePrimes(Stream.from(2))
    47 val primes = generatePrimes(Stream.from(2))
    48 
    48 
    49 // the first 10 primes
    49 // the first 10 primes
    50 primes.take(10).toList
    50 primes.take(10).par.toList
    51 
    51 
    52 time_needed(1, primes.filter(_ > 100).take(3000).toList)
    52 time_needed(1, primes.filter(_ > 100).take(3000).toList)
    53 time_needed(1, primes.filter(_ > 100).take(3000).toList)
    53 time_needed(1, primes.filter(_ > 100).take(1000).toList)
    54 
    54 
    55 // a stream of successive numbers
    55 // a stream of successive numbers
    56 Stream.from(2)
    56 
    57 
    57 Stream.from(2).print
    58 Stream.from(2).take(10)
    58 Stream.from(2).take(10).force
    59 Stream.from(2).take(10).print
    59 Stream.from(2).take(10).print
    60 Stream.from(10).take(10).print
    60 Stream.from(10).take(10).print
    61 
    61 
    62 Stream.from(2).take(10).force
    62 Stream.from(2).take(10).force
    63 
    63 
   222   def parse(sb: I) = 
   222   def parse(sb: I) = 
   223     for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
   223     for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
   224 }
   224 }
   225 
   225 
   226 
   226 
       
   227 // atomic parsers  
       
   228 case class CharParser(c: Char) extends Parser[String, Char] {
       
   229   def parse(sb: String) = 
       
   230     if (sb != "" && sb.head == c) Set((c, sb.tail)) else Set()
       
   231 }
       
   232 
       
   233 import scala.util.matching.Regex
       
   234 case class RegexParser(reg: Regex) extends Parser[String, String] {
       
   235   def parse(sb: String) = reg.findPrefixMatchOf(sb) match {
       
   236     case None => Set()
       
   237     case Some(m) => Set((m.matched, m.after.toString))  
       
   238   }
       
   239 }
       
   240 
       
   241 val NumParser = RegexParser("[0-9]+".r)
       
   242 def StringParser(s: String) = RegexParser(Regex.quote(s).r)
       
   243 
       
   244 println(NumParser.parse_all("12345"))
       
   245 println(NumParser.parse_all("12u45"))
       
   246 
       
   247 
       
   248 // convenience
       
   249 implicit def string2parser(s: String) = StringParser(s)
       
   250 implicit def char2parser(c: Char) = CharParser(c)
       
   251 
   227 implicit def ParserOps[I<% Seq[_], T](p: Parser[I, T]) = new {
   252 implicit def ParserOps[I<% Seq[_], T](p: Parser[I, T]) = new {
   228   def | (q : => Parser[I, T]) = new AltParser[I, T](p, q)
   253   def | (q : => Parser[I, T]) = new AltParser[I, T](p, q)
   229   def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
   254   def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
   230   def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
   255   def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
   231 }
   256 }
   239   def ~ (r: String) = 
   264   def ~ (r: String) = 
   240     new SeqParser[String, String, String](s, r)
   265     new SeqParser[String, String, String](s, r)
   241 }
   266 }
   242 
   267 
   243 
   268 
   244 // atomic parsers  
       
   245 case class CharParser(c: Char) extends Parser[String, Char] {
       
   246   def parse(sb: String) = 
       
   247     if (sb != "" && sb.head == c) Set((c, sb.tail)) else Set()
       
   248 }
       
   249 
       
   250 import scala.util.matching.Regex
       
   251 case class RegexParser(reg: Regex) extends Parser[String, String] {
       
   252   def parse(sb: String) = reg.findPrefixMatchOf(sb) match {
       
   253     case None => Set()
       
   254     case Some(m) => Set((m.matched, m.after.toString))  
       
   255   }
       
   256 }
       
   257 
       
   258 val NumParser = RegexParser("[0-9]+".r)
       
   259 def StringParser(s: String) = RegexParser(Regex.quote(s).r)
       
   260 
       
   261 println(NumParser.parse_all("12345"))
       
   262 println(NumParser.parse_all("12u45"))
       
   263 
       
   264 
       
   265 // convenience
       
   266 implicit def string2parser(s: String) = StringParser(s)
       
   267 implicit def char2parser(c: Char) = CharParser(c)
       
   268 
       
   269 implicit def ParserOps[I<% Seq[_], T](p: Parser[I, T]) = new {
       
   270   def | (q : => Parser[I, T]) = new AltParser[I, T](p, q)
       
   271   def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
       
   272   def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
       
   273 }
       
   274 
       
   275 implicit def StringOps(s: String) = new {
       
   276   def | (q : => Parser[String, String]) = new AltParser[String, String](s, q)
       
   277   def | (r: String) = new AltParser[String, String](s, r)
       
   278   def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)
       
   279   def ~[S] (q : => Parser[String, S]) = 
       
   280     new SeqParser[String, String, S](s, q)
       
   281   def ~ (r: String) = 
       
   282     new SeqParser[String, String, String](s, r)
       
   283 }
       
   284 
       
   285 
       
   286 val NumParserInt = NumParser ==> (s => s.toInt)
   269 val NumParserInt = NumParser ==> (s => s.toInt)
   287 
   270 
   288 NumParser.parse_all("12345")
   271 NumParser.parse_all("12345")
   289 NumParserInt.parse_all("12345")
   272 NumParserInt.parse_all("12345")
   290 NumParserInt.parse_all("12u45")
   273 NumParserInt.parse_all("12u45")