. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Core Main functions wordCount : : S t r i n g → Map ( String , I n t ) acceptedChars : : Char → Boolean Helper functions time : : ( a → IO b) → IO b c l o s e : : C l o s e a b l e a ⇒ a →IO () George Leontiev deltamethod GmbH Scala solution
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Core def 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.html George Leontiev deltamethod GmbH Scala solution
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Core def 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 GmbH Scala solution
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Typeclass instances v a l N = 10 i 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 GmbH Scala solution
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Executing // f u n c t i o n : : S t r i n g → IO S t r i n g def 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 GmbH Scala solution
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Executing package i n f o . f o l o n e . words import 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 ! a c t i o n ( path ) . unsafePerformIO () } } George Leontiev deltamethod GmbH Scala solution
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . First attempt def 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 . mkString v 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 GmbH Scala solution
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Second attempt def 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 . toStream v 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 GmbH Scala solution
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Typeclasses i 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.html George Leontiev deltamethod GmbH Scala solution
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Iteratees – same as Stream def 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 ) execute George Leontiev deltamethod GmbH Scala solution
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Iteratees – 3x faster def 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 t v 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 y def 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 ) ) ) ) ) execute George Leontiev deltamethod GmbH Scala solution