CS 3060: Programming Languages

The Scalable Language

Robert C. Green II, Ph.D.

Classes & Objects

The Basics

                            
            class Person(firstName: String, lastName: String)
                            
                        

A Complete Class

The Compass Class

Auxiliary Constructors

                            
            class Person(firstName: String) {
                println("Outer constructor")

                def this(firstName: String, lastName: String) {
                    this(firstName)
                    println("Inner constructor")
                }
                
                def talk() = println("Hi")
            }

            val bob = new Person("Bob")
            val bobTate = new Person("Bob", "Tate")
                            
                        

What if wrote...

                            
            object Person(firstName: String) {
                ...
                            
                        

Companion Objects

  • Class contains Instance methods
  • Object contains Class (or Static) methods
  • Both can (and should!) have the same name

Inheritance

The Employee Class

Traits

Chat with Ned Flanders

Functional Programming

Var vs. Val

What's the big deal?
Mutable states limit concurrency

Higher Order Functions

A function that can take other functions as a parameter

Anonymous Functions

                            
        val list = List("frodo", "samwise", "pippin")
        list.foreach(hobbit => println(hobbit))

        val hobbits = Set("frodo", "samwise", "pippin")
        hobbits.foreach(hobbit => println(hobbit))

        val hobbits = Map("frodo" -> "hobbit",
                          "samwise" -> "hobbit", 
                          "pippin" -> "hobbit")
        hobbits.foreach(hobbit => println(hobbit))

                            
                        

Deeper on Lists

                            
        list.tail
        list.head
        list.reverse
        list.count
                            
                        

Deeper on Lists

                            
        val words = List("peg", "al", "bud", "kelly")
        
        words.count (word => word.size > 2)
        words.filter(word => word.size > 2)
        words.map   (word => word.size)
        words.forall(word => word.size > 1)
        words.exists(word => word.size > 4)
                            
                        

Deeper on Lists

                            
words.sortWith((s, t) => 
    s.charAt(0).toLowerCase < t.charAt(0).toLowerCase)

words.sortWith((s, t) => s.size < t.size)
                            
                        

FoldLeft

                            
        val list = List(1, 2, 3)

        val sum = (0 /: list) {(sum, i) => sum + i}
                            
                        
                            
        val sum = list.foldLeft(0)((sum, value) => sum + value)
                            
                        

Left for you

  • File I/O

Assignment

Keep working on the Assignment. Incorporate functional things

Read Day 3