UMBC CMSC 331 Case Classes in Scala Intro. Basic Properties Case classes are regular classes which export their constructor parameters and which provide.

Slides:



Advertisements
Similar presentations
Sml2java a source to source translator Justin Koser, Haakon Larsen, Jeffrey Vaughan PLI 2003 DP-COOL.
Advertisements

Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
Object Initialization in X10 Yoav Zibin David Cunningham Igor Peshansky Vijay Saraswat IBM research in TJ Watson.
Tree Recursion Traditional Approach. Tree Recursion Consider the Fibonacci Number Sequence: Time: , 1, 1, 2, 3, 5, 8, 13, 21,... /
Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.
1. Scala 2. Traffic DSL in Scala 3. Comparison AToM3 4. Conclusion & Future work.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Peter Fritzson 1 MetaModelica for Meta-Modeling and Model Transformations Peter Fritzson, Adrian Pop, Peter Aronsson OpenModelica Course at INRIA, 2006.
The Singleton Pattern II Recursive Linked Structures.
More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
1 Working with References. 2 References Every object variable is a reference to an object Also true when an object is passed as an argument When the object.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp and Hélène Martin
6/10/2015Martin Odersky, LAMP, EPFL1 Programming Language Abstractions for Semi-Structured Data Martin Odersky Sebastian Maneth Burak Emir EPFL.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Programming in Scala Chapter 1. Scala: both object-oriented and functional Scala blends –object-oriented and –functional programming in a –statically.
CSE 341 Lecture 10 more about data types; nullable types; option Ullman ; slides created by Marty Stepp
Set, TreeSet, TreeMap, Comparable, Comparator. Def: The abstract data type set is a structure that holds objects and satifies ARC: Objects can be added.
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
The Scala Programming Language presented by Donna Malayeri.
Computer Science 112 Fundamentals of Programming II Introduction to Stacks.
UMBC CMSC 331 Traits in Scala. 2 Basic Properties Traits are used to define object types by specifying the signature of the supported methods. Unlike.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
- Neeraj Chandra.  It’s language written by by Martin Odersky at EPFL  It’s language written by by Martin Odersky at EPFL (École Polytechnique Fédérale.
Review Recursion Call Stack. Two-dimensional Arrays Visualized as a grid int[][] grays = {{0, 20, 40}, {60, 80, 100}, {120, 140, 160}, {180, 200, 220}};
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Logistics, Exercises and Demos Everyone should register for the course ‘SAV’ in whatAFunCourse Please obtain “The Calculus of Computation”
CS162 Week 1 Kyle Dewey. Overview Basic Introduction CS Accounts Scala survival guide.
Compiler (scalac, gcc) Compiler (scalac, gcc) I = 0 while (i < 10) { i = i + 1 } I = 0 while (i < 10) { i = i + 1 } source code i d 3 = 0 LF w i d 3 =
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Building Java Programs Binary Trees reading: 17.1 – 17.3.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Classes and Objects and Traits And Other Miscellany 25-Jan-16.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
A: A: double “4” A: “34” 4.
CMSC 330: Organization of Programming Languages Operational Semantics.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Module 9. Dealing with Generalization Course: Refactoring.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
CSE 3341/655; Part 3 35 This is all very wrong! What would the SW1/SW2 (RESOLVE) people say if they saw this? Problem: Lack of data abstraction; What would.
Scala HW5 Part 2 Combine Until Encode Decode. Combine Not sure what a correct test case is Fork(Leaf('d',4),Fork(Leaf('a',2),Leaf('b',3),List('a ', 'b'),5),List('d',
1 CMSC 341 Introduction to Trees Textbook sections:
Lecture 19: Binary Trees reading: 17.1 – 17.3
String is a synonym for the type [Char].
Building Java Programs
Software Development Java Classes and Methods
Agenda Warmup AP Exam Review: Litvin A2
CMSC 330: Organization of Programming Languages
Subtyping Rules David Evans cs205: engineering software BlackBear
PROGRAMMING IN HASKELL
Review Operation Bingo
Functions As Objects.
Introduction to Scala Unit 2
Case Classes in Scala Intro
Podcast Ch18b Title: STree Class
Programming Assignment 2A
Types and Classes in Haskell
PROGRAMMING IN HASKELL
Building Java Programs
The Recursive Descent Algorithm
Classes and Objects and Traits
HW4 Review Case Classes.
Presentation transcript:

UMBC CMSC 331 Case Classes in Scala Intro

Basic Properties Case classes are regular classes which export their constructor parameters and which provide a recursive decomposition mechanism via pattern matching. 2

Example 3 abstract class Term case class Var(name: String) extends Term case class Fun(arg: String, body: Term) extends Term case class App(f: Term, v: Term) extends Term

Code 4 Code Example For every case class the Scala compiler generates equals method which implements structural equality and atoString method. For instance and so... Fun("x", Fun("y", App(Var("x"), Var("y"))))

Results 5 val x1 = Var("x") val x2 = Var("x") val y1 = Var("y") println("" + x1 + " == " + x2 + " => " + (x1 == x2)) println("" + x1 + " == " + y1 + " => " + (x1 == y1)) Var(“x”) == Var(“x”) =>true Var(“x”) == Var(“y”) => false

Example object TermTest extends Application { def printTerm(term: Term) { term match { case Var(n) => print(n) case Fun(x, l, r) ) => print("^" + x + ".") printTerm(b) case App(f, v) => Console.print("(") printTerm(f) print(" ") printTerm(v) print(")") } …. abstract class Term case class Var(name: String) extends Term case class Fun(arg: String, body: Term) extends Term case class App(f: Term, v: Term) extends Term

Example Cont def isIdentityFun(term: Term): Boolean = term match { case Fun(x, Var(y)) if x == y => true case _ => false } val id = Fun("x", Var("x")) val t = Fun("x", Fun("y", App(Var("x"), Var("y")))) printTerm(t) println println(isIdentityFun(id)) println(isIdentityFun(t)) }

Another Example 8 abstract class IntTree case class Empty() extends IntTree case class Node(value: Int, left: IntTree, right:IntTree) extends IntTree

How would I add the Ints in such a Tree? object PlusTest extends Application { def plus(tree: IntTree) { tree match { case Empty => 0 case Node(value: Int, left: IntTree, right:IntTree) => value + plus(left) + plus(right) } abstract class IntTree case class Empty() extends IntTree case class Node(value: Int, left: IntTree, right:IntTree) extends IntTree

References 10 A tour of Scala : Traits (see lang.org) Programming in Scala, Martin Odersky et al, Artima press 2009 Beginning Scala, David Pollak, Apress, 2009