Share
Embed
A subjective on Functional Programming
My points on why FP needs to spread.
Robin Palotai
August 13, 2012
Transcript
19 slides
-
A subjective on Functional Programming Robin Palotai @bootcode http://ron.shoutboot.com August
13, 2012 Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming -
Foreword Have the following ever occured to you? Software we
write is buggy Errors are rather logged than handled Similar patterns of code are written over and over again Existing code is not easily adapted We use frameworks with tons of xml conguration and go insane Are you fed up? Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming -
Types are powerful Theorem CurryHoward isomorphism: A proof is a
program, the formula it proves is a type for the program. Notes Applies for subprograms (functions) too. Informally means if the code compiles, it is proved that from the input types the output type can be produced. Average response: So what? Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming -
Types are underrated Programmers don't appreciate types, partially because types
they encounter are usually shallow in information. primitives, string (no semantic) void (can't do anything with it) Object (not too informative) MemberProcessorStrategy (name doesn't tell us anything) There are some refreshing exceptions. Collection types (clear concept + expectable operations) Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming -
A contrieved (?) example Object getItem(String id) Doesn't tell much
detail about data or the underlying computation, so we resort to the Apidoc, if any. Apidoc throws if id is invalid syntactically returns null if no such id normally returns either Foo or Bar type by performing IO request and blocking the thread until the request is complete Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming -
Types can be expressive Object getItem(String id) Robin Palotai@bootcodehttp://ron.shoutboot.com A
subjective on Functional Programming -
Types can be expressive Object getItem(String id) getItem: String =>
Any Note In Functional Programming, functions are also rst-class values and have a type signature. Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming -
Types can be expressive Object getItem(String id) throws if id
is invalid syntactically getItem: String => Any Note In Functional Programming, functions are also rst-class values and have a type signature. Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming -
Types can be expressive Object getItem(String id) throws if id
is invalid syntactically String => Validation[E, Any] Note Validation holds a successful result or a description of the errors encountered during execution of the computation. While throw/try/catch is hard to compose, functions returning a Validation compose easily. Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming -
Types can be expressive Object getItem(String id) throws if id
is invalid syntactically returns null if no such id String => Validation[E, Any] Note Validation holds a successful result or a description of the errors encountered during execution of the computation. While throw/try/catch is hard to compose, functions returning a Validation compose easily. Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming -
Types can be expressive Object getItem(String id) throws if id
is invalid syntactically returns null if no such id String => Validation[E, Option[Any]] Note Option is either None or Some(value), marking nullness explicitly (no more NPE). Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming -
Types can be expressive Object getItem(String id) throws if id
is invalid syntactically returns null if no such id normally returns either Foo or Bar type String => Validation[E, Option[Any]] Note Option is either None or Some(value), marking nullness explicitly (no more NPE). Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming -
Types can be expressive Object getItem(String id) throws if id
is invalid syntactically returns null if no such id normally returns either Foo or Bar type String => Validation[E, Option[Either[Foo, Bar]]] Note Either carries one of two alternatives. If more alternatives are needed, it is common to use a custom Algebraic Data Type. Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming -
Types can be expressive Object getItem(String id) throws if id
is invalid syntactically returns null if no such id normally returns either Foo or Bar type by performing IO request String => Validation[E, Option[Either[Foo, Bar]]] Note Either carries one of two alternatives. If more alternatives are needed, it is common to use a custom Algebraic Data Type. Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming -
Types can be expressive Object getItem(String id) throws if id
is invalid syntactically returns null if no such id normally returns either Foo or Bar type by performing IO request String => Validation[E, IO[Option[Either[Foo, Bar]]]] Note IO holds the execution plan of an I/O action, making it possible to defer IO actions until necessary. Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming -
Types can be expressive Object getItem(String id) throws if id
is invalid syntactically returns null if no such id normally returns either Foo or Bar type by performing IO request and blocking the thread until the request is complete String => Validation[E, IO[Option[Either[Foo, Bar]]]] Note IO holds the execution plan of an I/O action, making it possible to defer IO actions until necessary. Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming -
Types can be expressive Object getItem(String id) throws if id
is invalid syntactically returns null if no such id normally returns either Foo or Bar type by performing IO request and blocking the thread until the request is complete String => Validation[E, Future[IO[Option[Either[Foo, Bar]]]]] Note Future (alias Promise) describes a computation that is running concurrently. With it blocking threads can be avoided. Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming -
FP lets you manipulate types Functional Programming has the concepts
and tools to dene, use and compose types capturing both the description of data and context of computation, resulting in correct and reusable software. Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming -
Where to now? Start studying FP. It is not just
a new API to skim overnight, but a new concept that will change the way you interpret and create software. Start now. Haskell Learn You a Haskell: http://learnyouahaskell.com Real World Haskell: http://book.realworldhaskell.org Scala Functional Programming in Scala: http://www.manning.com/bjarnason Scalaz 7: https://github.com/scalaz/scalaz/tree/scalaz-seven Robin Palotai@bootcodehttp://ron.shoutboot.com A subjective on Functional Programming