# Functional Programming in Scala

## Paul Chiusano, Runar Bjarnason

Language: English

Pages: 304

ISBN: 1617290653

Format: PDF / Kindle (mobi) / ePub

Functional programming (FP) is a programming style emphasizing functions that return consistent and predictable results regardless of a program's state. As a result, functional code is easier to test and reuse, simpler to parallelize, and less prone to bugs. Scala is an emerging JVM language that offers strong support for FP. Its familiar syntax and transparent interoperability with existing Java libraries make Scala a great place to start learning FP.

*Functional Programming in Scala* is a serious tutorial for programmers looking to learn FP and apply it to the everyday business of coding. The book guides readers from basic techniques to advanced topics in a logical, concise, and clear progression. In it, they'll find concrete examples and exercises that open up the world of functional programming.

Purchase of the print book comes with an offer of a free PDF, ePub, and Kindle eBook from Manning. Also available is all code from the book.

println(formatResult("factorial", 7, factorial)) println(formatResult("increment", 7, (x: Int) => x + 1)) println(formatResult("increment2", 7, (x) => x + 1)) println(formatResult("increment3", 7, x => x + 1)) println(formatResult("increment4", 7, _ + 1)) println(formatResult("increment5", 7, x => { val r = x + 1; r })) } The The The The The The The absolute value of -42 is 42 factorial of 7 is 5040 increment of 7 is 8 increment2 of 7 is 8 increment3 of 7 is 8 increment4 of 7 is 8 increment5 of

an imperative API that relies on side effects, see if you can provide a purely functional version of it, and use some of the functions we wrote here to make working with it more convenient. ©Manning Publications Co. We welcome reader comments about anything in the manuscript — other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders. Licensed to Leonardo Urbina

Gen.choose(1,10). Note that if we can't exhaustively enumerate all possible values, we'll likely want to sample our values differently—for instance, if we have Gen.choose(1,10), we could generate 1, 2, ... 10 in sequence, but if we have Gen.choose(0,1000000000), we won't be able to enumerate all possible values and should probably sample our random values more uniformly from the range. If we want to support both modes of test case generation (random and exhaustive), we need to extend Gen.

scope does—if run(p)(s) is Left(e), then run(scope(msg)(p)) is Left(e2), where errorStack(e2) will have at the top of the stack the message msg, followed by any messages added by p itself. We can take this one step further. A stack does not fully capture what the parser was doing at the time it failed. Consider the parser scope("abc")(a or b or c). If a, b, and c all fail, which error goes at the top of the stack? We could adopt some global convention, like always reporting the last parser's

it's only slightly more interesting. The real power of monoids comes from the fact that they compose. This means, for example, that if types A and B are monoids, then the tuple type (A, B) is also a monoid (called their product). EXERCISE 17: Prove it. def productMonoid[A,B](A: Monoid[A], B: Monoid[B]): Monoid[(A,B)] EXERCISE 18: Do the same with Either. This is called a monoid coproduct. def coproductMonoid[A,B](A: Monoid[A], B: Monoid[B]): Monoid[Either[A,B]] 10.5.1 Assembling more complex