scalaz - files.meetup.com · a library to support functional programming in Scala. Tuesday, April...
Transcript of scalaz - files.meetup.com · a library to support functional programming in Scala. Tuesday, April...
scalazget functional
Tuesday, April 12, 2011
import Predef._
Tuesday, April 12, 2011
scalaz is...
Tuesday, April 12, 2011
scalaz is
a library to support functional programming in Scala.
Tuesday, April 12, 2011
functional programming is...
Tuesday, April 12, 2011
functional programming is
programming with functions
Tuesday, April 12, 2011
a program is...
Tuesday, April 12, 2011
a program is
a single, referentially transparent expression
Tuesday, April 12, 2011
an expression is...
Tuesday, April 12, 2011
an expression is
a combination of sub expressions, using the constructs of a language. It evaluates to a result.
Tuesday, April 12, 2011
a referentially transparent expression...
Tuesday, April 12, 2011
any occurrence of
a referentially transparent expression
within a program could be replaced by its result, without changing the meaning of the program
Tuesday, April 12, 2011
a function is...
Tuesday, April 12, 2011
a function f: A => B is
a relation between every value of type A to exactly one value of type B.
and nothing else, no side effects!
(otherwise, function calls are not RT)
Tuesday, April 12, 2011
a type is...
Tuesday, April 12, 2011
a type is
a set of values
Tuesday, April 12, 2011
side effects are...
Tuesday, April 12, 2011
EVIL!
Tuesday, April 12, 2011
for example,
side effects are
• I/O to disk, console, network
• mutating fields or data structures
• throwing exceptions
Tuesday, April 12, 2011
functional programming
is sometimes called
expression oriented programming
Tuesday, April 12, 2011
functions are the glue to build programs out of smaller programs.
Tuesday, April 12, 2011
Why FP?
Tuesday, April 12, 2011
Modularity
The degree to which the parts can be separated and recombined.
Compositionality
Understand the parts, and the connections, and you understand the whole.
Tuesday, April 12, 2011
FP in Insert Lang Here
FP is possible in any language
(with differing degrees of difficulty)
Tuesday, April 12, 2011
Tuesday, April 12, 2011
FP in Scala
no statements, just expressions
Tuesday, April 12, 2011
FP in Scala
function literals
functions are values
higher-order functions
Tuesday, April 12, 2011
FP in Scala
algebraic data types (sort of)
Tuesday, April 12, 2011
FP in Scala
type parametric polymorphism
Tuesday, April 12, 2011
FP in Scala
implicit parameters for ad-hoc polymorphism
(aka type classes)
Tuesday, April 12, 2011
FP in Scala
Expressive type system
Type Inference
Type Constructor Polymorphism
Tuesday, April 12, 2011
FP in Scala: What’s hard?
no side effect tracking (up to you!)
lazy evaluation tricky
subtyping a hinderance
temptation of mutable vars, data structures
Tuesday, April 12, 2011
scalaz
• Open-source library for pure functional programming
• Version 5.0 stable; 6.0 imminent
http://scalaz.googlecode.com
http://github.com/scalaz/scalaz
Tuesday, April 12, 2011
scalaz
• Type Classes + Instances
• Pure Functional Data Structures
• General Functions
• Pimps
• Concurrency: Actors / Promise
Tuesday, April 12, 2011
Getting started...
Imports data types, functions, and necessary implicit conversions
Tuesday, April 12, 2011
Type Class
• Polymorphism independent of subtyping
• c.f. Comparable / Comparator
• Parametric polymorphism + constraints
• Compile time, implicit, constraint checks
• implicit members declare a type class instance...
• ... and implicit parameters express a constraint.
Tuesday, April 12, 2011
Type Class + Instance
Tuesday, April 12, 2011
Pure Functor
Pointed
Bind
Apply
Applicative
Zero Semigroup
Monoid
Equal Show
Order
Monad
InvariantFunctor
Plus Empty
Traverse
Foldable
and a few more...
Tuesday, April 12, 2011
Writing Type Class Instances
Tuesday, April 12, 2011
delegate to Any#{toString, ==}
Tuesday, April 12, 2011
Tuesday, April 12, 2011
Pimps
Tuesday, April 12, 2011
Pimps + Type Classes
Require Type Class
OO SyntaxAd hoc
Polymorphism
Tuesday, April 12, 2011
Behind the Scenes
Tuesday, April 12, 2011
Data Structures
• Zipper
• Tree / TreeLoc
• NonEmptyList
• FingerTree / Rope
• State
• Validation
Tuesday, April 12, 2011
I’m sure we’re out of time
Questions are always welcome:
• scalaz google group
• #scalaz on freenode
Tuesday, April 12, 2011