Download - Real World Scalaz

Transcript
Page 1: Real World Scalaz

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)

Page 2: Real World Scalaz

“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

Page 3: Real World 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)

Page 4: Real World Scalaz

“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

Page 5: Real World 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

Page 6: Real World Scalaz

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?

Page 7: Real World Scalaz

@_jrwest github.com/jrwest

blog.loopedstrange.com

* a little context before we talk about the “how & why?”

Page 8: Real World Scalaz

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

Page 9: Real World Scalaz

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

Page 10: Real World Scalaz

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

Page 11: Real World Scalaz

SOME EXAMPLES

Page 12: Real World Scalaz

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

Page 13: Real World Scalaz

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)

Page 14: Real World Scalaz

sideEffect.pure[IO]

 type V[X]=V

alidationNEL

[Throwable,X

]

def fetchKe

ys(keys: Lis

t[String]) =

{

  keys.map(

bucket.fetch

[CustomDomai

n](_))

    .sequen

ce

     .map(_.

sequence[V,O

ption[Custom

Domain]])

 }

* 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

Page 15: Real World Scalaz

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”

Page 16: Real World Scalaz

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

Page 17: Real World Scalaz

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

Page 18: Real World Scalaz

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

Page 19: Real World Scalaz

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

Page 20: Real World Scalaz

Impure

PureImpure

* “the sandwich” - common pattern in our code-- original image: http://fastfood.ocregister.com/files/2010/05/ice-cream-sandwich1.jpg

Page 21: Real World Scalaz

:)* 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

Page 22: Real World Scalaz

:(* 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!)

Page 23: Real World Scalaz

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”

Page 24: Real World Scalaz

“SCALE”?

Page 25: Real World Scalaz

“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?

Page 26: Real World 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

Page 27: Real World Scalaz

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

Page 28: Real World Scalaz

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

Page 29: Real World Scalaz

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

Page 30: Real World Scalaz

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

Page 31: Real World Scalaz

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

Page 32: Real World Scalaz

ASK #scalaz FOR HELP

Page 33: Real World Scalaz

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

Page 34: Real World Scalaz

PAYING UPFRONT

* of course this have some upfront cost* so does writing tests and similarly a good balance pays off

Page 35: Real World Scalaz

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

Page 36: Real World Scalaz

Thanks!

talk recap slide

Page 37: Real World Scalaz

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


Top Related