Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
..........................................................................................................Scala solutionHo...
..........................................................................................................Introductionhttp...
..........................................................................................................CoreMain functio...
..........................................................................................................Coredef accepted...
..........................................................................................................Coredef wordCoun...
..........................................................................................................Typeclass instan...
..........................................................................................................Executing// f u ...
..........................................................................................................Executingpackage...
..........................................................................................................All setLet’s see...
..........................................................................................................First attemptdef...
..........................................................................................................First attemptWor...
..........................................................................................................Second attemptde...
..........................................................................................................Second attemptJu...
..........................................................................................................Typeclassesi n s...
..........................................................................................................Monoids(S, ⊗, 1)...
..........................................................................................................Second attemptPr...
..........................................................................................................IterateesScala m...
..........................................................................................................Iteratees – same...
..........................................................................................................Iteratees – 3x f...
..........................................................................................................Wordcounting sof...
..........................................................................................................WordcountingI di...
..........................................................................................................That’s itQuestio...
Upcoming SlideShare
Loading in …5
×

Presentation

312 views

Published on

Solution for the functional programming meetup, Berlin.

  • Be the first to comment

Presentation

  1. 1. ..........................................................................................................Scala solutionHow to make your Scala controll effects a-laHaskellGeorge Leontievdeltamethod GmbHFebruary 28, 2013(λx.folonexlambda-calcul.us)@folone.infoGeorge Leontiev deltamethod GmbHScala solution
  2. 2. ..........................................................................................................Introductionhttps://github.com/folone/funclub-wordsNote: I intentionally made it more ”interesting” to show more neatscalaz stuffI won’t cover everything though. If something seems strange,please ask.George Leontiev deltamethod GmbHScala solution
  3. 3. ..........................................................................................................CoreMain functionswordCount : : S t r i n g → Map ( String , I n t )acceptedChars : : Char → BooleanHelper functionstime : : ( a → IO b) → IO bc l o s e : : C l o s e a b l e a ⇒ a →IO ()George Leontiev deltamethod GmbHScala solution
  4. 4. ..........................................................................................................Coredef acceptedChars ( c : Char ) = {v a l sum : ( ( ( Boolean , Boolean ) , Boolean )) ⇒Boolean = _ match {case (( a , b ) , c ) ⇒ a | | b | | c}v a l fun = ((_: Char ) . i s L e t t e r O r D i g i t ) &&&((_: Char ) . isWhitespace ) &&&((_: Char ) == ’ − ’)( fun >>> sum )( c )}http://www.haskell.org/arrows/index.htmlGeorge Leontiev deltamethod GmbHScala solution
  5. 5. ..........................................................................................................Coredef wordCount ( t e x t : S t r i n g ) : Map[ String , I n t ] =t e x t . f i l t e r ( acceptedChars )// s p l i t words. toLowerCase . s p l i t (”W” ) . t o L i s t// group. groupBy ( i d e n t i t y )// c a l c u l a t e group s i z e s. map { case ( key , value ) ⇒key . trim → value . length}George Leontiev deltamethod GmbHScala solution
  6. 6. ..........................................................................................................Typeclass instancesv a l N = 10i m p l i c i t v a l mapInstances = new Show [ L i s t [ ( String ,o v e r r i d e def shows ( l : L i s t [ ( String , I n t ) ] ) =l . f i l t e r N o t (_. _1 . isEmpty ). sortBy(−_. _2). take (N). f o l d L e f t (””) { case ( acc , ( key , value )) ⇒acc + ”n” + key + ”: ” + value}}George Leontiev deltamethod GmbHScala solution
  7. 7. ..........................................................................................................Executing// f u n c t i o n : : S t r i n g → IO S t r i n gdef mainIO ( path : S t r i n g ) =f o r {r e s u l t ← time ( f u n c t i o n ( path ))_ ← putStrLn ( r e s u l t )} y i e l d ()def main ( args : Array [ S t r i n g ] ) = {v a l path = args (0)// Yuck !mainIO ( path ) . unsafePerformIO ()}George Leontiev deltamethod GmbHScala solution
  8. 8. ..........................................................................................................Executingpackage i n f o . f o l o n e . wordsimport s c a l a z ._, Scalaz ._o b j e c t Main {def main ( args : Array [ S t r i n g ] ) {v a l path = args (0)v a l a c t i o n = WordsMemory . mainIO _ |+|WordsStream . mainIO _ |+|WordMachine . mainIO _// Yuck !ac t i o n ( path ) . unsafePerformIO ()}}George Leontiev deltamethod GmbHScala solution
  9. 9. ..........................................................................................................All setLet’s see how far we can push this solution.George Leontiev deltamethod GmbHScala solution
  10. 10. ..........................................................................................................First attemptdef w h o l e F i l e ( path : S t r i n g ) : IO [ S t r i n g ] =IO { Source . f r o m F i l e ( path ) }. bracket ( c l o s e ) {source ⇒IO {v a l t e x t = source . mkStringv a l r e s u l t = wordCount ( t e x t )r e s u l t . t o L i s t . shows}}George Leontiev deltamethod GmbHScala solution
  11. 11. ..........................................................................................................First attemptWorks fine, but eats all the heap on a large enough file.George Leontiev deltamethod GmbHScala solution
  12. 12. ..........................................................................................................Second attemptdef byLine ( path : S t r i n g ) : IO [ S t r i n g ] =IO { Source . f r o m F i l e ( path ) }. bracket ( c l o s e ) {source ⇒IO {v a l stream = source . g e t L i n e s . toStreamv a l r e s u l t = stream . map( wordCount ). f o l d L e f t (Map. empty [ String , I n t ] ) {case ( acc , v ) ⇒ acc |+| v}r e s u l t . t o L i s t . shows}}George Leontiev deltamethod GmbHScala solution
  13. 13. ..........................................................................................................Second attemptJust what is this |+|?George Leontiev deltamethod GmbHScala solution
  14. 14. ..........................................................................................................Typeclassesi n s t a n c e Show [ ( String , I n t ) ] where . . .i n s t a n c e Show Monoid b ⇒ Map a b where . . .http://debasishg.blogspot.de/2010/06/scala-implicits-type-classes-here-i.htmlGeorge Leontiev deltamethod GmbHScala solution
  15. 15. ..........................................................................................................Monoids(S, ⊗, 1)∀a, b ∈ S : a ⊗ b ∈ S∀a, b, c ∈ S : (a ⊗ b) ⊗ c = a ⊗ (b ⊗ c)∀a ∈ S : 1 ⊗ a = a ⊗ 1 = ahttp://apocalisp.wordpress.com/2010/06/14/on-monoids/George Leontiev deltamethod GmbHScala solution
  16. 16. ..........................................................................................................Second attemptPretty good, but can we do better?George Leontiev deltamethod GmbHScala solution
  17. 17. ..........................................................................................................IterateesScala machines(https://github.com/runarorama/scala-machines)https://dl.dropbox.com/u/4588997/Machines.pdfGave similar performance on a by-line basis.Thought, three times faster if we provide a Process to ssplit it bywords and then monoidally merge single-element Maps.George Leontiev deltamethod GmbHScala solution
  18. 18. ..........................................................................................................Iteratees – same as Streamdef wordFreq ( path : S t r i n g ) =g e t F i l e L i n e s (new F i l e ( path ) ,id outmap wordCount ) executeGeorge Leontiev deltamethod GmbHScala solution
  19. 19. ..........................................................................................................Iteratees – 3x fasterdef splitWords ( t e x t : S t r i n g ) : L i s t [ S t r i n g ] =t e x t . f i l t e r ( acceptedChars ). toLowerCase . s p l i t (”W” ) . t o L i s tv a l words : Process [ String , S t r i n g ] = ( f o r {s ← await [ S t r i n g ]_ ← traversePlan_ ( splitWords ( s ) ) ( emit )} y i e l d ( ) ) r e p e a t e d l ydef wordCount ( path : S t r i n g ) =g e t F i l e L i n e s (new F i l e ( path ) ,( id s p l i t words ) outmap (_. f o l d ( l ⇒ (1 , Map. empty [ String , I n t ] ) ,w ⇒ (0 , Map(w → 1 ) ) ) ) ) executeGeorge Leontiev deltamethod GmbHScala solution
  20. 20. ..........................................................................................................Wordcounting softwareScoobi http://nicta.github.com/scoobi/Spark http://spark-project.org/Scalding https://github.com/twitter/scalding/wiki/Type-safe-api-referenceGeorge Leontiev deltamethod GmbHScala solution
  21. 21. ..........................................................................................................WordcountingI did not have time to try to use those. But turns out, this codeshould work for these ”as is”.George Leontiev deltamethod GmbHScala solution
  22. 22. ..........................................................................................................That’s itQuestions?George Leontiev deltamethod GmbHScala solution

×