Scala Apologia 27-Apr-19.

Slides:



Advertisements
Similar presentations
7-Jun-14 Lists. Arrays and Lists Arrays are a fixed length and occupy sequential locations in memory This makes random access (for example, getting the.
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.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Normal Programs with Cardinality Constraints C = L {a 1,..., a n, not b 1,..., not b m } U L lower bound, U upper bound (missing L: 0, missing U: n+m)
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
1 Programming Languages and Paradigms Lisp Programming.
CS 116 Tutorial 2 Functional Abstraction. Reminders Assignment 2 is due this Wednesday at Noon.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Single assignment store Kernel language syntax Carlos Varela.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
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.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Getting Functional. 2 What is Functional Programming (FP)? In FP, Functions are first-class objects. That is, they are values, just like other objects.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Lisp by Namtap Tapchareon Lisp Background  Lisp was developed by John McCarthy in  Lisp is derives from List Processing Language. 
List Comprehensions. Python’s higher-order functions  Python supports higher-order functions that operate on lists similar to Scheme’s >>> def square(x):
1-Sep-15 Scala Apologia. 2 Java What’s wrong with Java? Not designed for highly concurrent programs The original Thread model was just wrong (it’s been.
Pattern matching. The if expression The else part of an if expression is optional if ( condition ) expression1 else expression2 If the condition evaluates.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Multiparadigm Programming in Scala Adapted from presentation by H. C. Cunningham and J. C. Church University of Mississipi.
Chapter 9: Functional Programming in a Typed Language.
Functions and Methods. Definitions and types A function is a piece of code that takes arguments and returns a result A pure function is a function whose.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
10 – Java Script (3) Informatics Department Parahyangan Catholic University.
Monads. foo1 Method to print a string, then return its length: scala> def foo1(bar: String) = { | println(bar) | bar.size | } foo1: (bar: String)Int scala>
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
6-Feb-16 Scala Apologia. 2 Java What’s wrong with Java? Not designed for highly concurrent programs The original Thread model was just wrong (it’s been.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Getting Functional. Object-Oriented Programming in Scala Scala is object-oriented, and is based on Java’s model An object is a singleton object (there.
23-Feb-16 Lists. Arrays and Lists Arrays are a fixed length and occupy sequential locations in memory This makes random access (for example, getting the.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
21-Mar-16 Getting Started with Scala. Hello World /** Everybody’s first program */ object HelloWorld { def main(args: Array[String]) { println("Hello,
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',
13-Jun-16 Scala Apologia. 2 Java What’s wrong with Java? Not designed for highly concurrent programs The original Thread model was just wrong (it’s been.
Introduction to Functional Programming Part 1 – The Basic Concepts Winter Young
Conditional Expressions
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
From Think Python How to Think Like a Computer Scientist
More Functional Programming
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Getting Functional.
Exceptions and other things
Lists 20-Sep-18.
PROGRAMMING IN HASKELL
Pure Kevin Edelmann.
PROGRAMMING IN HASKELL
Functions As Objects.
PROGRAMMING IN HASKELL
And why they are so useful
Getting Started with Scala
Scala Apologia 1-Jan-19.
Getting Functional.
PROGRAMMING IN HASKELL
Higher Order Functions
Declarative Computation Model Single assignment store (VRH 2
Getting Started with Scala
Clojure 2 22-Apr-19.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Monads.
HW4 Review Case Classes.
Presentation transcript:

Scala Apologia 27-Apr-19

Scala Genealogy Java C C++ Erlang Haskell Simula Prolog Smalltalk ML objects Lisp Prolog C functional programming Smalltalk syntax pattern matching ML C++ Erlang Haskell Java Actors Scala

List construction with :: and Nil Lists are homogeneous: All elements have the same type However, scala> "abc" :: List(1, 2, 3) res15: List[Any] = List(abc, 1, 2, 3) The newly-created list has a type which is the least upper bound An empty list has “nothing” in it scala> List() res16: List[Nothing] = List() The “name” of the empty list is Nil scala> Nil res17: scala.collection.immutable.Nil.type = List() Lists are built from Nil and the :: operator (which is right-associative) scala> 1 :: 2 :: 3 :: Nil res18: List[Int] = List(1, 2, 3) scala> 1 :: (2 :: (3 :: Nil)) res19: List[Int] = List(1, 2, 3)

map The result list doesn’t have to be of the same type map applies a one-parameter function to every element of a List, returning a new List scala> List(1, 2, 3, 4) map (n => 10 * n) res0: List[Int] = List(10, 20, 30, 40) The result list doesn’t have to be of the same type scala> List(1, 2, 3, 4) map (n => n % 2 == 0) res1: List[Boolean] = List(false, true, false, true) Since an element of the list is the only parameter to the function, and it’s only used once, you can abbreviate the function scala> List(1, 2, 3, 4) map (10 * _ + 6) res2: List[Int] = List(16, 26, 36, 46) Of course, you don’t have to use a literal function; you can use any previously defined function (yours or Scala’s) scala> List(-1, 2, -3, 4) map (_ abs) res3: List[Int] = List(1, 2, 3, 4)

flatMap Example: flatten “flattens” a list scala> val nested = List(List(1, 2, 3), List(4, 5)) nested: List[List[Int]] = List(List(1, 2, 3), List(4, 5)) scala> nested flatten res0: List[Int] = List(1, 2, 3, 4, 5) flatMap is like map, but the function given to flatMap is expected to return a list of values; the resultant list of lists is then “flattened” Syntax: def map[B](f: (A) => B): List[B] def flatMap[B](f: (A) => Traversable[B]): List[B] Example: scala> greeting map (word => word.toList) res2: List[List[Char]] = List(List(H, e, l, l, o), List(f, r, o, m), List(S, c, a, l, a)) scala> greeting flatMap (word => word.toList) res3: List[Char] = List(H, e, l, l, o, f, r, o, m, S, c, a, l, a)

filter filter is used to remove unwanted elements from a list, returning a new list scala> List(1, -2, 3, -4) filter (_ > 0) res3: List[Int] = List(1, 3) There is a corresponding (less often used) filterNot method scala> List(1, -2, 3, -4) filterNot (_ > 0) res4: List[Int] = List(-2, -4)