A Pragmatic Introduction to Scala Magnus Madsen. Presenting Scala: An alternative to Java.

Slides:



Advertisements
Similar presentations
The Art of Avoiding Work
Advertisements

Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
PHYS 2020 Pseudocode. Real Programmers Program in Pencil!  You can save a lot of time if you approach programming in a methodical way.  1) Write a clear.
Language of the Month If it’s December, it must be Ruby! Adam Coffman and Brent Beer.
1 CS 312 – Lecture 28 Continuations –Probably the most confusing thing you’ve seen all semester… Course summary –Life after CS 312.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Parts of a Computer Why Use Binary Numbers? Source Code - Assembly - Machine Code.
Groovy WHAT IS IT? HOW DOES IT WORK? IS IT USEFUL?
Introduction CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
10-Sep-15 Classes. Classes and objects Scala is an Object-Oriented (O-O), functional language Object-Oriented (O-O) means it’s built around “objects”
Formal Models of Computation Part II The Logic Model
Introduction to Shell Script Programming
A Brief Intro to Scala Tim Underwood. About Me Tim Underwood Co-Founder of Frugal Mechanic Software Developer Perl, PHP, C, C++, C#, Java, Ruby and now.
BIT 1003 – Presentation 7. Contents GENERATIONS OF LANGUAGES COMPILERS AND INTERPRETERS VIRTUAL MACHINES OBJECT-ORIENTED PROGRAMMING SCRIPTING LANGUAGES.
CS162 Week 1 Kyle Dewey. Overview Basic Introduction CS Accounts Scala survival guide.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
How Java becomes agile riding Rhino Xavier Casellato VP of engineering, Liligo.com.
Writing Scala Programs. Command Line There are three common operating systems: Windows (various flavors; I recommend Windows 7) UNIX or Linux (basically.
Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/
Implementation of Programming Languages 17 October 2015 | Software Technology Group | 1 Topics on EScala Vaidas Gasiunas, Lucas Satabin.
Introduction to Building Windows 8.1 & Windows Phone Applications.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CS162 Week 1 Kyle Dewey. Overview Basic Introduction CS Accounts Scala survival guide.
Andreas Enbohm Capgemini Sverige AB Scala or ”The Free Lunch is Over”
The Scala API. Scala has a reputation of being a difficult language Some people feel that Scala is beyond what the average programmer can master Others.
Development with Eclipse Software Engineering Prof. Werner Krandick.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
CS162 Week 1 Kyle Dewey. Overview Basic Introduction CS Accounts Scala survival guide.
Demo of Scalable Pluggable Types Michael Ernst MIT Dagstuhl Seminar “Scalable Program Analysis” April 17, 2008.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
Copyright © 2012 MICS‘12 and Curt Hill Development Systems: A Review Curt Hill Professor of Math and Computer Science Valley City State University
Classes and Objects and Traits And Other Miscellany 25-Jan-16.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
CSE 341 Section 10 Subtyping, Review, and The Future.
Flux & React Web Application Development Mark Repka, Rich McNeary, Steve Mueller.
By: Chad Gallati, Melissa Plakyda, Jenny Wilkes References: /a-z_programming_languages_groovy/
Scala: 6 Silver Bullets Alex Masselot Dev Forum - Vital-IT Tuesday March 9th, 2015.
A S P. Outline  The introduction of ASP  Why we choose ASP  How ASP works  Basic syntax rule of ASP  ASP’S object model  Limitations of ASP  Summary.
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Maven and Jelly James Strachan. Introduction Maven and Jelly are both Apache projects at Jakarta Ultimately both will be top.
CodeBeamer Bootcamp 2007 Customizing CodeBeamer features through scripting Aron Gombas Architect, Intland.
Fundamental of Java Programming (630002) Unit – 1 Introduction to Java.
“Object-Oriented Meets Functional” SARAH BIRNBAUM KATHERINE JOYCE
Scala days 2010 exmachina.
Top 8 Best Programming Languages To Learn
Midterm recap Total was 80 points Distribution range
Intro to ETEC Java.
Fast Number Crunching Fast Time to Market with Scala
What, Where and Why Swift
Coconut Sandor aguilar.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
Programming Language History and Evolution
Eclipse 20-Sep-18.
Main Points of Haskell Functional programming Laziness
A brief introduction to Kotlin
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
CSE 341: Programming Langs
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
(Computer fundamental Lab)
Eagle: Maturation and Evolution
The Scala API.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
CSE341: Programming Languages Section 1
SPL – PS1 Introduction to C++.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Presentation transcript:

A Pragmatic Introduction to Scala Magnus Madsen

Presenting Scala: An alternative to Java

Why I like Scala: object-orientated and functional elegant and concise unrestrictive – gives freedom of choice Scala makes me a happier programmer! Warning: Scala is the gateway drug to Haskell

A Playground for Fun Stuff: Engineering Perspective: – Actor-based Programming – Embedded DSLs, and more... Research Perspective: – Higher-Kinded Types – Delimited Continuations – Abstract Types, and more...

A Used Car Analogy class Car { var frontRight: Wheel; var frontLeft: Wheel; var backRight: Wheel; var backLeft: Wheel; } class Wheel {... }

Quote […] I can honestly say if someone had shown me the Programming in Scala book back in 2003 I'd probably have never created Groovy. James Strachan (creator of Groovy)

Case Study: MiniTAJS An inter-procedural dataflow analysis – a scaled down version of TAJS – has lots of cool stuff: abstract syntax trees, control flow graphs, etc. lattices, transfer functions, etc. – about 4500 lines of code of which 90-95% are in functional style

Main.scala package dk.brics.minitajs object Main { def main(args: Array[String]) { val options = Options.read(args.toList) Analysis.run(options) }

Options.scala case class Options(inputFile: File, context: Boolean, recency: Boolean, lazyprop: Boolean,...);

Case Classes: The bread and butter A case class declaration: – case class Options(inputFile: File,...) Automatically gives us: – getters and setters –.equals() and.hashCode() –.toString() –.copy() –.apply() –.unapply()

Options.scala object Options { def read(args: List[String]): Options = { val context = args.exists(_ == "--context"); val lazyprop = args.exists(_ == "--lazy");... Options(new File(args.last), context, recency,...); }

Bool.scala abstract sealed class Bool { def join(that: Bool): Bool = (this, that) match { case (TrueBool, TrueBool) => TrueBool; case (TrueBool, FalseBool) => AnyBool;... } case object AnyBool extends Bool; case object TrueBool extends Bool; case object FalseBool extends Bool; case object NotBool extends Bool;

Value.scala case class Value(..., bool: Bool, undef: Undef,...) { def isMaybeTrue: Boolean = (bool eq TrueBool) || (bool eq AnyBool); def joinUndef: Value = copy(undef = undef.join(MaybeUndef)); }

LatticeOps.scala abstract class LatticeOps(...) extends ContextMixin with RecencyMixin with PropagationMixin trait PropagationMixin { def propagate(state: BlockState, info: PropagateInfo, lattice: Lattice): Lattice; } new LatticeOps(...) with CallSensitivity with RecencyAbstraction with LazyPropagation

Arguments

Problem: In functional programming argument lists grow and grow Solution: Wrap arguments up inside a data type. In Scala this translates to a case class.

Example def propagate(state: BlockState, sourceContext: Context, sourceBlock: BasicBlock, targetContext: Context, targetBlock: BasicBlock, lattice: Lattice): Lattice def propagate(state: BlockState, info: PropagateInfo, lattice: Lattice): Lattice

def propagate(s: BlockState, i: PropagateInfo, l: Lattice): Lattice = { lattice.getState(i.targetContext, i.targetBlock) match { case Reachable(targetState) => { if (state != targetState) { queue.enqueue(i.targetContext, i.targetBlock); } lattice.putState(i.targetContext, i.targetBlock, targetState.join(s)); } case Unreachable => { queue.enqueue(i.targetContext, i.targetBlock); lattice.putState(i.targetContext, i.targetBlock, s); } Mutability!

Limited Mutability Problem: Whenever new flow enters a basicblock it must be added to the solver queue Potential Solution: We could modify all functions to return a pair where the last component is the set of basicblocks that must be enqueued

But the stack is deep Solver.Solve -> BlockTransfer.transfer -> BlockTransfer.transferCallBlock -> LatticeOps.functionEntry -> LazyPropagation.functionEntry -> LazyPropagation.propagate Not feasible to modify all return types Instead we use a mutable queue!

But the stack is still deep! How do we get a reference to the queue??? – We could use a global reference Or we could use Scala's implicits: class BlockTransfer(...)(implicit q: Queue) class LatticeOps(...)(implicit q: Queue)

Scaladoc

Some of the Bad Stuff Death Traps Debugging Compiler Warnings Compilation Times

Death Trap case class BasicBlock(var successors: Set[BasicBlock]); val a = BasicBlock(Set.empty); a.succesors = Set(a); a == a;

Difficult to Debug

Cryptic Compiler Warnings

Compilation is Slow Compiling miniTAJS takes 35 seconds – 4500 lines of code – 113 classes + 40 objects = 580.class files Why? – Scalac is written in Scala - i.e. it runs on the JVM – Scalac must type-check both Java and Scala – Scalac must do local type inference

Functions + Objects Does it work? No, not really (but...)

Functions + Objects Fundamental problem: – Functional Programming = Immutability – Object-orientated Programming = Mutability Immutable objects are not really objects Mutating functions are not really functions

Functions + Objects A proposed solution: 1.Split the program into FP and OO parts 2.Decide whether some data should be immutable or mutable (i.e. targed for FP or OO programming) 3.Prefer immutable data, otherwise use mutable data Not a silver bullet

Recent History Scala 2.10 Milestone 1 (Januar 2012) New Eclipse Plugin (January 2012) New IntelliJ IDEA Plugin (December 2011) Scala 2.9 (May 2011) – parallel collections Scala 2.8 (July 2010) – new collections framework

Critical Mass? Introduction to the Art of Programming Using Scala (October 2012) Scala for the Impatient (March 2012) Scala in Action (April 2012) Scala in Depth (April 2012) Actors in Scala (Januar 2012) Pro Scala: Monadic Design Patterns for the Web (August 2011) Programming in Scala 2nd (Januar 2011)

Recommended Books

Recommended Websites Official Scala website – Daily Scala – small code sniplets – CodeCommit – "Scala for Java Refugees" – StackOverflow –

Summary is viable alternative to Java – object-orientated and functional – has useful features not found in Java – runs on the JVM and interacts with Java – is fun!

Thank You! (now go download Scala)

Addendum Me> I need [what turns out to be virtual types] Erik> You could use an extra-linguistic solution Me> What do you mean "extra-linguistic"? Erik> A perl script...

Last Code Slide: Real Code

Embedded DSLs Scala has syntactic flexibility: object Button { def onClick(f: => Unit) {... } } Button.onClick(() => println("Hello"!)); But you can also write: Button.onClick { println("Hello"); }

Historical Anecdote BETA was supposed to be called Scala: For many years the name SCALA was a candidate for a new name – SCALA could mean SCAndinavian LAnguage, and in Latin it means ladder and could be interpreted as meaning something ‘going up’. The When, Why and Why Not of the BETA Programming Language