intro to functional programming
DESCRIPTION
Introduction to functional programming for a local meetup.TRANSCRIPT
![Page 1: Intro to Functional Programming](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/1.jpg)
FUNCTIONALPROGRAMMING
by Andraž Bajt @edofic
available atslides.com/edofic/functional-programming
![Page 2: Intro to Functional Programming](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/2.jpg)
WHO AM I?CS student at FRI
developer at Koofr
STaaS provider startup
![Page 3: Intro to Functional Programming](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/3.jpg)
ENTROPY
![Page 4: Intro to Functional Programming](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/4.jpg)
HALTING PROBLEM
![Page 5: Intro to Functional Programming](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/5.jpg)
FIGHTING ENTROPY
![Page 6: Intro to Functional Programming](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/6.jpg)
OBJECT ORIENTATIONmodel real worldobjects all the way down3 rulesdesign patterns
![Page 7: Intro to Functional Programming](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/7.jpg)
FUNCTIONS
![Page 8: Intro to Functional Programming](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/8.jpg)
LAMBDA CALCULUSAlonso Curch 1930sEquivalent to Turing Machine
![Page 9: Intro to Functional Programming](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/9.jpg)
LAMBDA CALCULUSVery simple
Abstractionλx.y
Applicationf x
Composability!
![Page 10: Intro to Functional Programming](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/10.jpg)
WHAT IS FP?
![Page 11: Intro to Functional Programming](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/11.jpg)
WHAT IS FP?Programming with values
Using function to transform values
![Page 12: Intro to Functional Programming](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/12.jpg)
MY STORY(roughly)
1. C(++)2. C#3. Java4. Python5. Scala6. Haskell
![Page 13: Intro to Functional Programming](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/13.jpg)
WHY MOVE TO FP LAND?Composability Abstractions
![Page 14: Intro to Functional Programming](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/14.jpg)
GOT MATH?
![Page 15: Intro to Functional Programming](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/16.jpg)
CURRYING apply = (f) => (x) => f x
apply((x) => x * 2)(1)
![Page 17: Intro to Functional Programming](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/21.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/24.jpg)
YOU SHOULD TRY ITLibraries for your language
Learn a new language!
![Page 25: Intro to Functional Programming](https://reader034.vdocuments.mx/reader034/viewer/2022042601/5484304fb479590a0d8b4b38/html5/thumbnails/25.jpg)
QUESTIONS