intro to functional programming

25
FUNCTIONAL PROGRAMMING by Andraž Bajt @edofic available at slides.com/edofic/functional-programming

Upload: andraz-bajt

Post on 05-Dec-2014

110 views

Category:

Software


4 download

DESCRIPTION

Introduction to functional programming for a local meetup.

TRANSCRIPT

Page 1: Intro to Functional Programming

FUNCTIONALPROGRAMMING

by Andraž Bajt @edofic

available atslides.com/edofic/functional-programming

Page 2: Intro to Functional Programming

WHO AM I?CS student at FRI

developer at Koofr

STaaS provider startup

Page 3: Intro to Functional Programming

ENTROPY

Page 4: Intro to Functional Programming

HALTING PROBLEM

Page 5: Intro to Functional Programming

FIGHTING ENTROPY

Page 6: Intro to Functional Programming

OBJECT ORIENTATIONmodel real worldobjects all the way down3 rulesdesign patterns

Page 7: Intro to Functional Programming

FUNCTIONS

Page 8: Intro to Functional Programming

LAMBDA CALCULUSAlonso Curch 1930sEquivalent to Turing Machine

Page 9: Intro to Functional Programming

LAMBDA CALCULUSVery simple

Abstractionλx.y

Applicationf x

Composability!

Page 10: Intro to Functional Programming

WHAT IS FP?

Page 11: Intro to Functional Programming

WHAT IS FP?Programming with values

Using function to transform values

Page 12: Intro to Functional Programming

MY STORY(roughly)

1. C(++)2. C#3. Java4. Python5. Scala6. Haskell

Page 13: Intro to Functional Programming

WHY MOVE TO FP LAND?Composability Abstractions

Page 14: Intro to Functional Programming

GOT MATH?

Page 15: Intro to Functional Programming

EXAMPLESfunction apply(f, x) { <div style="text-align: center;"></div> return f(x); }

apply(function(x){ return x * 2}, 1);

apply = (f,x) => f x

apply((x) => x * 2, 1)

Page 16: Intro to Functional Programming

CURRYING apply = (f) => (x) => f x

apply((x) => x * 2)(1)

Page 17: Intro to Functional Programming

COMPOSITION compose = (f) => (g) => (x) => f(g(x)) foo = (x) => x + 1 bar = (x) => x * 2 f = compose(foo)(bar)

f(2) # 5

Page 18: Intro to Functional Programming

COLLECTIONS people = [ {name: "John", surname: "Doe", age: 35}, {name: "Jane", surname: "Doe", age: 49} ]

people .filter((p) => p.age > 40) .map((p) => p.name + p.surname)

people.reduce(((total, p) => total + p.age), 0) / people.length

Page 19: Intro to Functional Programming

HELLO HASKELL WEBmain :: IO ()main = scotty 3000 helloRoutes

helloRoutes :: ScottyM ()helloRoutes = do get "/" $ html "hello there" get "/hello" $ html "Hello world" post "/hello" $ html "Hello postman" get "/hello/:name" $ do name <- param "name" html $ "Hello " mappend name

Page 20: Intro to Functional Programming

DSLpostUsersR :: Handler ValuepostUsersR = do Auth.adminOnly user <- requireJsonBody userIdMby <- runDB $ insertUnique user runValidationHandler $ ("username", "User with the supplied username already exists") validate (isJust userIdMby) getUsersUserR $ fromJust userIdMby

Page 21: Intro to Functional Programming

BUILDING A DSLdata Command a = Up a | Down a deriving (Eq, Show, Functor)type Sequence = Free Command () up, down :: Sequence -> Sequenceup = Free . Updown = Free . Down

done :: Sequencedone = Pure ()go = ($done) example = do go up go up go down go up

Page 22: Intro to Functional Programming

ORIGAMIfsum :: (Num a) => Fold a afsum = Fold (+) 0 id flen :: (Num b) => Fold a bflen = Fold (\s _ -> s + 1) 0 fromInteger favg :: (Fractional a) => Fold a afavg = (/) <$> fsum <*> flen

example = runfold favg [1,2,3]

Page 23: Intro to Functional Programming

ORIGAMI IMPLEMENTATIONFor foldable things

data Fold a b = forall x . Fold (x -> a -> x) x (x -> b) instance Functor (Fold a) where f fmap Fold step zero map = Fold step zero (f . map) instance Applicative (Fold a) where pure a = Fold const () (const a) Fold f1 z1 m1 <*> Fold f2 z2 m2 = Fold f z m where f (x1,x2) e = (f1 x1 e, f2 x2 e) z = (z1, z2) m (f, x) = (m1 f) (m2 x) runfold :: (Foldable t) => Fold a b -> t a -> brunfold (Fold f z m) t = m $ foldl' f z t

Page 24: Intro to Functional Programming

YOU SHOULD TRY ITLibraries for your language

Learn a new language!

Page 25: Intro to Functional Programming

QUESTIONS