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

Learning Elixir

Book Description

Unveil many hidden gems of programming functionally by taking the foundational steps with Elixir

About This Book

  • Explore the functional paradigms of programming with Elixir through use of helpful examples
  • Concise step-by-step instructions to teach you difficult technical concepts
  • Bridge the gap between functional programming and Elixir

Who This Book Is For

This book targets developers new to Elixir, as well as Erlang, in order to make them feel comfortable in functional programming with Elixir, thus enabling them to develop more scalable and fault-tolerant applications.

Although no knowledge of Elixir is assumed, some programming experience with mainstream Object-Oriented programming languages such a Ruby, Python, Java, C# would be beneficial.

What You Will Learn

  • Explore Elixir to create resilient, scalable applications
  • Create fault-tolerant applications
  • Become better acquainted with Elixir code and see how it is structured to build and develop functional programs
  • Learn the basics of functional programming
  • Gain an understanding of effective OTP principles
  • Design program-distributed applications and systems
  • Write and create branching statements in Elixir
  • Learn to do more with less using Elixir's metaprogramming
  • Be familiar with the facilities Elixir provides for metaprogramming, macros, and extending the Elixir language

In Detail

Elixir, based on Erlang's virtual machine and ecosystem, makes it easier to achieve scalability, concurrency, fault tolerance, and high availability goals that are pursued by developers using any programming language or programming paradigm. Elixir is a modern programming language that utilizes the benefits offered by Erlang VM without really incorporating the complex syntaxes of Erlang.

Learning to program using Elixir will teach many things that are very beneficial to programming as a craft, even if at the end of the day, the programmer isn't using Elixir. This book will teach you concepts and principles important to any complex, scalable, and resilient application. Mostly, applications are historically difficult to reason about, but using the concepts in this book, they will become easy and enjoyable. It will teach you the functional programing ropes, to enable them to create better and more scalable applications, and you will explore how Elixir can help you achieve new programming heights. You will also glean a firm understanding of basics of OTP and the available generic, provided functionality for creating resilient complex systems. Furthermore, you will learn the basics of metaprogramming: modifying and extending Elixir to suite your needs.

Style and approach

An exploration of functional programming and Elixir with easy to follow examples using Elixir and the functional style. All the topics, concepts, and principles covered are clearly and concisely explained with either code examples or in depth discussions, or both!

Table of Contents

  1. Learning Elixir
    1. Table of Contents
    2. Learning Elixir
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Introducing Elixir – Thinking Functionally
      1. Why functional?
      2. Installing Elixir
        1. GNU/Linux
        2. Apple Mac OS X
        3. Windows
        4. Manual installation – binary
        5. Manual installation – source
      3. Hello, World!
        1. Using the IO.puts/2 function
        2. Using the inspect/2 function
      4. Exercises
      5. Summary
    9. 2. Elixir Basics – Foundational Steps toward Functional Programming
      1. Everything is an expression
      2. A short introduction to types
        1. Numerical types
          1. Memory usage
        2. Binary, hexadecimal, and octal numbers
        3. Atoms
          1. Atom memory usage
        4. Booleans
        5. Strings
        6. (Linked) Lists
          1. A little more about strings
          2. Ranges
        7. Tuples
          1. Tuples or lists
        8. Binaries
          1. Even more about Strings
        9. Some more built-in types
          1. Functions
          2. Process IDs
      3. Invariable variables and pattern matching
        1. Using the underscore
        2. More pattern matching
          1. IEEE-754
      4. Elixir structure
      5. Elixir files
      6. Exercises
      7. Summary
    10. 3. Modules and Functions – Creating Functional Building Blocks
      1. Modules
      2. Anonymous functions
        1. Pattern matching
      3. Named functions
        1. Private functions
      4. Calling functions
        1. When to use .
      5. Grabbing functions
      6. When patterns aren't enough for matching
      7. Functional algorithms for everyday problems
        1. Iteration versus recursion
          1. Performance considerations
        2. Reverse
        3. Sorting
      8. Mix – the ladle of Elixir
        1. Structure of Elixir projects
          1. mix.exs
          2. .gitignore
          3. config
          4. README.md
          5. lib
          6. test
          7. Compiling a project
          8. Testing a project
          9. Running interactively
          10. Files
          11. Mix and beyond
      9. Building functional projects
        1. Flatten
          1. A small introduction to testing
      10. More to do about modules
      11. Testing with comments
      12. Exercises
      13. Summary
    11. 4. Collections and Stream Processing
      1. Keywords, maps, and dictionaries
        1. Keywords
        2. Maps
        3. Dictionaries
        4. More pattern matching
        5. Modifying dictionaries
        6. Performance considerations
        7. Structures and Hash dicts
          1. Yet another dictionary type
      2. Flow-based programming
      3. Stream processing and Elixir
        1. Processing with the Enum module
        2. Processing with the Stream module
          1. Greedy versus lazy
          2. Stream examples
            1. Koolaid
        3. Graphs
          1. A small introduction to graphs
          2. Node ancestors
      4. Exercises
      5. Summary
    12. 5. Control Flow – Occasionally You Need to Branch
      1. Branching with Elixir
        1. if and unless
        2. The new else if
        3. Elixir case expressions
      2. Examples using branching
        1. FizzBuzz
        2. Mergesort
          1. Writing tests
          2. Implementing the sort
      3. Exception handling
        1. Raising exceptions
        2. Error, exit, and throw
        3. Handling exceptions
          1. The try-rescue blocks
          2. The try-catch blocks
        4. Using exceptions
          1. Opening files
        5. Exceptions recap
      4. Determinism
      5. References
      6. Exercises
      7. Summary
    13. 6. Concurrent Programming – Using Processes to Conquer Concurrency
      1. Parallel computation and concurrent computation
      2. Erlang processes and OS processes
        1. Parallel map
      3. Basics of Elixir process
        1. Self
        2. Sending messages
        3. Receiving messages
        4. Spawn
        5. Process links
          1. Spawning with links
        6. Process monitor
        7. Storing state inside processes
        8. Naming processes
        9. Process module
      4. Applications
        1. Ping pong
        2. Work pool
      5. Summary
    14. 7. OTP – A Poor Name for a Rich Framework
      1. Applications
      2. Gen(eric) behaviours
        1. Gen(eric) servers
          1. Asynchronous messaging
        2. Gen(eric) events
        3. Special OTP processes
        4. Variable scope in the Gen* processes
        5. Back-pressure and load shedding
      3. Supervisors
        1. Fail fast(er)
        2. Designing with supervisors
        3. Assumptions of the OTP process initialization
      4. Exercises
      5. Summary
    15. 8. Distributed Elixir – Taking Concurrency to the Next Node
      1. Obligatory discussion about distributed computing
        1. Fallacies of distributed computing
          1. The network is reliable
          2. There is no latency
          3. Bandwidth is infinite
          4. The network is secure
          5. Topology doesn't change
          6. There is only one administrator
          7. Transport cost is zero
          8. The network is homogeneous
          9. Fighting dragons blindfolded, equipped with a butter knife
        2. CAP – choose two, and one will be partition tolerance
          1. Why you must choose partition tolerance
          2. Relaxing definitions
      2. Another short discussion about networks
        1. Topologies
      3. Distributed computing with Elixir
        1. OTP nodes
          1. Node names
          2. Connecting nodes
            1. Cookies and node security
          3. Node ping pong
          4. Group leader
          5. Globally registered names
      4. Summary
    16. 9. Metaprogramming – Doing More with Less
      1. Behaviours and protocols
        1. Behaviours
          1. Defining behaviours
            1. A short introduction to typespecs
            2. Using – implementing – behaviours
          2. Protocols
            1. Built-in protocols
      2. Abstract syntax trees
      3. Our first macros
      4. Context and macro hygiene
        1. Unhygienic macros – overriding context
      5. Macro examples
        1. Debugging and tracing
        2. Static data to functions
        3. Testing macros
      6. Domain-specific languages
        1. Creating a DSL
          1. State of the macro
      7. With great power...
      8. Exercises
      9. Summary
      10. Going forward
    17. Index