CS 3060: Programming Languages

The Scalable Language

Robert C. Green II, Ph.D.

Day 1

Edward Scissorhands

Key Points

  • Oh so Java-like
  • Runs on the Java virtual machine
  • Uses Java libraries directly
  • Statically typed (but doesn't always feel like it)
  • Hybrid: Supports both object-oriented and functional programming paradigms

Key Points

  • Type inference
  • Functional concepts
  • Immutable variables
  • Advanced programming constructs

Running a File

The Code

                            
        object HelloWorld {
          def main(args: Array[String]) {
            println("Hello, world!")
          }
        }
                            
                        

Interactive Shell

                            
        HelloWorld.main(null)
                            
                        

Compile & Execute

                            
        scalac HelloWorld.scala
        scala HelloWorld
                            
                        

Without a Main

                            
            object HelloWorld extends App {
                println("Hello, world!")
            }
                            
                        

Script

                            
            object HelloWorld extends App {
                println("Hello, world!")
            }
            
            HelloWorld.main(args)
                            
                        

Script

                            
            println("Hello, world!")
                            
                        

Is Scala strongly typed?

                            
            "abc" + 4
                            
                        
                            
            4 + "abc"
                            
                        
                            
            4 + "1.0"
                            
                        
                            
            4 * "abc"
                            
                        

Output

                        
        println("My magic output...")
        print("My magic output...")
                        
                    

Variables

                        
        val a = 1
        val b = 3
                        
                    
                        
        val a:Int = 1
        val b:Int = 3
                        
                    

Conditionals

                        
        val a = 1
        val b = 3

        if(b<a){
            println("true")
        }else{
            false("true") 
        }
                        
                    

Loops

While Loops

                            
        def whileLoop {
            var i = 1

            while(i <= 3) {
                println(i)
                i += 1
            }
        }
        whileLoop
                            
                        

For Loops

                            
        def forLoop {
            println( "for loop using Java-style iteration" )
            
            for(i <- 0 until args.length) {
                println(args(i))
            }
        }
        
        forLoop
                            
                        

For Loops

                            
        def forLoop {
            println( "for loop using Java-style iteration" )
            
            args.foreach { arg =>
                println(arg)
            }
        }
        
        forLoop
                            
                        

Ranges

The Basics

                            
            val range = 0 until 10
            range.start
            range.end
            range.step
                            
                        

Increments

                            
            (0 to 10) by 5
            (0 to 10) by 6
            (0 until 10 by 5)
                            
                        

Direction

                            
            val range = (10 until 0) by -1
            val range = (10 until 0)
            val range = (0 to 10)
            val range = 'a' to 'e'
                            
                        

Tuples

Basics

                            
            val person = ("Elvis", "Presley")
            
            val (x, y) = (1, 2)
            val (a, b) = (1, 2, 3)
                            
                        

Lists

Basics

                            
            val a = List(1, 2, 3)
            a(2)
                            
                        

One catch

                            
            Nil
                            
                        

Sets

Basics

                            
            val animals = Set("lions", "tigers", "bears")
            animals + "armadillos"
            animals - "tigers"

            animals + Set("armadillos", "raccoons")

            animals ++ Set("armadillos", "raccoons")
            animals -- Set("lions", "bears")
                            
                        

A little more...

                            
animals ** Set("armadillos", "raccoons", "lions", "tigers")

Set(1, 2, 3) == Set(3, 2, 1)
                            
                        

Methods

                        
    def double(x:Int):Int = x * 2
                        
                    
                        
    def double(x:Int):Int = {
        x * 2
    }
                        
                    

Methods

                        
def name(p1: Type1, p2: Type2) [:returnType] = {
    if(original.length <= maxLength)
        return original;        
    original.substring(0, maxLength - suffix.length) + suffix;
}
                        
                    
 
                        
def ellipse(original: String, maxLength: Int) : String = {
    if(original.length <= maxLength)
        return original;        
    original.substring(0, maxLength - suffix.length) + suffix;
}
                        
                    

Left for you

  • Random Number Generation
  • Iteration
  • String functions

Assignment

Start the Assignment

Read Day 2

Resources