O documento discute como implementar controle de efeitos em Scala de maneira semelhante a Haskell usando técnicas como iteratees e monóides. Ele apresenta várias abordagens para contar palavras em um arquivo de texto, culminando na utilização de iteratees para processar o arquivo linha a linha de forma três vezes mais rápida.
19. .....
.
....
.
....
.
.....
.
....
.
....
.
....
.
.....
.
....
.
....
.
....
.
.....
.
....
.
....
.
....
.
.....
.
....
.
.....
.
....
.
....
.
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