30
What is functional programming? Functional programming in R Wrap-up Functional Programming in R David Springate @datajujitsu David Springate Functional Programming in R

Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Functional Programming in R

David Springate

@datajujitsu

David Springate Functional Programming in R

Page 2: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Outline

1 What is functional programming?2 Elements of functional programming3 Functional Programming in R4 A Functional-style generic bootstrap5 Wrap-up and further reading

David Springate Functional Programming in R

Page 3: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

What is functional programming?

David Springate Functional Programming in R

Page 4: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Programming metaphysics

Programs are representations of reality in a computer

There are different ways to represent reality. . .

David Springate Functional Programming in R

Page 5: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

OOP / imperative metaphysics

C, Python, Java etc.

Everything is an object with state and behaviour

A river is an object withvarious attributes boundto it:

Flow rate

Depth

Pollution levels

Salinity

David Springate Functional Programming in R

Page 6: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Functional Metaphysics

No man ever steps in the same river twice, for it’snot the same river and he’s not the same man. -Heraclitus

Lisp, Haskell, F#, Clojure etc.

Things are collections of fixed values whichgo through processes (functions) over time

David Springate Functional Programming in R

Page 7: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Elements of Functional Programming

1 Functions as first class citizens2 Vectorised / declarative expressions3 “Pure” functions - No side effects4 Anonymous functions5 Immutability6 Recursion

David Springate Functional Programming in R

Page 8: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Functional programming in R

David Springate Functional Programming in R

Page 9: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

R Genealogy

S

Scheme

David Springate Functional Programming in R

Page 10: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

R is a strongly functional language

. . . everything is a function call!

> 1 + 2

## [1] 3

. . . is the same as. . .

> ‘+‘(1, 2)

## [1] 3

David Springate Functional Programming in R

Page 11: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

R is a strongly functional language

. . . everything is a function call!

> 1:10

## [1] 1 2 3 4 5 6 7 8 9 10

. . . is the same as. . .

> ‘:‘(1, 10)

## [1] 1 2 3 4 5 6 7 8 9 10

David Springate Functional Programming in R

Page 12: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Vectorised functions

Are functions that operate on vectors/matrices/dataframes as wellas on single numbers

Often much faster than looping over a vector

Higher level - less to debug!

Very deep in the language

David Springate Functional Programming in R

Page 13: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Vectorised functions

Get all even numbers up to 200000

> # C style vector allocation:

> x <- c()

> for(i in 1:200000){+ if(i %% 2 == 0){+ x <- c(x, i)

+ }+ }

## user system elapsed

## 9.86 0.00 9.88

David Springate Functional Programming in R

Page 14: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Vectorised functions

Get all even numbers up to 200000

> # FP style vectorised operation

> a <- 1:200000

> x <- a[a %% 2 == 0]

## user system elapsed

## 0.01 0.00 0.01

David Springate Functional Programming in R

Page 15: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Vectorised functions

Most built-in functions are vectorised:

> # e.g.

> paste()

> colMeans()

> rowSums()

> log()

> sqrt()

> x > y

> is.na()

> ifelse()

> rnorm() # etc. etc.

David Springate Functional Programming in R

Page 16: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Higher-order functions

. . . Are functions that operate on all elements of a collection(vector/list/vector/matrix/dataframe)

Function to be applied to each element in turn

In[1] In[2] In[3] In[5] In[4]

Out[1] Out[2] Out[3] Out[5] Out[4]

Input collection

Output collection

David Springate Functional Programming in R

Page 17: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Higher-order functions

You just need to think about what goes in and what you want tocome out:

lapply : Any collection -> FUNCTION -> list

sapply : Any collection -> FUNCTION -> matrix/vector

apply : Matrix/dataframe + margin -> FUNCTION ->matrix/vector

Reduce : Any collection -> FUNCTION -> single element

David Springate Functional Programming in R

Page 18: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Higher-order functions

You just need to think about what goes in and what you want tocome out:

lapply : Any collection -> FUNCTION -> list

sapply : Any collection -> FUNCTION -> matrix/vector

apply : Matrix/dataframe + margin -> FUNCTION ->matrix/vector

Reduce : Any collection -> FUNCTION -> single element

David Springate Functional Programming in R

Page 19: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Higher-order functions

You just need to think about what goes in and what you want tocome out:

lapply : Any collection -> FUNCTION -> list

sapply : Any collection -> FUNCTION -> matrix/vector

apply : Matrix/dataframe + margin -> FUNCTION ->matrix/vector

Reduce : Any collection -> FUNCTION -> single element

David Springate Functional Programming in R

Page 20: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Higher-order functions

You just need to think about what goes in and what you want tocome out:

lapply : Any collection -> FUNCTION -> list

sapply : Any collection -> FUNCTION -> matrix/vector

apply : Matrix/dataframe + margin -> FUNCTION ->matrix/vector

Reduce : Any collection -> FUNCTION -> single element

David Springate Functional Programming in R

Page 21: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Closures

An object is data with functions. A closure is a function with data.- John D Cook

Function data1

Returns

New function enclosing data

data

data2

Arguments to function

data2

Can build functions thatreturn new functions:

Useful if some workonly needs to be doneonce, when thefunction is generated

Great for optimisationand randomisationproblems

David Springate Functional Programming in R

Page 22: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

An FP-style Bootstrapping function

A Generic function to sample linear models with replacement

Illustrates:

Functions returning new functions

Higher-order functions

Anonymous functions

Vectorised functions

Will test using the iris data: data(iris)

c.f. a non-FP version of the same function

David Springate Functional Programming in R

Page 23: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

An FP-style Bootstrapping function

boot_lm <- function(formula, data, ...){function(){lm(formula=formula,

data=data[sample(nrow(data), replace=TRUE),], ...)

}}

iris_boot <- boot_lm(Sepal.Length ~ Petal.Length, iris)

bstrap <- sapply(X=1:1000,

FUN=function(x) iris_boot()$coef)

David Springate Functional Programming in R

Page 24: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

An FP-style Bootstrapping function

apply(bstrap, MARGIN=1, FUN=quantile,

probs=c(0.025, 0.5, 0.975))

## (Intercept) Petal.Length

## 2.5% 4.155 0.3696

## 50% 4.314 0.4072

## 97.5% 4.458 0.4455

David Springate Functional Programming in R

Page 25: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

A Non-FP-style Bootstrapping function

boot_lm_nf <- function(d, form, iters, output, ...){for(i in 1:iters){x <- lm(formula=form,

data=d[sample(nrow(d),

replace = TRUE),], ...)[[output]]

if(i == 1){bootstrap <- matrix(data=NA, nrow=iters,

ncol=length(x),

dimnames=list(NULL,names(x)))

bootstrap[i,] <- x

} else bootstrap[i,] <- x

}bootstrap

}

David Springate Functional Programming in R

Page 26: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

A Non-FP-style Bootstrapping function

bstrap2 <- boot_lm_nf(d=iris,

form=Sepal.Length ~ Petal.Length,

iters=1000, output="coefficients")

CIs <- c(0.025, 0.5, 0.975)

cbind( "(Intercept)"=quantile(bstrap2[,1],probs = CIs),

"Petal.Length"=quantile(bstrap2[,2],probs = CIs))

## (Intercept) Petal.Length

## 2.5% 4.167 0.3711

## 50% 4.309 0.4093

## 97.5% 4.447 0.4460

David Springate Functional Programming in R

Page 27: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Wrap-up

David Springate Functional Programming in R

Page 28: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Advantages of Functional Programming

Functional programming in R is

More concise

Often faster

Easier to read and debug

More elegant

Higher level

Truer to the language!

. . . than non-fp

David Springate Functional Programming in R

Page 29: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Further reading

Functional Programming

mitpress.mit.edu/sicp

Functional programming in R

github.com/hadley/devtools/wiki

www.burns-stat.com/pages/Tutor/R_inferno.pdf

Vectorisation

Programming language metaphysics

http://www.infoq.com/presentations/ Are-We-There-Yet-Rich-Hickey

David Springate Functional Programming in R

Page 30: Functional Programming in R · 2018-04-10 · Lisp, Haskell, F#, Clojure etc. Things are collections of xed values which go through processes (functions) over time David Springate

What is functional programming?Functional programming in R

Wrap-up

Thank you

github.com/DASpringate

linkedin.com/in/daspringate/

@datajujitsu

daspringate@gmail

. . . Any questions?

David Springate Functional Programming in R