Learning Elixir
eBook - ePub

Learning Elixir

  1. 286 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Learning Elixir

About this book

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!

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Learning Elixir


Table of Contents

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

Learning Elixir

Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be ...

Table of contents

  1. Learning Elixir

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Learning Elixir by Kenny Ballou in PDF and/or ePUB format, as well as other popular books in Computer Science & Software Development. We have over one million books available in our catalogue for you to explore.