functional programming in r
TRANSCRIPT
Functional Programming in RA Pragmatic Introduction
Soumendra Prasad Dhanee
Data Scientist.com
18th October, 20148th November, 2014
(delivered at R Meetup, Mumbai)
Soumendra Prasad Dhanee Functional Programming in R Slide 1/51
1 IntroductionGetting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
2 More about functionsProblem 2Anonymous FunctionsProblem 3
3 Applicative ProgrammingMapmapplyFilterNegate
4 Closure
Soumendra Prasad Dhanee Functional Programming in R Slide 2/51
IntroductionMore about functions
Applicative ProgrammingClosure
Overview
We’ll start by reviewing basics
We’ll see the ideas at action first (by solving problems) andabstractions later
We’ll digress a lot
Soumendra Prasad Dhanee Functional Programming in R Slide 3/51
Next ...
1 IntroductionGetting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
2 More about functionsProblem 2Anonymous FunctionsProblem 3
3 Applicative ProgrammingMapmapplyFilterNegate
4 Closure
Soumendra Prasad Dhanee Functional Programming in R Slide 4/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
Writing a function in R
1funct ionname <− f u n c t i o n ( arg1 , arg2 , . . . ) {2<<e x p r e s s i o n s>>3[ r e t u r n (<<e x p r e s s i o n >>) ]4}
return() is optional
last statement of the function body is returned by default
Soumendra Prasad Dhanee Functional Programming in R Slide 5/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
Example of a Function
1squa r e . o f <− f u n c t i o n ( x ) {2x*x3}
Call the function
1squa r e . o f (4 )2y <− squa r e . o f ( x=4)3squa r e . o f ( z=4)
Soumendra Prasad Dhanee Functional Programming in R Slide 6/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
More on Functions
Arguments of R functions can be either required or optional,indicated by syntax
Required arguments have no default value
Optional arguments are defined to have a default value
Soumendra Prasad Dhanee Functional Programming in R Slide 7/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
More on Functions - Example
1add <− f u n c t i o n ( x , y = 2) {2x+y3}
1add (3 )2add (3 , 2 )3add ( x=3)4add ( x=3, y=2)5add ( y=2)6add ( , 3)
Soumendra Prasad Dhanee Functional Programming in R Slide 8/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
Example - Computing Power I
Write a function that computes the kth power of an argument x,using one default argument, and one optional argument, i.e. anargument that has a default value.
We’ll be coming back to this function later on.
Soumendra Prasad Dhanee Functional Programming in R Slide 9/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
Example - Computing Power II
1power <− f u n c t i o n ( x , k=2) xˆk
Soumendra Prasad Dhanee Functional Programming in R Slide 10/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
The apply family of functions
lapply(X, FUN, ...)returns a list of the same length as X, each element of which isthe result of applying FUN to the corresponding element of X
sapply(X, FUN, ...)a user-friendly version of lapply by default returning a vectoror matrix if appropriate
Read the Documentation
Soumendra Prasad Dhanee Functional Programming in R Slide 11/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
Vectorized Functions
... functions that operate on vectors or matrices or dataframes.
Often much faster than looping over a vector, often use.Internal or .Primitive
Higher abstraction - less code to write, less to debug
Soumendra Prasad Dhanee Functional Programming in R Slide 12/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
.Internal and .Primitive - I
log
## function (x, base = exp(1)) .Primitive("log")
paste
## function (..., sep = " ", collapse = NULL)
## .Internal(paste(list(...), sep, collapse))
## <bytecode: 0x1f8a2c8>
## <environment: namespace:base>
Soumendra Prasad Dhanee Functional Programming in R Slide 13/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
.Internal and .Primitive - II
colMeans
## function (x, na.rm = FALSE, dims = 1L)
## {
## if (is.data.frame(x))
## x <- as.matrix(x)
## if (!is.array(x) || length(dn <- dim(x)) < 2L)
## stop("'x' must be an array of at least two dimensions")
## if (dims < 1L || dims > length(dn) - 1L)
## stop("invalid 'dims'")
## n <- prod(dn[1L:dims])
## dn <- dn[-(1L:dims)]
## z <- if (is.complex(x))
## .Internal(colMeans(Re(x), n, prod(dn), na.rm)) + (0+1i) *
## .Internal(colMeans(Im(x), n, prod(dn), na.rm))
## else .Internal(colMeans(x, n, prod(dn), na.rm))
## if (length(dn) > 1L) {
## dim(z) <- dn
## dimnames(z) <- dimnames(x)[-(1L:dims)]
## }
## else names(z) <- dimnames(x)[[dims + 1]]
## z
## }
## <bytecode: 0x2ccce40>
## <environment: namespace:base>
Soumendra Prasad Dhanee Functional Programming in R Slide 14/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
Examples of Vectorized Functions
ifelse()
is.na()
log()
sqrt()
rnorm()
colMeans()
rowSums()
x > y
x == y
!x
Soumendra Prasad Dhanee Functional Programming in R Slide 15/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
Primality Testing I
Given a number, decide if it is a prime or not.
Hint 1 : min(x) gives the lowest element of vector x
min(c(2, 1, 3))
## [1] 1
Soumendra Prasad Dhanee Functional Programming in R Slide 16/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
Primality Testing II
1min ( x%%( 2 : ( x−1) ) )>0
The Deconstruction
12 : ( x−1)2x%%( 2 : ( x−1) )3min ( x%%( 2 : ( x−1) ) )>0
Soumendra Prasad Dhanee Functional Programming in R Slide 17/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
Lists of functions
x <- 1:10
funs <- list(
sum = sum,
mean = mean,
median = median
)
sapply(funs, function(f) f(x))
## sum mean median
## 55.0 5.5 5.5
Soumendra Prasad Dhanee Functional Programming in R Slide 18/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
Let’s get started
Problem 1InputA numeric vector (1:200000)
OutputA vector containing the even numbers in the inputvector, listed in the order they appeared in the inputvector
Soumendra Prasad Dhanee Functional Programming in R Slide 19/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
Solution - for loop I
Solution using for loop
1x <− 1:2000002ans <− l o g i c a l (200000)3f o r ( i i n x )4{5i f ( i%%2==0) { ans [ i ] = TRUE } e l s e {6ans [ i ] = FALSE }7}8x [ ans ]
## user system elapsed
## 0.542 0.005 0.546
Soumendra Prasad Dhanee Functional Programming in R Slide 20/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
Solution - for loop II
Solution using for loop
1x <− 1:2000002ans <− c ( )3f o r ( i i n x )4{5i f ( i%%2==0) { ans [ i ] = TRUE } e l s e {6ans [ i ] = FALSE }7}8x [ ans ]
## user system elapsed
## 56.511 2.631 59.333
Soumendra Prasad Dhanee Functional Programming in R Slide 21/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
Solution - vectorized functions
Solution using vectorized functions
1x <− 1:2000002ans <− x%%2==03x [ ans ]
## user system elapsed
## 0.013 0.000 0.013
Soumendra Prasad Dhanee Functional Programming in R Slide 22/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
Solution - higher-order functions
Solution using higher-order functions
1x <− 1:2000002ans <− s a pp l y ( x , f u n c t i o n ( i ) i%%2==0)3x [ ans ]
## user system elapsed
## 0.554 0.011 0.566
Soumendra Prasad Dhanee Functional Programming in R Slide 23/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
What are higher-order functions?
Higher-order FunctionsA higher-order function (also functional form, functional orfunctor) is a function that does at least one of the following:
takes one or more functions as an input
outputs a function
-source - Wikipedia
Soumendra Prasad Dhanee Functional Programming in R Slide 24/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
An example
sapplysapply(X, FUN, ...)
Soumendra Prasad Dhanee Functional Programming in R Slide 25/51
IntroductionMore about functions
Applicative ProgrammingClosure
Getting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
What are first-class functions?
First-class FunctionsFirst-class functions are functions that can be treated like anyother piece of data ...
can be stored in a variable
can be stored in a list
can be stored in an object
can be passed to other functions
can be returned from other functions
... just like any other piece of data.
Soumendra Prasad Dhanee Functional Programming in R Slide 26/51
Next ...
1 IntroductionGetting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
2 More about functionsProblem 2Anonymous FunctionsProblem 3
3 Applicative ProgrammingMapmapplyFilterNegate
4 Closure
Soumendra Prasad Dhanee Functional Programming in R Slide 27/51
IntroductionMore about functions
Applicative ProgrammingClosure
Problem 2Anonymous FunctionsProblem 3
Rounding off
Problem 2aInputA numeric vector (rnorm(100, 0, 1))
OutputA vector containing all the numbers in the input vectorrounded to the nearest integer
Soumendra Prasad Dhanee Functional Programming in R Slide 28/51
IntroductionMore about functions
Applicative ProgrammingClosure
Problem 2Anonymous FunctionsProblem 3
Solution 2a - Rounding off
Solution using sapply
1s a pp l y ( rnorm (100 , 0 , 1) , round )
Soumendra Prasad Dhanee Functional Programming in R Slide 29/51
IntroductionMore about functions
Applicative ProgrammingClosure
Problem 2Anonymous FunctionsProblem 3
Rounding off
Problem 2bInputA numeric vector (rnorm(100, 0, 1))
OutputA vector containing all the numbers in the input vectorrounded to two decimal places
Soumendra Prasad Dhanee Functional Programming in R Slide 30/51
IntroductionMore about functions
Applicative ProgrammingClosure
Problem 2Anonymous FunctionsProblem 3
Solution 2b - Rounding off
Solution using sapply
1s a pp l y ( rnorm (100 , 0 , 1) , f u n c t i o n ( x ) round ( x , 2) )
Soumendra Prasad Dhanee Functional Programming in R Slide 31/51
IntroductionMore about functions
Applicative ProgrammingClosure
Problem 2Anonymous FunctionsProblem 3
Anonymous Functions
Anonymous FunctionsAnonymous function is a function that is not bound to anidentifier.source - Wikipedia
Soumendra Prasad Dhanee Functional Programming in R Slide 32/51
IntroductionMore about functions
Applicative ProgrammingClosure
Problem 2Anonymous FunctionsProblem 3
Acting on Matrices
Problem 3InputA 3-by-3 matrix (matrix(1:9, ncol=3))
OutputAdd 1 to row 1, 4 to row 2, 7 to row 3
Soumendra Prasad Dhanee Functional Programming in R Slide 33/51
IntroductionMore about functions
Applicative ProgrammingClosure
Problem 2Anonymous FunctionsProblem 3
Solution 3 - Matrix Addition
Solution using sweep
1m <− mat r i x ( 1 : 9 , n co l =3)2sweep (m, 1 , c (1 , 4 , 7) , ”+” )
## [,1] [,2] [,3]
## [1,] 2 5 8
## [2,] 6 9 12
## [3,] 10 13 16
Soumendra Prasad Dhanee Functional Programming in R Slide 34/51
Next ...
1 IntroductionGetting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
2 More about functionsProblem 2Anonymous FunctionsProblem 3
3 Applicative ProgrammingMapmapplyFilterNegate
4 Closure
Soumendra Prasad Dhanee Functional Programming in R Slide 35/51
IntroductionMore about functions
Applicative ProgrammingClosure
MapmapplyFilterNegate
Applicative Programming
Calling by function B of function A, where function A wasoriginally supplied to function B as an argument.
Examplesmap
reduce (fold)
filter
Soumendra Prasad Dhanee Functional Programming in R Slide 36/51
IntroductionMore about functions
Applicative ProgrammingClosure
MapmapplyFilterNegate
Map
Map(f, x, ...)
1
2f u n c t i o n ( f , . . . )3{4f <− match . fun ( f )5mapply (FUN = f , . . . , SIMPLIFY = FALSE)6}7<bytecode : 0x402ad90>8<env i ronment : namespace : base>
Soumendra Prasad Dhanee Functional Programming in R Slide 37/51
IntroductionMore about functions
Applicative ProgrammingClosure
MapmapplyFilterNegate
mapply
mapply is a multivariate version of sapply.
1mapply ( rep , 1 : 4 , 4 : 1 )2
3mapply ( rep , t imes = 1 : 4 , x = 4 : 1 )4
5mapply ( rep , x = 1 : 4 , t imes = 4 : 1 )
Soumendra Prasad Dhanee Functional Programming in R Slide 38/51
IntroductionMore about functions
Applicative ProgrammingClosure
MapmapplyFilterNegate
Filter I
Choose all the even numbers.
1F i l t e r ( f u n c t i o n ( x ) x%%2 , 1 :200000)
## user system elapsed
## 0.419 0.004 0.424
Soumendra Prasad Dhanee Functional Programming in R Slide 39/51
IntroductionMore about functions
Applicative ProgrammingClosure
MapmapplyFilterNegate
Filter II
The previous solution was wrong.
Now what is the output of the following -
1F i l t e r ( c (T, F) , 1 :200000)
Soumendra Prasad Dhanee Functional Programming in R Slide 40/51
IntroductionMore about functions
Applicative ProgrammingClosure
MapmapplyFilterNegate
Filter III
1f u n c t i o n ( f , x )2{3i nd <− as . l o g i c a l ( u n l i s t ( l a p p l y ( x , f ) ) )4x [ ! i s . na ( i nd ) & ind ]5}6<bytecode : 0 x4 f87 f00>7<env i ronment : namespace : base>
Soumendra Prasad Dhanee Functional Programming in R Slide 41/51
IntroductionMore about functions
Applicative ProgrammingClosure
MapmapplyFilterNegate
Negate I
What is the outcome?
1Negate ( c (T, F) , 1 : 10 )
Soumendra Prasad Dhanee Functional Programming in R Slide 42/51
IntroductionMore about functions
Applicative ProgrammingClosure
MapmapplyFilterNegate
Negate II
What is the outcome?
1F i l t e r ( Negate ( f u n c t i o n ( x ) x%%2) , 1 : 10 )
Soumendra Prasad Dhanee Functional Programming in R Slide 43/51
IntroductionMore about functions
Applicative ProgrammingClosure
MapmapplyFilterNegate
Negate III
1f u n c t i o n ( f )2{3f <− match . fun ( f )4f u n c t i o n ( . . . ) ! f ( . . . )5}6<bytecode : 0 x4e9c250>7<env i ronment : namespace : base>
Soumendra Prasad Dhanee Functional Programming in R Slide 44/51
Next ...
1 IntroductionGetting StartedVectorizationVectorization in PracticeProblem 1Higher-order FunctionsFirst-class Functions
2 More about functionsProblem 2Anonymous FunctionsProblem 3
3 Applicative ProgrammingMapmapplyFilterNegate
4 Closure
Soumendra Prasad Dhanee Functional Programming in R Slide 45/51
IntroductionMore about functions
Applicative ProgrammingClosure
Computing Power Revisited
Write a function that computes the kth power of an argument x,using one default argument, and one optional argument, i.e. anargument that has a default value.
1power <− f u n c t i o n ( x , k=2) {2xˆk3}
Soumendra Prasad Dhanee Functional Programming in R Slide 46/51
IntroductionMore about functions
Applicative ProgrammingClosure
Function Factory with Closures
A function returning a function:
1power <− f u n c t i o n ( exponent ) {2f u n c t i o n ( x ) {3x ˆ exponent4}5}6
7squa r e <− power (2 )8squa r e (2 )9squa r e (4 )10
11cube <− power (3 )12cube (2 )13cube (4 )
Soumendra Prasad Dhanee Functional Programming in R Slide 47/51
IntroductionMore about functions
Applicative ProgrammingClosure
What are Closures?
An object is data with functions. A closure
is a function with data.
- John D. Cook
... an abstraction binding a function to its scope.- Wikipedia
Closures get their name because they enclose theenvironment of the parent function and can access all itsvariables.- Hadley Wickham
Soumendra Prasad Dhanee Functional Programming in R Slide 48/51
IntroductionMore about functions
Applicative ProgrammingClosure
Closer look at closures I
square
## function(x) {
## x ^ exponent
## }
## <environment: 0x217dcf8>
cube
## function(x) {
## x ^ exponent
## }
## <environment: 0x26cb1d0>
Soumendra Prasad Dhanee Functional Programming in R Slide 49/51
IntroductionMore about functions
Applicative ProgrammingClosure
Closer look at closures II
as.list(environment(square))
## $exponent
## [1] 2
as.list(environment(cube))
## $exponent
## [1] 3
Soumendra Prasad Dhanee Functional Programming in R Slide 50/51
IntroductionMore about functions
Applicative ProgrammingClosure
Closer look at closures III
1l i b r a r y ( p r y r )2unenc l o s e ( squa r e )3#> f u n c t i o n ( x )4#> {5#> xˆ26#> }7unenc l o s e ( cube )8#> f u n c t i o n ( x )9#> {10#> xˆ311#> }
Soumendra Prasad Dhanee Functional Programming in R Slide 51/51