Post on 20-Jun-2015
transcript
REAL W⊛RLD SCALAZ
http://speakerdeck.com/u/jrwest/p/real-world-scalaz
changed talk title (“practical scalaz” by Chris Marshall http://skillsmatter.com/podcast/scala/practical-scalaz-2518)
“If you are thinking of using Scalaz, stop now while you still have your sanity"
* context: java devs learning scala* source: http://zeroturnaround.com/blog/scala-sink-or-swim-part-1/#scalaz
“How does this help you ship features or improve your product?”
* context: commenting on the use of lenses* source: https://twitter.com/#!/coda/status/167794305107361796)
“It will not immediately help you write better programs that are easy to understand”
* hear/read this often* source: http://zeroturnaround.com/blog/scala-sink-or-swim-part-1/#scalaz
“There are two types of [libraries]: the ones people hate
and the ones nobody uses”- unknown
* applies to scala too* know correct attribution? contact me
HOW &
WHY?* those quotes pretty much boil down to these two questions:* how: apply tools given by scalaz to real world problems? to handle human scale?* why would we choose this solution over other ones?
@_jrwest github.com/jrwestblog.loopedstrange.com
* a little context before we talk about the “how & why?”
I WORK @
* small team, < 15 engineers* many have java bg, but not all, most w/ little to no scala experience* most have oop bg, varying knowledge of FP topics
WE USE SCALA{ a lot }
* 15+ public facing services supported by many other internal modules/libs* most built on netty, also I/O bound* originally java, now > 70% scala
libraryDependencies += “org.scalaz” %% “scalaz-core” % “6.0.3”
* almost all our services depend on scalaz* usage levels and parts of scalaz used vary among services* blocked on upgrading to 6.0.4 b/c of binary compatibility issues
SOME EXAMPLES
JSON Mappingdef FileManifestJSONR = new JSONR[AppID=>FileManifest] {
def read(json: JValue) = manifestNoId.applyJSON( field(jsonManifestKey), field(changeLogEntryCachedJsonKey)
)(json) }}
* lift-json-scalaz (https://github.com/lift/framework/tree/master/core/json-scalaz)* validationnel applicative, error accumulation = better user-facing errors & debugging* composition
for { mbExists <- manifestFiles member path
_ <- manifestFiles -= path
_ <- changeLogEntryFiles -=? (path, mbExists >| false | true)
} yield mbExists
S @-@ A / State[S,A]
* easy manipulation of highly nested case classes (ex: file manifests)* better implementation hiding* rich libraries (e.g. MapLens, SetLens, & provided state actions)
sideEffect.pure[IO] type V
[X]=Validation
NEL[Throwable,
X]
def fetchKe
ys(keys: List[
String]) = {
keys.map(bu
cket.fetch[Cus
tomDomain](_))
.sequen
ce
.map(_.se
quence[V,Opt
ion[CustomDoma
in]])
}
* can’t guarantee purity, can delineate impurity* examples: storing data in riak, uploading files to s3 and phonegap build svc* composition of IO makes side-effecting code much more reusable
NonEmptyList[A]
Pure[P[_]]OptionW[A]
Kleisli[M[_],A,B]
Functor[F[_]]Apply[A[_]]
Traverse[T[_]]
Bind[Z[_]]
Semigroup[A]Zero[A]
Tree[A]
Fold
able[F[_
]]
Identity
MA[M[_],A]
MAB[M[_],A,B]
* we make use of a lot more as well* data structures like Validation and NonEmptyList* most type classes and “enrichments”
ITS NOT ALL OR NOTHING* shades of grey, not black and white* scalaz is really many libraries, use the parts you want* we started using “encrichments” and some data structures, grew from there --original image: http://www.chicagonow.com/chicago-tough/files/2012/03/chipsallin.jpg
MAKE BORDERS* don’t abuse scala’s flexibility & power* toss pure/impure code into a salad bowl & mixing is a bad idea* define borders where pure code meets impure code--original image: http://media.nowpublic.net/images//59/4/59455eabd625a15e2afb583dcfbb27ce.jpg
Public Interface
ImplementationDependency Calls
* your code can be pure* public interface is the border, it runs IO, throws exceptions, etc* existing libraries probably aren’t pure -- many are java after all
Outside World
Business LogicData Access
* communicate with clients via HTTP * the most important part -- your code -- make pure* * perform side-effects like talk to database, read/write files, etc
Impure
PureImpure
* “the sandwich” - common pattern in our code-- original image: http://fastfood.ocregister.com/files/2010/05/ice-cream-sandwich1.jpg
:)* code has become much more DRY/modular and as a result more unit testable* concurrent code becomes “simpler” to reason about when pure* FP makes reasoning about bugs simpler, narrow down cause faster
:(* scala hardsips: bin. compatability problems, tooling deficiencies, type inference limits* import scalaz._; import Scalaz._ adds a lot of clutter in the current scope* lack of docs/related content (in Scala!)
scalaz7
* not currently used in our production systems but one or two of my side projects* much better for teaching (explicit type class instance usage -- w/o syntax ext.)* addresses some of the issues we have like importing “the scalaz world”
“SCALE”?
“Human Scale”* the complexity comments may be FUD, but that does not mean there is not stuff to learn here* how do we help new employees learn scala & scalaz?
HIRE PEOPLE WHO WANT TO LEARN
* hiring from all backgrounds instead of hiring a “scala or java developer” is much easier* those people need to be open to exploring and learning new tech/methods
Shared Resources* easy win, shares effort in finding good resources* dropbox full of downloaded papers and books purchased by the company* internal talks available on github
LEARN TOGETHER* code review/pull requests* face to face discussion on a whiteboard* team talks on fridays, any one can attend, anyone can talk, neither mandatory
ON YOUR OWN
* RTFC -- in the end it all just code* apply what you know, this is what really makes you learn it* internal talks mostly make you aware of it, give you starting point, application is key
ITS NOT ALL OR NOTHING* focus on understanding a part of the library* break that part down, use it in the repl in a few cases (w/ a few types, examples, etc)* as you learn more about each part and then more parts the bigger picture emerges--original image: http://www.chicagonow.com/chicago-tough/files/2012/03/chipsallin.jpg
Learn You a??
* not necessary* understanding syntax, basics can be useful while learning concepts until scala docs fill out-- haskell logo: http://www.haskell.org/wikistatic/haskellwiki_logo.png
ASK #scalaz FOR HELP
BREAKRULES
* you may hear things you cannot do, “validation is not a monad”* ignore and explore yourself -- these are the real learning opportunites* understand how to implement validation monad and why no error accum. happens
PAYING UPFRONT
* of course this have some upfront cost* so does writing tests and similarly a good balance pays off
A1A2A3L1L2L3
LevelUp
* scalaz covers most advanced features in scala language* great guide for library and code design (to an extent). Scalaz guys have done serious amount of research in this regard
Thanks!
talk recap slide
SOME RESOURCES• Atlassian’s Typeclassopedia - http://typeclassopedia.bitbucket.org
• Apocalisp Blog - http://apocalisp.wordpress.com/
• “Functional Programming in Scala” MEAP - http://manning.com/bjarnason/
• Eric Torreborre’s Blog - http://etorreborre.blogspot.com/
• StackMob Engineering Blog - http://www.stackmob.com/category/engineering/
• Learn You a Scalaz (my WIP, about to undergo major rework) - https://github.com/jrwest/learn-you-a-scalaz
• Chris Marshall’s “Practical Scalaz” Talk - http://skillsmatter.com/podcast/scala/practical-scalaz-2518