Functions As Objects.

Slides:



Advertisements
Similar presentations
Lecture 10 Methods COMP1681 / SE15 Introduction to Programming.
Advertisements

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.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
Programming Languages and Paradigms The C Programming Language.
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.
Logic & program control part 2: Simple selection structures.
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.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 Gentle Introduction to Programming Session 2: Functions.
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.
Clojure 3 Recursion, Higher-order-functions 27-Aug-15.
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”
Introduction to Python
Lambdas and Streams. Functional interfaces Functional interfaces are also known as single abstract method (SAM) interfaces. Package java.util.function.
Pattern matching. The if expression The else part of an if expression is optional if ( condition ) expression1 else expression2 If the condition evaluates.
Scala Overview Brandon Clopton CSCI 431. Scala fuses object-oriented and functional programming in a statically typed programming language. It is aimed.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
ITIP © Ron Poet Lecture 12 1 Finding Out About Objects.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
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.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
By Mr. Muhammad Pervez Akhtar
ITP © Ron Poet Lecture 6 1 More on if. ITP © Ron Poet Lecture 6 2 Remembering Tests  We often want to remember the result of a test, so that we can use.
A: A: double “4” A: “34” 4.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.
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.
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Sequences and for loops. Simple for loops A for loop is used to do something with every element of a sequence scala> for (i
Functional Processing of Collections (Advanced) 6.0.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
CS314 – Section 5 Recitation 10
Instant Scala.
University of Central Florida COP 3330 Object Oriented Programming
Introduction to Scheme
Programming Languages and Paradigms
University of Central Florida COP 3330 Object Oriented Programming
Getting Functional.
Exceptions and other things
Programming Paradigms
Functional Processing of Collections (Advanced)
Lists 20-Sep-18.
Important Concepts from Clojure
Important Concepts from Clojure
Nicholas Shahan Spring 2016
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
Pattern Matching.
Getting Started with Scala
Getting Functional.
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
CSE 341 Section 3 Nick Mooney Spring 2017.
Recursion, Higher-order-functions
Important Concepts from Clojure
Nate Brunelle Today: Conditional Decision Statements
COMPUTER PROGRAMMING SKILLS
Names of variables, functions, classes
Millennium High School Agenda Calendar
functions are also data! other functions as input!
Presentation transcript:

Functions As Objects

Literal (“anonymous”) functions This is a literal number: 3.1416 This is a literal string: "Hello there!" This is a literal function: (x: Int, y: Int) => x – y It’s called “anonymous” because we didn’t bother to give it a name The following are equivalent: val subtract = (x: Int, y: Int) => x – y def subtract(x: Int, y: Int) = x – y Changes: No def, no name, the = changes to => (a “rocket”) Functions are values, just like numbers are values They can be saved in variables They can be passed to methods as arguments They can be returned from methods as results

Example use of functions scala> def isEven(x: Int) = x % 2 == 0 isEven: (x: Int)Boolean scala> val isOdd = (x: Int) => x % 2 != 0 isOdd: Int => Boolean = <function1> scala> def printPassingNumbers(predicate: Int => Boolean) { | for (i <- 1 to 10 if predicate(i)) print(i + " ") | println | } printPassingNumbers: (predicate: Int => Boolean)Unit scala> printPassingNumbers(isEven) 2 4 6 8 10 scala> printPassingNumbers(isOdd) 1 3 5 7 9

When to use anonymous functions The body of an anonymous function can be a compound expression (enclosed in { }) scala> printPassingNumbers((n: Int) => { | var prime = true | for (i <- 2 to n - 1 if n % i == 0) prime = false | prime | }) 1 2 3 5 7 This isn’t particularly easy to read The ideal use of an anonymous function is when: The function is short and simple You only need it in one or maybe two places

foreach foreach is an expression that applies a one-argument function to each value in a sequence (list, vector,…) scala> for (i <- List("one", "two")) print(i) onetwo scala> List("one", "two").foreach(print) onetwo scala> Vector("one", "two").foreach(print) onetwo Because foreach is a “binary” method, you can use operator notation scala> List("one", "two") foreach print onetwo The value of a foreach expression is Unit

map map applies a function to each element of a sequence, and returns a sequence of results scala> (1 to 10) map ((x: Int) => x * x) res27: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 4, 9, 16, 25, 36, 49, 64, 81, 100) scala> (1 to 10).toList map ((x: Int) => x * x) res28: List[Int] = List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100) When possible, map returns the same type of sequence as it was given

filter filter applies a predicate (test) to each element of a sequence, and returns a sequence of those values that pass the test scala> (1 to 10) filter ((x: Int) => x % 3 == 0) res29: scala.collection.immutable.IndexedSeq[Int] = Vector(3, 6, 9) When possible, filter returns the same type of sequence as it was given

reduce reduce applies a binary operation between each pair of values, returning a single value as the result scala> (1 to 10) reduce ((x: Int, y: Int) => x + y) res31: Int = 55 scala> List(3, 1, 4, 1, 5, 9, 2, 6, 5) reduce ((x: Int, y: Int) => if (x > y) x else y res30: Int = 9

What’s the point? Scala provides a lot of higher-order functions that take functions as arguments Using these can make your code much shorter and easier to read scala> List(3, 1, 4, 1, 5, 9, 2, 6, 5) exists ((x: Int) => x > 5) res33: Boolean = true scala> List(3, 1, 4, 1, 5, 9, 2, 6, 5) forall ((x: Int) => x > 5) res34: Boolean = false

The End