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.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

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.
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Modern Programming Languages, 2nd ed.
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
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.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Pattern Matching. The match statement C and Java have a switch statement which uses a small integer value to choose among alternatives In Java 7, it is.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
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.
Getting Functional. 2 What is Functional Programming (FP)? In FP, Functions are first-class objects. That is, they are values, just like other objects.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Python Control of Flow.
Methods. Why methods? A method gives a name to something that you want to do, so you don’t have to think about how to do it, you just do it The name should.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Scala Parallel Collections Aleksandar Prokopec EPFL.
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”
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
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 and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Scala Parallel Collections Aleksandar Prokopec, Tiark Rompf Scala Team EPFL.
CPS120: Introduction to Computer Science Decision Making in Programs.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Python Functions.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Scala Parallel Collections Aleksandar Prokopec EPFL.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
CS162 Week 1 Kyle Dewey. Overview Basic Introduction CS Accounts Scala survival guide.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
Computer Science 111 Fundamentals of Programming I Default and Optional Parameters Higher-Order Functions.
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>
Chapter SevenModern Programming Languages1 A Second Look At ML.
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.
Sequences and for loops. Simple for loops A for loop is used to do something with every element of a sequence scala> for (i
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Q and A for Section 4.4 CS 106, Fall If statement syntax Q: The syntax for an if statement (or conditional statement) is: if _______________ : _____________.
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
Types CSCE 314 Spring 2016.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Getting Functional.
Important Concepts from Clojure
Important Concepts from Clojure
PROGRAMMING IN HASKELL
Functions As Objects.
Building Java Programs
PROGRAMMING IN HASKELL
Pattern Matching.
Getting Functional.
CSC1018F: Intermediate Python
Chapter 4: Control Structures I (Selection)
Higher Order Functions
(more) Python.
Important Concepts from Clojure
PROGRAMMING IN HASKELL
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Lisp.
Monads.
Presentation transcript:

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 result depends only on its arguments You call a function If called again with the same arguments, a pure function will return the same result A method is a function that belongs to an object You “talk” to the object and ask it to run its method More formally, you send a message to the object 2

Syntax Method (in a class or object): def name (args ) = expression scala> class Person(name: String) { | def doubleName = name * 2 | } defined class Person scala> val jo = new Person("Jo") jo: Person = scala> jo.doubleName res4: String = JoJo Function: (args ) => expression scala> val mirror = (name: String) => name + name.reverse mirror: String => String = scala> mirror("Dave") res6: String = DaveevaD 3

Operators and methods Reminder: Operators are really methods scala> res0: Int = 4 scala> 2.+(2) res1: Int = 4 Binary (object + one argument) methods can be treated like operators scala> List(1, 2, 3, 4).contains(3) res2: Boolean = true scala> List(1, 2, 3, 4) contains 3 res3: Boolean = true 4

Function types Functions are objects, and every object has a type The type of (name: String) => name + name.reverse is String => String scala> (s: String, from: Int, to: Int) => s.drop(from).take(to - from + 1) res4: (String, Int, Int) => String = scala> res4("paper", 1, 3) res5: String = ape So the type of a function is ( types of its arguments ) => type of its result 5

Anonymous functions Functions can be given names scala> val foo = (n: Int) => n * (n - 1) foo: Int => Int = And then they can be called by name scala> foo(5) res1: Int = 20 But they don’t have to have a name to be called scala> ((n: Int) => n * (n - 1))(5) res2: Int = 20 6

Higher-order functions A higher-order function is one that either takes one or more functions as arguments, or returns a function as a result, or both Scala provides a number of higher-order functions, including the “big three”: map, filter, and reduce map applies a function to each element of a sequence, returning a sequence of the results filter applies a predicate (Boolean function) to each element of a sequence, and returns a sequence of those elements that satisfy the predicate (the predicate returns true ) reduce repeatedly applies a binary operation to pairs of elements of a sequence, returning a single value

map map applies a function to each element of a sequence, returning a sequence of the results scala> Array(1, 2, 3).map((x: Int) => x * x) res11: Array[Int] = Array(1, 4, 9) scala> Vector(1, 2, 3) map ((x: Int) => x * x) res12: scala.collection.immutable.Vector[Int] = Vector(1, 4, 9) scala> List(1, 2, 3) map ((x: Int) => x * x) res13: List[Int] = List(1, 4, 9) scala> Range(1, 6) map ((x: Int) => x * x) res15: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 4, 9, 16, 25) scala> (1 until 6) map ((x: Int) => x * x) res16: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 4, 9, 16, 25) scala> "boogie" map ((ch: Char) => "aeiou" contains ch) res17: scala.collection.immutable.IndexedSeq[Boolean] = Vector(false, true, true, false, true, true) 8

More about map scala> def addS(str: String) = str + "s" addS: (str: String)String scala> List("dog", "cat", "horse") map addS res0: List[String] = List(dogs, cats, horses) scala> List(1, 2, 3) map addS :9: error: type mismatch; found : String => String required: Int => ? List(1, 2, 3) map addS ^ scala> def addS(x: Any) = x + "s" addS: (x: Any)String scala> List(1, 2, 3) map addS res2: List[String] = List(1s, 2s, 3s) 9

filter filter applies a predicate (Boolean function) to each element of a sequence, and returns a sequence of those elements that satisfy the predicate (the predicate returns true ) scala> List(3, 1, 4, 1, 5, 9) filter ((x: Int) => x > 3) res0: List[Int] = List(4, 5, 9) scala> "University" filter ((ch: Char) => ch > 'm') res1: String = nvrsty scala> (2 to 20) filter ((x: Int) => isPrime(x)) res9: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 3, 5, 7, 11, 13, 17, 19) scala> "Scala is a good language".split(" ") filter ((w: String) => w.length >= 5) res10: Array[String] = Array(Scala, language) 10

reduce reduce repeatedly applies a binary operation to pairs of elements of a sequence, returning a single value scala> (1 to 10) reduce ((x: Int, y: Int) => x + y) res12: Int = 55 scala> (1 to 10) reduce ((x: Int, y: Int) => x * y) res13: Int = scala> List("one", "two", "three") reduce ((x: String, y: String) => x + y) res15: String = onetwothree Of course, many of these are already supplied by Scala scala> (1 to 10).sum res16: Int = 55 scala> (1 to 10).product res17: Int = scala> List("one", "two", "three").mkString res21: String = onetwothree

Shortcuts When you define a method with def, you must specify the types of the parameter With literal functions, if the parameter type is obvious, you usually don’t need to specify it scala> List(3, 1, 4, 1, 5, 9) filter ((x) => x > 3) res0: List[Int] = List(4, 5, 9) With a single parameter and an inferred type, you can also leave out the parentheses scala> List(3, 1, 4, 1, 5, 9) filter (x => x > 3) res1: List[Int] = List(4, 5, 9) In fact, with a single parameter whose type is obvious, you can leave out the parameter name, and just use an underscore scala> List(3, 1, 4, 1, 5, 9) filter (_ > 3) res2: List[Int] = List(4, 5, 9) 12

Splitting lists scala> "one two three" takeWhile ((ch: Char) => ch != ' ') res0: String = one scala> "one two three" takeWhile (_ != ' ') res1: String = one scala> "one two three" dropWhile (_ != ' ') res2: String = " two three" scala> "one two three" span (_ != ' ') res3: (String, String) = (one," two three") scala> "one two three" partition (_ != ' ') res4: (String, String) = (onetwothree," ") scala> List(3, 5, 6, 8, 9) partition (_ % 2 == 0) res4: (List[Int], List[Int]) = (List(6, 8),List(3, 5, 9)) 13

Testing all elements sequence.forall( predicate ) checks if every element of the sequence satisfies the predicate scala> List(1, 2, 3) forall (_ > 0) res0: Boolean = true scala> List(1, -2, 3) forall (_ > 0) res1: Boolean = false sequence.exists( predicate ) checks if any element of the sequence satisfies the predicate scala> List(1, 2, 3) exists (_ < 0) res2: Boolean = false scala> List(1, -2, 3) exists (_ < 0) res3: Boolean = true 14

Extreme underscores If you have more than one parameter, you can sometimes use an underscore for each The first underscore stands for the first parameter, the second underscore for the second parameter, etc. scala> List(5, 3, 4, 2, 1) sortWith (_ < _) res1: List[Int] = List(1, 2, 3, 4, 5) scala> "This is a list of words".split(" ") sortWith (_.length < _.length) res2: Array[String] = Array(a, is, of, This, list, words) 15

find list.find( predicate ) returns, as Some( value ), the first value in the sequence that satisfies the predicate, or None if no such value is found scala> List(3, 1, 4, 1, 6) find (_ > 3) res5: Option[Int] = Some(4) scala> List(3, 1, 4, 1, 6) find (_ > 7) res6: Option[Int] = None scala> "Read the assignment carefully".split(" ") find (_.length > 6) res7: Option[String] = Some(assignment) I’ll review Option in just a moment 16

find with Strings scala> val digits = Math.PI.toString digits: String = scala> List(3, 1, 4, 1, 6) find (_ > 3) res0: Option[Int] = Some(4) scala> digits find (_ > 3) res1: Option[Char] = Some(3) scala> digits find (_ > '3') res2: Option[Char] = Some(4) scala> 3 == '3' res3: Boolean = false scala> '3'.toInt res4: Int = 51 17

Working with an Option You can match on an Option val opt = men find (x => isHonest(x)) opt match { case Some(man) => println(s"$man is an honest man.") case None => println("Not found.") } An Option is a collection (of zero or one thing), so you can use collection operations on it scala> val abc = Some("abc") abc: Some[String] = Some(abc) scala> abc.isEmpty res5: Boolean = false scala> abc.isDefined res6: Boolean = true scala> for (a <- abc) println(a) abc scala> abc getOrElse("xyz") res8: String = abc 18

foreach Unlike the previously discussed higher-order functions, the return value of foreach is Unit, () foreach does something with each element of a sequence, and is used for its side effects scala> (1 to 10) foreach (x => print(x * x + " ")) scala> var sum = 0; (1 to 10) foreach (x => sum += x * x) sum: Int = 385 Scala is “multi-paradigm”: It’s object-oriented and functional Functional languages don’t allow, or at least try to avoid, side effects The entire purpose of foreach is to have side effects! If you want to get side effects from a higher-order function, use foreach in preference to any of the others 19

for comprehensions A for comprehension is a convenient way to combine a number of map and filter operations scala> (1 to 20) filter (isPrime _) map (x => x * x) res1: scala.collection.immutable.IndexedSeq[Int] = Vector(4, 9, 25, 49, 121, 169, 289, 361) scala> for (i <- 1 to 20 if isPrime(i)) yield (i * i) res2: scala.collection.immutable.IndexedSeq[Int] = Vector(4, 9, 25, 49, 121, 169, 289, 361) 20

Why higher-order functions? Use of higher-order functions makes code shorter and (usually) easier to read With for comprension: scala> (1 to 10) map (x => x * x) res27: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 4, 9, 16, 25, 36, 49, 64, 81, 100) Just as a loop: scala> var v: Vector[Int] = Vector() v: Vector[Int] = Vector() scala> for (i v res29: Vector[Int] = Vector(1, 4, 9, 16, 25, 36, 49, 64, 81, 100) Higher-order functions make certain tasks much easier Just like anything else, learning to use higher-order functions easily and effectively takes practice 21

The End 22