Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Functional Programming in R

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Functional Programming in R

Given what a beautiful and mature functional programming language R is, there is a surprising, though understandable, lack of visibility of functional programming techniques in R. This is a talk given to the Mumbai R meetup group in October/November, 2014, meant to introduce the audience to Functional Programming in R.

Avatar for Soumendra Prasad Dhanee

Soumendra Prasad Dhanee

November 08, 2014

Other Decks in Programming

Transcript

  1. Functional Programming in R A Pragmatic Introduction Soumendra Prasad Dhanee

    Data Scientist .com 18th October, 2014 8th November, 2014 (delivered at R Meetup, Mumbai) Soumendra Prasad Dhanee Functional Programming in R Slide 1/51
  2. 1 Introduction Getting Started Vectorization Vectorization in Practice Problem 1

    Higher-order Functions First-class Functions 2 More about functions Problem 2 Anonymous Functions Problem 3 3 Applicative Programming Map mapply Filter Negate 4 Closure Soumendra Prasad Dhanee Functional Programming in R Slide 2/51
  3. Introduction More about functions Applicative Programming Closure Overview We’ll start

    by reviewing basics We’ll see the ideas at action first (by solving problems) and abstractions later We’ll digress a lot Soumendra Prasad Dhanee Functional Programming in R Slide 3/51
  4. Next ... 1 Introduction Getting Started Vectorization Vectorization in Practice

    Problem 1 Higher-order Functions First-class Functions 2 More about functions Problem 2 Anonymous Functions Problem 3 3 Applicative Programming Map mapply Filter Negate 4 Closure Soumendra Prasad Dhanee Functional Programming in R Slide 4/51
  5. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-class Functions Writing a function in R 1 functionname <− 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
  6. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-class Functions Example of a Function 1 square . of <− f u n c t i o n ( x ) { 2 x*x 3 } Call the function 1 square . of (4) 2 y <− square . of ( x=4) 3 square . of ( z=4) Soumendra Prasad Dhanee Functional Programming in R Slide 6/51
  7. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-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
  8. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-class Functions More on Functions - Example 1 add <− f u n c t i o n ( x , y = 2) { 2 x+y 3 } 1 add (3) 2 add (3 ,2) 3 add ( x=3) 4 add ( x=3, y=2) 5 add ( y=2) 6 add ( , 3) Soumendra Prasad Dhanee Functional Programming in R Slide 8/51
  9. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-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. an argument 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
  10. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-class Functions Example - Computing Power II 1 power <− f u n c t i o n ( x , k=2) xˆk Soumendra Prasad Dhanee Functional Programming in R Slide 10/51
  11. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-class Functions The apply family of functions lapply(X, FUN, ...) returns a list of the same length as X, each element of which is the result of applying FUN to the corresponding element of X sapply(X, FUN, ...) a user-friendly version of lapply by default returning a vector or matrix if appropriate Read the Documentation Soumendra Prasad Dhanee Functional Programming in R Slide 11/51
  12. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-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
  13. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-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
  14. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-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 dimen ## 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)) + Soumendra Prasad Dhanee Functional Programming in R Slide 14/51
  15. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-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
  16. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-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
  17. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-class Functions Primality Testing II 1 min ( x%%( 2 : ( x−1) ) )>0 The Deconstruction 1 2 : ( x−1) 2 x%%( 2 : ( x−1) ) 3 min ( x%%( 2 : ( x−1) ) )>0 Soumendra Prasad Dhanee Functional Programming in R Slide 17/51
  18. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-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
  19. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-class Functions Let’s get started Problem 1 Input A numeric vector (1:200000) Output A vector containing the even numbers in the input vector, listed in the order they appeared in the input vector Soumendra Prasad Dhanee Functional Programming in R Slide 19/51
  20. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-class Functions Solution - for loop I Solution using for loop 1 x <− 1:200000 2 ans <− l o g i c a l (200000) 3 f o r ( i i n x ) 4 { 5 i f ( i%%2==0) { ans [ i ] = TRUE } e l s e { 6 ans [ i ] = FALSE } 7 } 8 x [ ans ] ## user system elapsed ## 0.542 0.005 0.546 Soumendra Prasad Dhanee Functional Programming in R Slide 20/51
  21. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-class Functions Solution - for loop II Solution using for loop 1 x <− 1:200000 2 ans <− c () 3 f o r ( i i n x ) 4 { 5 i f ( i%%2==0) { ans [ i ] = TRUE } e l s e { 6 ans [ i ] = FALSE } 7 } 8 x [ ans ] ## user system elapsed ## 56.511 2.631 59.333 Soumendra Prasad Dhanee Functional Programming in R Slide 21/51
  22. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-class Functions Solution - vectorized functions Solution using vectorized functions 1 x <− 1:200000 2 ans <− x%%2==0 3 x [ ans ] ## user system elapsed ## 0.013 0.000 0.013 Soumendra Prasad Dhanee Functional Programming in R Slide 22/51
  23. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-class Functions Solution - higher-order functions Solution using higher-order functions 1 x <− 1:200000 2 ans <− sapply ( x , f u n c t i o n ( i ) i%%2==0) 3 x [ ans ] ## user system elapsed ## 0.554 0.011 0.566 Soumendra Prasad Dhanee Functional Programming in R Slide 23/51
  24. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-class Functions What are higher-order functions? Higher-order Functions A higher-order function (also functional form, functional or functor) 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
  25. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-class Functions An example sapply sapply(X, FUN, ...) Soumendra Prasad Dhanee Functional Programming in R Slide 25/51
  26. Introduction More about functions Applicative Programming Closure Getting Started Vectorization

    Vectorization in Practice Problem 1 Higher-order Functions First-class Functions What are first-class functions? First-class Functions First-class functions are functions that can be treated like any other 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
  27. Next ... 1 Introduction Getting Started Vectorization Vectorization in Practice

    Problem 1 Higher-order Functions First-class Functions 2 More about functions Problem 2 Anonymous Functions Problem 3 3 Applicative Programming Map mapply Filter Negate 4 Closure Soumendra Prasad Dhanee Functional Programming in R Slide 27/51
  28. Introduction More about functions Applicative Programming Closure Problem 2 Anonymous

    Functions Problem 3 Rounding off Problem 2a Input A numeric vector (rnorm(100, 0, 1)) Output A vector containing all the numbers in the input vector rounded to the nearest integer Soumendra Prasad Dhanee Functional Programming in R Slide 28/51
  29. Introduction More about functions Applicative Programming Closure Problem 2 Anonymous

    Functions Problem 3 Solution 2a - Rounding off Solution using sapply 1 sapply ( rnorm (100 , 0 , 1) , round ) Soumendra Prasad Dhanee Functional Programming in R Slide 29/51
  30. Introduction More about functions Applicative Programming Closure Problem 2 Anonymous

    Functions Problem 3 Rounding off Problem 2b Input A numeric vector (rnorm(100, 0, 1)) Output A vector containing all the numbers in the input vector rounded to two decimal places Soumendra Prasad Dhanee Functional Programming in R Slide 30/51
  31. Introduction More about functions Applicative Programming Closure Problem 2 Anonymous

    Functions Problem 3 Solution 2b - Rounding off Solution using sapply 1 sapply ( 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
  32. Introduction More about functions Applicative Programming Closure Problem 2 Anonymous

    Functions Problem 3 Anonymous Functions Anonymous Functions Anonymous function is a function that is not bound to an identifier. source - Wikipedia Soumendra Prasad Dhanee Functional Programming in R Slide 32/51
  33. Introduction More about functions Applicative Programming Closure Problem 2 Anonymous

    Functions Problem 3 Acting on Matrices Problem 3 Input A 3-by-3 matrix (matrix(1:9, ncol=3)) Output Add 1 to row 1, 4 to row 2, 7 to row 3 Soumendra Prasad Dhanee Functional Programming in R Slide 33/51
  34. Introduction More about functions Applicative Programming Closure Problem 2 Anonymous

    Functions Problem 3 Solution 3 - Matrix Addition Solution using sweep 1 m <− matrix ( 1 : 9 , ncol =3) 2 sweep (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
  35. Next ... 1 Introduction Getting Started Vectorization Vectorization in Practice

    Problem 1 Higher-order Functions First-class Functions 2 More about functions Problem 2 Anonymous Functions Problem 3 3 Applicative Programming Map mapply Filter Negate 4 Closure Soumendra Prasad Dhanee Functional Programming in R Slide 35/51
  36. Introduction More about functions Applicative Programming Closure Map mapply Filter

    Negate Applicative Programming Calling by function B of function A, where function A was originally supplied to function B as an argument. Examples map reduce (fold) filter Soumendra Prasad Dhanee Functional Programming in R Slide 36/51
  37. Introduction More about functions Applicative Programming Closure Map mapply Filter

    Negate Map Map(f, x, ...) 1 2 f u n c t i o n ( f , . . . ) 3 { 4 f <− match . fun ( f ) 5 mapply (FUN = f , . . . , SIMPLIFY = FALSE) 6 } 7 <bytecode : 0x402ad90> 8 <environment : namespace : base> Soumendra Prasad Dhanee Functional Programming in R Slide 37/51
  38. Introduction More about functions Applicative Programming Closure Map mapply Filter

    Negate mapply mapply is a multivariate version of sapply. 1 mapply ( rep , 1:4 , 4 : 1) 2 3 mapply ( rep , times = 1:4 , x = 4 :1 ) 4 5 mapply ( rep , x = 1:4 , times = 4 : 1 ) Soumendra Prasad Dhanee Functional Programming in R Slide 38/51
  39. Introduction More about functions Applicative Programming Closure Map mapply Filter

    Negate Filter I Choose all the even numbers. 1 F 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
  40. Introduction More about functions Applicative Programming Closure Map mapply Filter

    Negate Filter II The previous solution was wrong. Now what is the output of the following - 1 F i l t e r ( c (T, F) , 1:200000) Soumendra Prasad Dhanee Functional Programming in R Slide 40/51
  41. Introduction More about functions Applicative Programming Closure Map mapply Filter

    Negate Filter III 1 f u n c t i o n ( f , x ) 2 { 3 ind <− as . l o g i c a l ( u n l i s t ( l a p p l y ( x , f ) ) ) 4 x [ ! i s . na ( ind ) & ind ] 5 } 6 <bytecode : 0 x4f87f00 > 7 <environment : namespace : base> Soumendra Prasad Dhanee Functional Programming in R Slide 41/51
  42. Introduction More about functions Applicative Programming Closure Map mapply Filter

    Negate Negate I What is the outcome? 1 Negate ( c (T, F) , 1:10) Soumendra Prasad Dhanee Functional Programming in R Slide 42/51
  43. Introduction More about functions Applicative Programming Closure Map mapply Filter

    Negate Negate II What is the outcome? 1 F 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
  44. Introduction More about functions Applicative Programming Closure Map mapply Filter

    Negate Negate III 1 f u n c t i o n ( f ) 2 { 3 f <− match . fun ( f ) 4 f u n c t i o n ( . . . ) ! f ( . . . ) 5 } 6 <bytecode : 0x4e9c250> 7 <environment : namespace : base> Soumendra Prasad Dhanee Functional Programming in R Slide 44/51
  45. Next ... 1 Introduction Getting Started Vectorization Vectorization in Practice

    Problem 1 Higher-order Functions First-class Functions 2 More about functions Problem 2 Anonymous Functions Problem 3 3 Applicative Programming Map mapply Filter Negate 4 Closure Soumendra Prasad Dhanee Functional Programming in R Slide 45/51
  46. Introduction More about functions Applicative Programming Closure 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. an argument that has a default value. 1 power <− f u n c t i o n ( x , k=2) { 2 xˆk 3 } Soumendra Prasad Dhanee Functional Programming in R Slide 46/51
  47. Introduction More about functions Applicative Programming Closure Function Factory with

    Closures A function returning a function: 1 power <− f u n c t i o n ( exponent ) { 2 f u n c t i o n ( x ) { 3 x ˆ exponent 4 } 5 } 6 7 square <− power (2) 8 square (2) 9 square (4) 10 11 cube <− power (3) 12 cube (2) 13 cube (4) Soumendra Prasad Dhanee Functional Programming in R Slide 47/51
  48. Introduction More about functions Applicative Programming Closure 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 the environment of the parent function and can access all its variables. - Hadley Wickham Soumendra Prasad Dhanee Functional Programming in R Slide 48/51
  49. Introduction More about functions Applicative Programming Closure 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
  50. Introduction More about functions Applicative Programming Closure 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
  51. Introduction More about functions Applicative Programming Closure Closer look at

    closures III 1 l i b r a r y ( p r y r ) 2 unenclose ( square ) 3 # > f u n c t i o n ( x ) 4 # > { 5 # > xˆ2 6 # > } 7 unenclose ( cube ) 8 # > f u n c t i o n ( x ) 9 # > { 10 # > xˆ3 11 # > } Soumendra Prasad Dhanee Functional Programming in R Slide 51/51