progs/lecture5.scala
changeset 482 769bda18a43d
parent 481 e03a0100ec46
--- a/progs/lecture5.scala	Fri Dec 08 00:54:36 2023 +0000
+++ b/progs/lecture5.scala	Mon Dec 25 01:10:55 2023 +0100
@@ -1,6 +1,15 @@
 // Scala Lecture 5
 //=================
 
+for (n <- (1 to 10).toList) yield {
+  val add = 10
+  n + add
+}
+
+println(add)
+
+List(1,2,3,4).sum
+
 // extension methods
 // implicit conversions
 // (Immutable) OOP
@@ -11,14 +20,14 @@
 
 // Extensions or How to Pimp your Library
 //======================================
-//
+
 // For example adding your own methods to Strings:
 // Imagine you want to increment strings, like
 //
 //     "HAL".increment
 //
 // you can avoid ugly fudges, like a MyString, by
-// using implicit conversions.
+// using extensions.
 
 extension (s: String) {
   def increment = s.map(c => (c + 1).toChar)
@@ -28,6 +37,7 @@
 
 
 
+// a more relevant example
 
 import scala.concurrent.duration.{TimeUnit,SECONDS,MINUTES}
 
@@ -45,8 +55,12 @@
 5.seconds + 2.minutes   //Duration(125, SECONDS )
 
 
+// Implicit Conversions
+//=====================
+
+
+
 // Regular expressions - the power of DSLs in Scala
-//                                     and Laziness
 //==================================================
 
 abstract class Rexp
@@ -57,6 +71,8 @@
 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence     r1 . r2  
 case class STAR(r: Rexp) extends Rexp             // star         r*
 
+val r = STAR(CHAR('a'))
+
 
 // some convenience for typing in regular expressions
 import scala.language.implicitConversions    
@@ -76,7 +92,11 @@
   def ~ (s: Rexp) = SEQ(r, s)
 }
 
+val r1 = CHAR('a') | CHAR('b') | CHAR('c')
+val r2 = CHAR('a') ~  CHAR('b')
 
+val r3 : Rexp = "hello world"
+      
 
 //example regular expressions
 val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"