Mastering Functional Programming

Book description

Learn how functional programming can help you in deploying web servers and working with databases in a declarative and pure way

Key Features

  • Learn functional programming from scratch
  • Program applications with side effects in a pure way
  • Gain expertise in working with array tools for functional programming

Book Description

In large projects, it can get difficult keeping track of all the interdependencies of the code base and how its state changes at runtime. Functional Programming helps us solve these problems. It is a paradigm specifically designed to deal with the complexity of software development. This book will show you how the right abstractions can reduce complexity and make your code easy to read and understand.

Mastering Functional Programming begins by touching upon the basics such as what lambdas are and how to write declarative code with the help of functions. It then moves on to more advanced concepts such as pure functions and type classes, the problems they aim to solve, and how to use them in real-world scenarios. You will also explore some of the more advanced patterns in the world of functional programming, such as monad transformers and Tagless Final. In the concluding chapters, you will be introduced to the actor model, implement it in modern functional languages, and explore the subject of parallel programming.

By the end of the book, you will have mastered the concepts entailing functional programming along with object-oriented programming (OOP) to build robust applications.

What you will learn

  • Write reliable and scalable software based on solid foundations
  • Explore the cutting edge of computer science research
  • Effectively solve complex architectural problems in a robust way
  • Avoid unwanted outcomes such as errors or delays and focus on business logic
  • Write parallel programs in a functional style using the actor model
  • Use functional data structures and collections in your day-to-day work

Who this book is for

If you are from an imperative and OOP background, this book will guide you through the world of functional programming, irrespective of which programming language you use.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Mastering Functional Programming
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. The Declarative Programming Style
    1. Technical requirements
    2. Principles of declarative programming
      1. Example – go-to versus loops
      2. Example – nested loop
      3. Don't Repeat Yourself (DRY)
    3. Declarative versus imperative collections
      1. Filtering
    4. Declarative programming in other languages
    5. Summary
    6. Questions
  8. Functions and Lambdas
    1. Functions as behavior
    2. Functions in functional programming
    3. Higher-order functions
    4. Understanding lambda functions
    5. The concept of functions in different programming languages
    6. Summary
    7. Questions
  9. Functional Data Structures
    1. Collections framework
      1. Imperative collections
      2. Functional collections
    2. Algebraic approach
    3. Effect types
      1. Try
      2. Option
    4. Data structures in different programming languages
    5. Summary
    6. Questions
    7. Further reading
  10. The Problem of Side Effects
    1. Side effects
    2. Mutable states
    3. Pure functions
      1. Referential transparency
    4. Generally encountered side effects
      1. Error
      2. Absence of result
      3. Delay and asynchronous computations
      4. Logging
      5. Input-output operations
    5. But how do we get rid of the side effects?
    6. The pure functional paradigm in different languages
    7. Summary
    8. Questions
  11. Effect Types - Abstracting Away Side Effects
    1. Turning effects into data
    2. The sequential combination of effects with Monads
      1. Introducing the map function
      2. Introducing the flatMap function
    3. Summary
    4. Questions
  12. Effect Types in Practice
    1. Future
      1. Motivation and the imperative example
      2. Abstraction and the functional example
      3. Composing Futures
      4. flatMap generalized
    2. Either
    3. Reader
      1. Functional solution
    4. Summary
    5. Questions
  13. The Idea of the Type Classes
    1. Rich Wrapper pattern
      1. Motivation
      2. Implicit conversions
      3. Rich Wrapper
    2. The Type Class pattern
    3. Interpretation of the Type Class pattern
      1. Injectable interfaces
      2. Toolboxes
    4. Type classes in different languages
    5. Summary
    6. Questions
  14. Basic Type Classes and Their Usage
    1. A motivation for organizing type classes into systems and libraries
    2. The Cats library for purely functional programming
      1. The structure of the library
      2. Core
      3. Type class hierarchy
        1. Abstract methods
        2. Concrete methods
      4. Laws
      5. Syntax
      6. Instances
      7. Data
      8. Infrastructure synergy
    3. Type classes
      1. Monad
        1. Writer effect type
        2. The tailRecM method
      2. Functor
      3. Applicative
        1. Motivation
        2. Applicative type class
        3. Implementation of the type class
      4. Monoid
        1. Implementation for Either
        2. MonoidK
      5. Traverse
    4. Summary
    5. Questions
  15. Libraries for Pure Functional Programming
    1. Cats effect
      1. ProductR
      2. IO – the concurrence data type
        1. Referential transparency
        2. Inversion of control
      3. Asynchrony with IO
        1. Blocking example
        2. Concurrency infrastructure
        3. Running tasks in bunches
        4. Heavy load with blocking
        5. Synchronous tasks
      4. Constructing asynchronous tasks
        1. Asynchronous API
        2. Asynchronous example
      5. Fibers
        1. The computation
        2. IO combination without Fibers
        3. IO combination with Fibers
        4. Canceling Fibers
      6. Bracket
    2. Server-side programming
      1. The architecture of a server-side application
        1. Communication protocol
        2. The software architecture of a server
        3. Example specification
      2. Orchestration and infrastructure
        1. Docker
          1. Docker-compose
          2. Dockerfiles
      3. Backend architecture
        1. Model
        2. Database layer
      4. Server-side programming
      5. Querying the server
    3. Summary
    4. Questions
  16. Patterns of Advanced Functional Programming
    1. Monad Transformers
      1. The specialization of effect types
      2. An application with multiple side effects
        1. Asynchrony
        2. The side effect of errors
      3. Monad Transformers
      4. Generalizing the pattern
    2. Tagless Final
      1. Programming to capabilities
      2. Implementations
      3. Execution semantics abstraction
      4. Computation as a value
      5. Free Monad
    3. Type-level programming
      1. A naive implementation of the heterogeneous list
      2. Type-level solution to the heterogeneous list problem
      3. Reclusive implicit resolution
      4. Debugging type-level computations
      5. Libraries for type-level programming
    4. Summary
    5. Questions
  17. Introduction to the Actor Model
    1. Overview of parallelism solutions
    2. Traditional model synchronization on monitors
      1. Synchronization
      2. Problems with the traditional model – race conditions and deadlocks
    3. The actor model as a replacement for the traditional model
      1. Deadlock example revisited
    4. Summary
    5. Questions
  18. The Actor Model in Practice
    1. Akka overview
      1. Principles of Akka
        1. Encapsulation
        2. Messaging
        3. No leaking of mutable state
        4. Fault-tolerance and supervision
        5. Messaging guarantees
        6. Asynchrony
    2. Defining, creating, and messaging actors
      1. Callbacks
      2. Supervision
      3. Context and references
        1. Managing the actor hierarchy
        2. Managing the life cycle
        3. Supervision
      4. Creating actors
      5. Actor parameters
    3. Working with actor systems
      1. Task specification
      2. Implementation
    4. Summary
    5. Questions
  19. Use Case - A Parallel Web Crawler
    1. Problem statement
      1. The graph structure of the web
      2. Collecting information from the graph
      3. Parallel nature of the task
    2. Sequential solution
    3. A parallel solution with Akka
      1. Strategy
      2. Implementation
      3. Caveats
        1. Visited links
        2. Fault tolerance
        3. Counting the responded actors
        4. Real-world side effects
    4. Summary
  20. Introduction to Scala
    1. Motivation for using Scala
    2. Scala infrastructure
      1. Scala interpreter
      2. SBT build tool
    3. Variables and functions
    4. Control structures
      1. If and While
      2. For
      3. Pattern matching
      4. Partial functions
    5. Inheritance model
      1. Classes
      2. Traits
      3. Singleton objects
    6. Summary
  21. Assessments
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
  22. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Mastering Functional Programming
  • Author(s): Anatolii Kmetiuk
  • Release date: August 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788620796