topics

2021-02-05

"Elixir Practical Guide", a book written by our CEO Kuroda, has been released by Impress.

Buy at Amazon

I don't remember exactly when I first encountered Elixir, the programming language that is the subject of this book. Let's vaguely say it was in the mid-2010s. For about a decade before that, I used Ruby on Rails (a web development framework based on the Ruby programming language, commonly known as "Rails") exclusively. I've developed web applications with Rails and written books explaining Rails.

I'm not even sure why I got interested in Elixir. I had heard rumors that there was such a thing as a "functional programming language" out there, and that it was very interesting. I had even done some research on functional languages other than Elixir. However, I had a preconceived notion that they were favored by researchers and theorists, but were not practical, so I did not want to take them seriously.

Then a turning point came for me: in the late 2010s, single page applications (SPA) became all the rage in the world of web application development. SPA is a generic term for web applications that provide UI/UX similar to desktop applications. The use of JavaScript frameworks (React, Vue, etc.) to support SPA development spread rapidly. In order to ride this huge wave, I vigorously collected information and repeated trial and error. In the process, I discovered Phoenix Framework, an Elixir-based web development framework, commonly known as "Phoenix".

As my research progressed, I became more and more excited; Phoenix seemed to be an elegant solution to the various problems I was facing in SPA development at the time. Phoenix seemed like an elegant solution to many of the problems I was facing in SPA development at the time, and by pursuing the concurrent programming that Elixir excelled at, I could create innovative web services that went beyond the traditional boundaries. I had a hunch that Phoenix would be a game changer in our industry.

Once we actually built a concrete web application of some scale, we found Elixir to be a very easy to write and practical programming language. The functional programming language constraint that all data is immutable was not a hindrance to development. In addition, Elixir does not have what is called "inheritance" in object-oriented programming (OOP), but surprisingly, this was not a source of frustration.

Compared to Java, PHP, Python, Ruby, etc., Elixir has a very short history and is not well known by the general public. However, I predict that Elixir will have a golden age in the 2020s. The reason for this is the enthusiasm of the Elixir community. The Elixir ecosystem is rapidly maturing, with many talented people and influential companies joining the fray.

Now, this book is an introduction to Elixir. It explains basic concepts like "variables" and "integers" in as much detail as possible, in order to open the door not only to current programmers who want to try a new language, but also to beginners in programming. In addition, we have included many concrete source codes and their execution results. In particular, we have tried to explain in detail the examples of incorrect source codes and the warnings and error messages that are output when they are executed. This is to avoid misleading beginners and discouraging them from learning.

This book is divided into two parts: first part and second part, and this is the first part. The main topics in the first part are modules, functions, data types, and pattern matching. The second part covers structures, polymorphism, behaviors, metaprogramming, and concurrent programming. The second part will be published as "Advanced Elixir Practical Guide".

The first part is further divided into five parts. Part I is a rather quick introduction to the basic concepts of Elixir programming. Learn about Elixir's distinctive language specifications here, including data types, modules, functions, macros, pattern matching, pipe operators, and more. The procedure for setting up an Elixir development environment will be explained in Chapter 2.

In Part II, we will discuss modules and functions in detail. We will also discuss macros, but not in too much depth. Macros will be covered in the second part. Part III deals with Elixir data structures. Part III covers Elixir data structures, especially lists (Chapter 12) and maps (Chapter 14).

Part IV, titled "The Fascinating World of Elixir", focuses on pattern matching, a language specification that makes Elixir's lack of inheritance a no-brainer. Other interesting topics covered in this part include "comprehension notation" and "reduce functions".

In Part V, we will do development exercises on the subject of tic-tac-toe. The first half is preparation work. First, we will explain how to use Elixir's testing framework ExUnit and Elixir's build tool Mix. Next, we will explain the two concepts of agents and GenServer as the necessary tooling for the exercise. In the second half of Part V, we will actually develop an application. In the second half of Part V, we will actually develop the application, and through this development exercise, the reader will learn various know-how. For example, how to develop in "project" units combining multiple modules, how to interact with users on the terminal, how to keep data in the application, how to write and run tests, and so on.

Practice questions are provided at the end of each chapter from Chapter 3 to Chapter 25. It is difficult to learn a programming language just by reading a book, so we encourage you to challenge yourself without looking at the answers in the appendix at the end of the book.

November 2020
Tsutomu Kuroda

Table of Contents

  • Part 1
    • Chapter 1 Introduction
      • 1.1 What is Elixir?
      • 1.2 The Relationship Between Elixir and Erlang
    • Chapter 2 Building an Elixir Development Environment
      • 2.1 Creating an Elixir Container
      • 2.2 How to Use Elixir Containers
    • Chapter 3 Elixir Basics(1)
      • 3.1 How an Elixir Program Works
      • 3.2 Basic Concepts
      • 3.3 Exercises
    • Chapter 4 Elixir Basics(2)
      • 4.1 Data Types
      • 4.2 Numbers
      • 4.3 Atoms
      • 4.4 Strings
      • 4.5 Lists, Char Lists, Tuples, Keyword Lists, Maps
      • 4.6 Functions
      • 4.7 Type Identification
      • 4.8 Exercises
    • Chapter 5 Elixir Basics(3)
      • 5.1 Variables and Pattern Matching
      • 5.2 Functions, Modules, and Macros
      • 5.3 Exercises
    • Chapter 6 Elixir Basics(4)
      • 6.1 Operators
      • 6.2 File System and input/output
      • 6.3 if macro, for macro, Range structure
      • 6.4 Exercises
  • Part 2
    • Chapter 7 Modules
      • 7.1 Defining a Modules
      • 7.2 Module Aliases
      • 7.3 Module Attributes
      • 7.4 Exercises
    • Chapter 8 Functions(1)
      • 8.1 Unnamed Functions
      • 8.2 Named Functions
      • 8.3 Functions and Keyword Lists
      • 8.4 Exercises
    • Chapter 9 Functions(2)
      • 9.1 Classification of Function Calls and Function Visibility
      • 9.2 Capture Operators
      • 9.3 import Directives
      • 9.4 Exercises
    • Chapter 10 Macro Basics and Conditional Branch
      • 10.1 Introduction to Macros
      • 10.2 if Macro
      • 10.3 cond Macro
      • 10.4 Exercises
  • Part 3
    • Chapter 11 Atoms
      • 11.1 What are atoms?
      • 11.2 Functions that Work with Atoms
      • 11.3 The Role of Atoms
      • 11.4 Exercises
    • Chapter 12 Lists
      • 12.1 Creating a List
      • 12.2 Using Lists
      • 12.3 Converting a List into Another List
      • 12.4 Data Structure of Lists
      • 12.5 Head, Tail, and Cons Operators
      • 12.6 Exercises
    • Chapter 13 Tuples and Keyword Lists
      • 13.1 Tuples
      • 13.2 Keyword Lists
      • 13.3 Exercises
    • Chapter 14 Maps
      • 14.1 Map Basics
      • 14.2 Using Maps
      • 14.3 Basic Operations on Maps
      • 14.4 Map Data Structures
      • 14.5 Exercises
    • Chapter 15 Bitstrings, Binaries, Strings, and Char Lists
      • 15.1 Bitstrings
      • 15.2 Binaries
      • 15.3 Strings(1)
      • 15.4 Strings(2)
      • 15.5 Functions for Strings
      • 15.6 Data Structures of Binaries
      • 15.7 Char Lists
      • 15.8 Exercises
  • Part 4
    • Chapter 16 Pattern Matching
      • 16.1 What is Pattern Matching?
      • 16.2 Pattern Matching for Lists
      • 16.3 Pattern Matching for Tuples
      • 16.4 Pattern Matching for Maps
      • 16.5 Pattern Matching for Binary Strings
      • 16.6 Pattern Matching for Bitstrings
      • 16.7 Pin Operators
      • 16.8 Pattern Matching for Function Arguments
      • 16.9 Exercises
    • Chapter 17 Enum Module
      • 17.1 Enumerable
      • 17.2 Functions that Convert an Enumerable to a List with the Same Number of Elements
      • 17.3 Functions that Convert all or part of an Enumerable to a List and Return it
      • 17.4 Functions that Return a Single Element Belonging to an Enumerable
      • 17.5 Functions that Return True/False Values
      • 17.6 Other Functions
      • 17.7 Exercises
    • Chapter 18 for Macros
      • 18.1 Basics of for Macros
      • 18.2 Using Generators and Filters
      • 18.3 The :into Option
      • 18.4 Bitstring Generators
      • 18.5 Exercises
    • Chapter 19 Case Macros and With Macros
      • 19.1 Basic Usage of the Case Macro
      • 19.2 Case Macro Usage Examples
      • 19.3 with Macros
      • 19.4 Exercises
    • Chapter 20 Functions with Multiple Clauses
      • 20.1 Creating an Unnamed Function with Multiple Clauses
      • 20.2 Named Functions with Multiple Clauses
      • 20.3 Guarding Named Functions Against Clauses
      • 20.4 Exercises
    • Chapter 21 reduce Functions
      • 21.1 Accumulator
      • 21.2 Example Usage of the reduce Function
      • 21.3 Exercises
    • Chapter 22 Error Handling
      • 22.1 Exceptions
      • 22.2 Throws
      • 22.3 Exercises
  • Part 5
    • Chapter 23 ExUnit
      • 23.1 Introduction to ExUnit
      • 23.2 Writing Tests
      • 23.3 Exercises
    • Chapter 24 Mix
      • 24.1 The Mix Project
      • 24.2 The First Implementation
      • 24.3 Extensions
      • 24.4 Exercises
    • Chapter 25 Agents and GenServer
      • 25.1 Agents
      • 25.2 GenServer
      • 25.3 Exercises
    • Chapter 26 Tic-tac-toe(1)
      • 26.1 Specification of the application create in the development exercise
      • 26.2 Creating the Application Framework
      • 26.3 Agents to Manage the Game State
      • 26.4 Console
      • 26.5 Defining the play task
    • Chapter 27 Tic-tac-toe(2)
      • 27.1 Displaying the game state
      • 27.2 Managing turn
      • 27.3 Updating the Grid
      • 27.4 Refresh the Screen
    • Chapter 28 Tic-tac-toe(3)
      • 28.1 Judging the Result
      • 28.2 Letting the Computer Choose a Move
  • Appendix A. Answers to the Exercises
To List of topics