FUNCTIONAL PROGRAMMING byAndražBajt @edofic availableat slides.com/edofic/functional-programming
WHO AM I? CS studentatFRI developer atKoofr STaaS provider startup
ENTROPY
HALTING PROBLEM
FIGHTING ENTROPY
OBJECT ORIENTATION modelrealworld objects allthe waydown 3 rules design patterns
FUNCTIONS
LAMBDA CALCULUS Alonso Curch 1930s Equivalentto Turing Machine
LAMBDA CALCULUS Verysimple Abstraction λx.y Application fx Composability!
WHAT IS FP?
WHAT IS FP? Programming with values Using function to transform values
MY STORY (roughly) 1. C(++) 2. C# 3. Java 4. Python 5. Scala 6. Haskell
WHY MOVE TO FP LAND? Composability Abstractions
GOT MATH?
EXAMPLES functionapply(f,x){ <divstyle="text-align:center;"></div> returnf(x); } apply(function(x){returnx*2},1); apply=(f,x)=>fx apply((x)=>x*2,1)
CURRYING apply=(f)=>(x)=>fx apply((x)=>x*2)(1)
COMPOSITION compose=(f)=>(g)=>(x)=>f(g(x)) foo=(x)=>x+1 bar=(x)=>x*2 f=compose(foo)(bar) f(2)#5
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
HELLO HASKELL WEB main::IO() main=scotty3000helloRoutes helloRoutes::ScottyM() helloRoutes=do get "/" $html"hellothere" get "/hello"$html"Helloworld" post"/hello"$html"Hellopostman" get "/hello/:name"$do name<-param"name" html$"Hello"`mappend`name
DSL postUsersR::HandlerValue postUsersR=do Auth.adminOnly user<-requireJsonBody userIdMby<-runDB$insertUniqueuser runValidationHandler$ ("username","Userwiththesuppliedusernamealreadyexists") `validate`(isJustuserIdMby) getUsersUserR$fromJustuserIdMby
BUILDING A DSL dataCommanda=Upa|Downaderiving(Eq,Show,Functor) typeSequence=FreeCommand() up,down::Sequence->Sequence up=Free.Up down=Free.Down done::Sequence done=Pure() go=($done) example=do goup goup godown goup
ORIGAMI fsum::(Numa)=>Foldaa fsum=Fold(+)0id flen::(Numb)=>Foldab flen=Fold(s_->s+1)0fromInteger favg::(Fractionala)=>Foldaa favg=(/)<$>fsum<*>flen example=runfoldfavg[1,2,3]
ORIGAMI IMPLEMENTATION For foldable things dataFoldab=forallx.Fold(x->a->x)x(x->b) instanceFunctor(Folda)where f`fmap`Foldstepzeromap=Foldstepzero(f.map) instanceApplicative(Folda)where purea=Foldconst()(consta) Foldf1z1m1<*>Foldf2z2m2=Foldfzmwhere f(x1,x2)e=(f1x1e,f2x2e) z=(z1,z2) m(f,x)=(m1f)(m2x) runfold::(Foldablet)=>Foldab->ta->b runfold(Foldfzm)t=m$foldl'fzt
YOU SHOULD TRY IT Libraries for your language Learn a new language!
QUESTIONS

Intro to Functional Programming