Real Work Scalaz
-
Upload
stackmob-inc -
Category
Documents
-
view
472 -
download
4
Transcript of Real Work Scalaz
![Page 1: Real Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/1.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/2.jpg)
“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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/3.jpg)
“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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/4.jpg)
“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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/5.jpg)
“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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/6.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/7.jpg)
@_jrwest github.com/jrwestblog.loopedstrange.com
* a little context before we talk about the “how & why?”
![Page 8: Real Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/8.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/9.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/10.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/11.jpg)
SOME EXAMPLES
![Page 12: Real Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/12.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/13.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/14.jpg)
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
![Page 15: Real Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/15.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/16.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/17.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
![Page 18: Real Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/18.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
![Page 19: Real Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/19.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/20.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/21.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
![Page 22: Real Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/22.jpg)
:(* 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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/23.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/24.jpg)
“SCALE”?
![Page 25: Real Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/25.jpg)
“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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/26.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/27.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/28.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/29.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/30.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/31.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
![Page 32: Real Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/32.jpg)
ASK #scalaz FOR HELP
![Page 33: Real Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/33.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/34.jpg)
PAYING UPFRONT
* of course this have some upfront cost* so does writing tests and similarly a good balance pays off
![Page 35: Real Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/35.jpg)
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 Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/36.jpg)
Thanks!
talk recap slide
![Page 37: Real Work Scalaz](https://reader033.fdocuments.in/reader033/viewer/2022042521/55859ac2d8b42ad06d8b5064/html5/thumbnails/37.jpg)
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