Slide 1

Slide 1 text

FUNCTIONAL GEEKERY FOR AN IMPERATIVE MIND Rajeev N B

Slide 2

Slide 2 text

@rshetty on Github @rbharshetty on Twitter

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

SIMON PEYTON JONES

Slide 5

Slide 5 text

NIRVANA C, C++, Java, Ruby .. Haskell Nirvana Useful Less Useful Unsafe Safe

Slide 6

Slide 6 text

IMPERATIVE VS FUNCTIONAL

Slide 7

Slide 7 text

PROPERTY IMPERATIVE FUNCTIONAL FOCUS Changing existing values Declaring new values CONTROL FLOW Loops, Conditionals and Function calls Functional calls including Recursion MANIPULATION UNIT Instances of classes and structs Functions and Data Structures. MENTAL MODEL Algorithmic Programming Composing problems with set of functions.

Slide 8

Slide 8 text

FUNCTIONAL PROGRAMMING?

Slide 9

Slide 9 text

– John Hughes (Why Functional Programming Matters) “Functional Programming is so called because a program consists entirely of functions”

Slide 10

Slide 10 text

CONCEPTS

Slide 11

Slide 11 text

FUNCTION COMPOSITION (F O G)

Slide 12

Slide 12 text

It is the act of pipelining result of a function into input of another, creating a new function

Slide 13

Slide 13 text

Function g(x) x + 1 Function f(x) x * x x = 3 g(x)= 4 f(g(x))= 16

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

It is the act of pipelining result of a function into input of with another, creating a new function

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

LAZY EVALUATION

Slide 21

Slide 21 text

Evaluation of a expression is deferred until the result is needed by other computations

Slide 22

Slide 22 text

LAZY EVALUATION Call by need Infinite Data structures Thunks Performant

Slide 23

Slide 23 text

HIGHER ORDER FUNCTIONS

Slide 24

Slide 24 text

Functions which either take functions as arguments or return functions as return values

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

CURRYING

Slide 28

Slide 28 text

Transforming function that takes multiple arguments into function that takes single argument

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

PURE FUNCTION

Slide 32

Slide 32 text

Function that returns exactly the same result every time it is called with same set of arguments

Slide 33

Slide 33 text

Pure Function f(x) = Math.cos(x) Pure Function ( No Side Effects ) x f(x)

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

REFERENTIAL TRANSPARENCY Expression is referentially transparent if it can be replaced with its value Property of Pure functions No side effects Easier to reason about programs

Slide 36

Slide 36 text

WHY PURE FUNCTIONS? Reproducible results Memoization Parallelization

Slide 37

Slide 37 text

IMMUTABILITY

Slide 38

Slide 38 text

Immutable object is an object whose state cannot be modified after it is created

Slide 39

Slide 39 text

IMMUTABLE DATA STRUCTURES Data structure once created cannot be modified Uses sophisticated structure sharing

Slide 40

Slide 40 text

WHY? Concurrency Security

Slide 41

Slide 41 text

FUNCTIONAL THINKING

Slide 42

Slide 42 text

REFERENCES Haskell Wiki Learn You a Haskell Ruby Function Composition Resurgence of Functional Programming Code samples

Slide 43

Slide 43 text

THANKS

Slide 44

Slide 44 text

QUESTIONS?