Book Image

Scala Functional Programming Patterns

By : Atul S. Khot
Book Image

Scala Functional Programming Patterns

By: Atul S. Khot

Overview of this book

Scala is used to construct elegant class hierarchies for maximum code reuse and extensibility and to implement their behavior using higher-order functions. Its functional programming (FP) features are a boon to help you design “easy to reason about” systems to control the growing software complexities. Knowing how and where to apply the many Scala techniques is challenging. Looking at Scala best practices in the context of what you already know helps you grasp these concepts quickly, and helps you see where and why to use them. This book begins with the rationale behind patterns to help you understand where and why each pattern is applied. You will discover what tail recursion brings to your table and will get an understanding of how to create solutions without mutations. We then explain the concept of memorization and infinite sequences for on-demand computation. Further, the book takes you through Scala’s stackable traits and dependency injection, a popular technique to produce loosely-coupled software systems. You will also explore how to currying favors to your code and how to simplify it by de-construction via pattern matching. We also show you how to do pipeline transformations using higher order functions such as the pipes and filters pattern. Then we guide you through the increasing importance of concurrent programming and the pitfalls of traditional code concurrency. Lastly, the book takes a paradigm shift to show you the different techniques that functional programming brings to your plate. This book is an invaluable source to help you understand and perform functional programming and solve common programming problems using Scala’s programming patterns.
Table of Contents (19 chapters)
Scala Functional Programming Patterns
Credits
About the Author
Aknowledgement
About the Reviewers
www.PacktPub.com
Preface
Index

Index

A

  • abstractions
    • about / Abstractions
  • abstract override method / Scala's easy and breezy decorations – stackable modifications
  • accumulator idiom / Persistent data structures
  • Akka
    • about / Akka actors take the stage
    • actors / Akka actors take the stage
  • apply() factory method
    • using / The apply() factory method
  • autoboxing / Memoization and the flyweight pattern
  • auto_ptr template
    • reference link / The loan pattern

B

  • base case / Recursive structures
  • boilerplate
    • about / The loan pattern
  • Builder pattern
    • using / Builders
    • overloaded constructors, using / Ease of object creation
    • named arguments, using / Scala shines again
    • case class, using / Scala shines again

C

  • cached / Call by name
  • cake pattern / A taste of the cake pattern
  • call by name technique / Call by name
  • case statements
    • used, for deconstruction / Deconstruction with case statements
  • CGLIB
    • URL / Hibernate's lazy loading
  • chain of responsibility
    • about / The chain of responsibility
    • chain / The chain of responsibility
    • scalaish / Scalaish Chain Of Responsibilities
    • URL / Scalaish Chain Of Responsibilities
    • collect idiom / Match and mismatch – the collect idiom
  • ChoosyVet / A taste of the cake pattern
  • class invariants
    • URL / The iterator design pattern
  • command design pattern
    • about / The command design pattern
  • companion objects
    • about / Scala singletons
    • apply() factory method / The apply() factory method
    • factory method / The factory method pattern
    • apply method, using / The Scala version
  • composition over inheritance
    • about / The dreaded diamond
    • URL / The dreaded diamond
  • concise expression
    • about / Concise expression
  • concurrency heisenbugs
    • URL / Immutability is golden
  • criteria / The recursive grep
  • cross-cutting concern / Illusion and reality – the proxy pattern
  • currying
    • about / A taste of the curry
    • type inference / Type inference
    • implicit resolution / Of implicits and explicits
    • explicit resolution / Of implicits and explicits
    • code block style / Stylish blocks
    • implementing / Wrapping it up

D

  • database view / A view to a collection
  • data structures
    • making immutable / Immutability is golden
  • decorator pattern
    • using / Frills and thrills – the decorator pattern
  • decorators
    • implementing / Frills and thrills – decorators again
  • dependencies injection pattern / Dependencies injection pattern
  • Directed Acyclic Graphs (DAG)
    • about / Null Objects – singletons in another garb
  • Domain Specific Language (DSL)
    • about / Ease of object creation
  • Don’t Repeat Yourself principle (DRY) / Interfaces as types
  • dreaded diamond
    • about / The dreaded diamond
    • defeating / Defeating the dreaded diamond
  • duration
    • URL / Scala's Futures

E

  • enclosing scope
    • about / Roping in a scope
  • enum types
    • URL / Sealed traits
  • essence of recursion / Recursive structures
  • Eta expansion
    • about / The underscore – Scala's Swiss army knife
  • events / Events
  • exhaustiveness checks / Sealed traits
  • expression parser
    • about / An expression parser
  • external stimulus
    • reacting / Events
  • extract method refactoring
    • reference link / Serving the curry

F

  • factory method
    • about / The factory method pattern
  • FileFinder actor / Akka actors take the stage
  • filter / The recursive grep
  • filters
    • implementing / Pipes and filters
    • reference link / Pipes and filters
    • implementing, in Scala / Pipes and filters – the Scala version
  • filters out elements / Streams are collections
  • find command / Recursive structures, The recursive grep
    • decoupling, from egrep command / The recursive grep
  • first-class objects
    • about / A strategy in Scala land
  • First In First Out (FIFO) queue / The producer/consumer pattern
  • flatMap method
    • about / FlatMap
  • fluent interfaces
    • URL / Scala idioms
  • flyweight pattern
    • about / Memoization and the flyweight pattern
  • force call / A view to a collection
  • for expression
    • about / Foreach–sugary sweetener
    • one generator / One generator
    • generator and filter / A generator and a filter
    • URL / A generator and a filter
    • two generators / Two generators
  • free variable
    • about / Roping in a scope
  • Functiona1
    • URL / Fifth cut–using andThen
  • functional error handling
    • about / Functional error handling
    • pattern matching / Pattern matching
  • functional transformation / A view to a collection
  • functions
    • about / Functions, Functions as first-class values
  • Functors
    • about / Functors
    • maps / Maps
  • futures
    • about / Scala's Futures

G

  • grouping continuous integers
    • issues / The problem – grouping continuous integers
    • Java code / Java code
    • recursively / Thinking recursively...
    • reusability / Reusability – the commonality/variability analysis
    • commonality/variability analysis / Reusability – the commonality/variability analysis
    • one-liner shockers / The one-liner shockers

H

  • heisenbugs
    • URL / Persistent data structures
  • Hibernate
    • lazy loading / Hibernate's lazy loading
  • Hollywood principle
    • URL / Dependencies injection pattern

I

  • iContract
    • reference link / Builders
  • immutable
    • about / Immutable
  • implicit conversion
    • about / Scala shines again
  • infinite sequences
    • implementing / Infinite sequences – Scala streams
  • initialization
    • URL / Concise expression
  • Inoculate trait / A taste of the cake pattern
  • interfaces
    • as types / Interfaces as types
    • moveGoods(GoodsMover goodsMover)method / Interfaces as types
    • walk(Walks walker) method / Interfaces as types
  • Inversion Of Control (IoC)
    • about / Dependencies injection pattern
    • URL / Dependencies injection pattern
  • inverted index
    • about / An inverted index
  • iterator design pattern
    • about / The iterator design pattern

J

  • Java
    • verbosity / Verbosity

L

  • lazy collections
    • about / Lazy collections
  • lazy keyword / Lazy val – calling by need
  • lazy loaded / Hibernate's lazy loading
  • lazy loading, Hibernate
    • about / Hibernate's lazy loading
  • lazy vals / Lazy val – calling by need
  • leaky abstractions
    • URL / Immutability is golden
  • lexical scope
    • about / Roping in a scope
  • lifting, method
    • about / The underscore – Scala's Swiss army knife
  • linearization / Defeating the dreaded diamond
  • list
    • iterating / Pattern matching
    • nth element, obtaining / Getting the nth element of a list
    • forming / Infinite sequences – Scala streams
  • List extractor
    • URL / Deconstruction with case statements
  • loan pattern
    • about / The loan pattern
    • example / The loan pattern
  • local functions
    • defining / Local functions – hiding and biding their time

M

  • maps
    • about / Maps
  • master thread / Threads – masters and slaves
  • memoization
    • about / Memoization and the flyweight pattern
  • method invocation
    • reference link / Options are container
  • Mockito
    • URL / Dependencies injection pattern
  • mock object
    • URL / Dependencies injection pattern
  • Monads
    • about / Monads
  • monads / Monads
  • Monoids
    • about / Monoids
    • foldLeft method, using / It is a Monoid
  • Monostate
    • URL / Singletons – being one and only one
  • Multiple inheritance (MI) / Defeating the dreaded diamond

N

  • NameIt trait
    • about / Traits – Scala's rich interfaces
    • removing / Mix-ins – rich interfaces
  • natural ordering
    • about / Sorted
    • URL / Sorted
  • new list / Streams are collections
  • node
    • visiting / Pattern matching
  • non-strict sequences / Infinite sequences – Scala streams
  • nth element, of list
    • obtaining / Getting the nth element of a list
  • Null Object
    • about / Null Objects – singletons in another garb
    • reference link / Null Objects – singletons in another garb
    • Option class, using / Null Objects – the Scala way
    • sentinel-based tree node, writing / Options are container
  • Null Pointer Exception (NPE)
    • about / Null Objects – singletons in another garb

O

  • observer
    • about / Events
    • URL / Events
  • Open/Closed principle (OCP) / Passing algorithms around
  • Option class
    • reference link / Null Objects – the Scala way
    • using / Null Objects – the Scala way
  • overloaded constructors
    • using / Ease of object creation

P

  • parser combinators
    • about / Parser combinators
  • partial functions
    • about / Partial functions
    • visitor pattern / Visitor pattern – the Scala way
  • partially applied function
    • about / The underscore – Scala's Swiss army knife
  • pattern guard / Pattern guards
  • pattern matching
    • about / Pattern matching, Many hues of pattern matching
    • deconstruction, with case statements / Deconstruction with case statements
    • stack overflows / Stack overflows
    • de-structuring / De-structuring
    • typed patterns / Typed patterns
    • pattern guards / Pattern guards
    • tuple explosion / Tuple explosion
    • partial functions / Partial functions
  • patterns
    • about / Patterns and those aha! moments
    • command design pattern / The command design pattern
    • strategy design pattern / The strategy design pattern
  • persistent data structures
    • about / Persistent data structures
  • pipes
    • implementing / Pipes and filters
    • reference link / Pipes and filters
    • implementing, in Scala / Pipes and filters – the Scala version
  • poison pill / Threads – masters and slaves
  • polling / Events
  • producer/consumer pattern
    • about / The producer/consumer pattern
  • proxy pattern
    • using / Illusion and reality – the proxy pattern
    • URL / Illusion and reality – the proxy pattern
  • proxy server / Illusion and reality – the proxy pattern

Q

  • queues
    • URL / The producer/consumer pattern

R

  • RabbitMQ
    • URL / The producer/consumer pattern
  • race condition
    • URL / Immutability is golden
  • read-only view, of collection
    • URL / Immutability is golden
  • recursion
    • forms / Two forms of recursion
  • recursive case / Recursive structures
  • recursive grep
    • about / The recursive grep
  • recursive streams
    • about / Recursive streams
  • recursive structures
    • about / Recursive structures
  • reduce combinator / Reduce
  • refactoring
    • URL / Recursive streams, Dependencies injection pattern
    • reference link / Roping in a scope
  • referential transparency
    • about / Referential transparency
  • Replace Conditional with Polymorphism refactoring
    • URL / A tale of two hierarchies
  • Resource Acquisition Is Initialization (RAII)
    • about / The loan pattern
    • reference link / The loan pattern
  • rich interfaces, Scala / Traits – Scala's rich interfaces

S

  • Scala
    • streams / Infinite sequences – Scala streams
    • rich interfaces / Traits – Scala's rich interfaces
    • stackable modifications / Scala's easy and breezy decorations – stackable modifications
  • scalaidioms
    • about / Scala idioms
  • Scalaish Schwartzian transform
    • about / Scalaish Schwartzian transform
  • Scala way
    • iterating by / Iterating the Scala way
  • Scalaz
    • URL / The one-liner shockers
  • scope
    • about / Roping in a scope
    • lexical scope / Roping in a scope
  • sealed traits / Sealed traits
  • self-annotation feature / A taste of the cake pattern
  • sentinel node
    • about / Null Objects – singletons in another garb
  • side effects free / Functions
  • sieve method / Sieve of Eratosthenes
  • Sieve of Eratosthenes algorithm
    • about / Sieve of Eratosthenes
  • Simple Build Tool (sbt)
    • URL / The one-liner shockers
  • Single Point Of Truth (SPOT) principle
    • about / Interfaces as types
    • URL / Interfaces as types
  • Single Responsibility Principle (SRP) / The recursive grep
  • singleton
    • about / Singletons – being one and only one
  • slave thread / Threads – masters and slaves
  • slice and dice
    • about / Pattern matching
    • URL / Pattern matching
  • SortBy method
    • about / SortBy
  • sorted method
    • about / Sorted
  • SortWith method
    • about / SortWith
  • stackable modifications
    • about / Scala's easy and breezy decorations – stackable modifications
  • stack overflows / Stack overflows
  • stacks
    • URL / The producer/consumer pattern
  • strategy design pattern
    • about / The strategy design pattern, The strategy design pattern
    • in Scala / A strategy in Scala land
  • Stream.continually() method / Infinite sequences – Scala streams
  • Streams / Two forms of recursion
    • about / Streams are collections
  • strict sequences / Infinite sequences – Scala streams

T

  • tail call / Tail recursion to the rescue
  • tail recursion
    • using / Tail recursion to the rescue
  • template method
    • implementing / Serving the curry
  • test double technique
    • URL / Dependencies injection pattern
  • threads
    • about / Threads and futures
  • Thunk / Infinite sequences – Scala streams
  • traits
    • using / Mix-ins – rich interfaces
  • transformations / A view to a collection

U

  • underscore
    • about / The underscore – Scala's Swiss army knife
  • Unit / Lazy val – calling by need
  • Urban Dictionary
    • URL / Abstractions

V

  • validation, issues
    • about / A validation problem
    • stage, setting / Setting the stage
    • arrays, using / First cut–using arrays
    • map, using / Second cut–using a map
    • for expression, using / Third cut–using a for expression
    • foldLeft, using / Fourth cut–using foldLeft
    • andThen, using / Fifth cut–using andThen
    • compose, using / Sixth cut–using compose
  • value of the block / Lazy val – calling by need
  • variable
    • declaring / Roping in a scope
    • reference link / Roping in a scope
  • view / A view to a collection
  • visitor pattern
    • about / A tale of two hierarchies, The Visitor pattern

W

  • walk() method / The dreaded diamond
  • worker thread / Threads – masters and slaves