Case Classes in Scala Intro

Slides:



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

More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
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.
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.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
The Singleton Pattern II Recursive Linked Structures.
Hand Trace and Output for: int digit = 0; int number = 1423; do { digit = number % 10; System.out.println(digit); number = number / 10; } while (number.
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.
Reusable Classes.  Motivation: Write less code!
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.
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.
1 Gentle Introduction to Programming Session 5: Memory Model, Object Oriented Programming.
Programming in Scala Chapter 1. Scala: both object-oriented and functional Scala blends –object-oriented and –functional programming in a –statically.
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
RECURSIVE PATTERNS WRITE A START VALUE… THEN WRITE THE PATTERN USING THE WORDS NOW AND NEXT: NEXT = NOW _________.
CSE 341 Lecture 10 more about data types; nullable types; option Ullman ; slides created by Marty Stepp
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
The Scala Programming Language presented by Donna Malayeri.
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.
© 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.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
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.
UNDERSTANDING RECURSIVE CLASSES CMSC 150: Lecture 22.
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 =
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Classes and Objects and Traits And Other Miscellany 25-Jan-16.
A: A: double “4” A: “34” 4.
Support standard JavaScript code with static typing Encapsulation through classes and modules Support for constructors, properties and.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Module 9. Dealing with Generalization Course: Refactoring.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
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.
UMBC CMSC 331 Case Classes in Scala Intro. Basic Properties Case classes are regular classes which export their constructor parameters and which provide.
An overview of Scala and Main Topics  Presentation intent.  What is Scala?  What is LiftWeb?  A trivial chat application  Q&A Author: Marius Danciu.
Introduction to Type Driven Development in Scala
Lecture 19: Binary Trees reading: 17.1 – 17.3
String is a synonym for the type [Char].
Objects as a programming concept
Building Java Programs
Building a Program Generator With Scala
Agenda Warmup AP Exam Review: Litvin A2
Classes and Objects and Traits
CMSC 330: Organization of Programming Languages
Subtyping Rules David Evans cs205: engineering software BlackBear
Review Operation Bingo
Computer Science 312 Haskell Lists 1.
Programming in C# Comparison (Sorting)
CSC 113: Computer programming II
Introduction to Scala Unit 2
Week 4 Object-based Programming (2) Classes and Objects: A Deeper Look
Programming Assignment 2A
Types and Classes in Haskell
PROGRAMMING IN HASKELL
Which best describes the relationship between classes and objects?
UMBC CMSC 331 ONLINE LECTURE 3
CIS 199 Final Review.
The Recursive Descent Algorithm
Classes and Objects and Traits
Chapter 4 Test Review First day
Presentation transcript:

Case Classes in Scala Intro UMBC CMSC 331

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

Example 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 Code Example Fun("x", Fun("y", App(Var("x"), Var("y")))) 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 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

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

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