O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Functional Programming: A PragPub Anthology

Book Description

Explore functional programming and discover new ways of thinking about code. You know you need to master functional programming, but learning one functional language is only the start. In this book, through articles drawn from PragPub magazine and articles written specifically for this book, you'll explore functional thinking and functional style and idioms across languages. Led by expert guides, you'll discover the distinct strengths and approaches of Clojure, Elixir, Haskell, Scala, and Swift and learn which best suits your needs.

Contributing authors: Rich Hickey, Stuart Halloway, Aaron Bedra, Michael Bevilacqua-Linn, Venkat Subramaniam, Paul Callaghan, Jose Valim, Dave Thomas, Natasha Murashev, Tony Hillerson, Josh Chisholm, and Bruce Tate.

Functional programming is on the rise because it lets you write simpler, cleaner code, and its emphasis on immutability makes it ideal for maximizing the benefits of multiple cores and distributed solutions. So far nobody's invented the perfect functional language - each has its unique strengths. In Functional Programming: A PragPub Anthology, you'll investigate the philosophies, tools, and idioms of five different functional programming languages.

See how Swift, the development language for iOS, encourages you to build highly scalable apps using functional techniques like map and reduce. Discover how Scala allows you to transition gently but deeply into functional programming without losing the benefits of the JVM, while with Lisp-based Clojure, you can plunge fully into the functional style. Learn about advanced functional concepts in Haskell, a pure functional language making powerful use of the type system with type inference and type classes. And see how functional programming is becoming more elegant and friendly with Elixir, a new functional language built on the powerful Erlang base.The industry has been embracing functional programming more and more, driven by the need for concurrency and parallelism. This collection of articles will lead you to mastering the functional approach to problem solving. So put on your explorer's hat and prepare to be surprised. The goal of exploration is always discovery.

What You Need:

Familiarity with one or more programming languages.

Table of Contents

  1.  Introduction
  2.  Acknowledgements
  3. Part I. The Functional Paradigm
    1. 1. Functional Programming Is Big Again
      1. We’ve Seen This Movie Before
      2. New Arguments for Functional Programming
    2. 2. Functional Thinking for the Imperative Mind
      1. It’s About Functions
      2. It’s About Immutability
      3. It’s a Way of Thinking
  4. Part II. Scala: A Hybrid Language
    1. 3. Scala and Functional Style
      1. Functional Purity
      2. Higher-Order Functions
      3. A Simple Example
      4. A Practical Example
      5. What About Debugging and Performance?
    2. 4. Working with Scala Collections
      1. Immutable Collections
      2. Mutable Collections
      3. Lazy Collections
    3. 5. Creating Higher-Order Functions in Scala
      1. Creating a Higher-Order Function
      2. Multiple Parameter Lists
      3. Function Values and the Loan Pattern
  5. Part III. Clojure: The New Lisp
    1. 6. An Interview with Rich Hickey
      1. Why Clojure?
      2. The Infrastructure
      3. Compared to What?
    2. 7. Getting Clojure: Why Lisp Still Matters
      1. The REPL
      2. Vectors and Keywords
      3. Macros
    3. 8. Identity, Value, and State in Clojure
      1. The Object-Oriented Model
      2. The Clojure Model
    4. 9. Concurrent Programming in Clojure
      1. A "Simple" Concurrent Programming Problem
      2. Clojure’s Solution
  6. Part IV. Elixir: Making Programming Fun Again
    1. 10. Patterns and Transformations in Elixir
      1. Pattern Matching
      2. Pattern Matching Structured Data
      3. Pattern Matching and Functions
      4. Transformation Is Job #1
    2. 11. Getting Functional with Elixir
      1. Anonymous Functions
      2. Named Functions
      3. A Practical Example
      4. Refactor to Functional Style
      5. What’s Different About This Code
    3. 12. Getting Parallel with Elixir
      1. The Actor Model
      2. Actors and Elixir
      3. Messages
      4. Monitoring Your Processes
      5. A Final Example
      6. Concurrency Is the Core of Elixir
  7. Part V. Haskell: The Researcher’s Playground
    1. 13. Functional Thinking and Haskell
      1. What It’s All About
      2. A Quick Exercise
      3. Data Types Are Cheap
      4. Pattern Matching
      5. Recursion, Control, and Higher-Order Functions
      6. Further Features
    2. 14. Haskell Hands-On
      1. One Step at a Time
      2. Generating Candidates
      3. Dictionary Filtering
      4. Breadth-First Searching
      5. Using the Search
      6. Performance and Optimization
  8. Part VI. Swift: Functional Programming for Mobile Apps
    1. 15. Swift: What You Need to Know
      1. Hello, Swift!
      2. Functional Swift
    2. 16. Functional Thinking in Swift
      1. Avoid Nil, Unless You Mean It
      2. Avoid Mutable State
      3. Use Higher-Order Functions
  9. Part VII. Going Deeper
    1. 17. Protocols in Swift vs. Ruby and Elixir
      1. The Problem with Extensions
      2. The Case for Protocols
      3. Protocols and Extensions
    2. 18. Pattern Matching in Scala
      1. Counting Coins
      2. Matching All the Things
      3. Using Extractions
    3. 19. Concurrency in Scala
      1. Using Parallel Collections
      2. Knowing When to Use Concurrency
      3. Revisiting an Earlier Example
    4. 20. Clojure’s Exceptional Handling of ExceptionsClojure’s Exceptional Handling of Exceptions
      1. A Simple Example
      2. The Problem with Exceptions
      3. A Solution: Conditions
      4. Make Life Simple for Your Callers
      5. Laziness and Errors
    5. 21. A Testing Framework for Elixir
      1. Investing in Testing
      2. One Experiment, Several Measurements
      3. Optimizing Setup with TrueStory
      4. Condense and Combine the Measurements
      5. Controlling Setup Repetition with Nested Contexts
      6. Controlling Setup Repetition with Story Pipes
    6. 22. Building Test Data with Elixir
      1. The Typical Approaches
      2. Beautiful Data for Beautiful Tests
      3. Registering Templates and Prototypes with Forge
      4. Instantiating Template Entries
      5. Mutual Attributes and Having
      6. Creating Structs
      7. Creating Custom Entities
      8. Customizing Persistence
    7. 23. Haskell’s Type SystemHaskell’s Type System
      1. TL;DR
      2. What Are Types For?
      3. A Concrete Example: Sorting
      4. The Language of Haskell’s Type System
      5. Type Inference and Type Checking
      6. Some Examples
      7. Comfort Break
      8. Interfaces and Type Classes
      9. Some Real-World Examples with Interfaces
      10. Pros and Cons—the 80/20 Rule
      11. Beyond Haskell: Dependent Types
      12. Propositions Are Types, and Proofs Are Programs
      13. Another Look at Sorting
      14. Back to Earth
    8. 24. A Haskell Project: Testing Native Code
      1. Our Native Code
      2. Our Model
      3. A Brief Introduction to Haskell’s FFI
      4. Wrapping Our Native Code in Haskell
      5. Experimenting with GHCi
      6. A Brief Introduction to QuickCheck
      7. Writing an Equivalence Property
      8. Defect Smashing
    9. 25. The Many Faces of Swift Functions
      1. Anatomy of Swift Functions
      2. Calling All Functions
      3. Calling on Methods
      4. Instance Methods Are Curried Functions
      5. Init: A Special Note
      6. Fancy Parameters
      7. Access Controls
      8. Fancy Return Types
      9. Nested Functions
    10. 26. A Functional Approach to Lua
      1. First-Class Functions in Lua
      2. Recursion in Lua
      3. Building with Functional Primitives
      4. A Simple Game Animation
  10. A1. Meet the Authors
  11.  Bibliography