Programming Elixir

Programming Elixir

Dave Thomas

Language: English

Pages: 620

ISBN: 168050200X

Format: PDF / Kindle (mobi) / ePub


As a developer, you’ve probably heard that functional programming techniques help manage the complexities of today’s real-world, concurrent systems. You’re also investigating designs that help you maximize uptime and manage security.

This book is your guide to Elixir, a modern, functional, and concurrent programming language. Because Elixir runs on the Erlang VM, and uses the underlying Erlang/OTP architecture, it benefits from almost 20 years of research into high performance, highly parallel, and seriously robust applications. Elixir brings a lot that’s new: a modern, Ruby-like, extendable syntax, compile and runtime evaluation, a hygienic macro system, and more.

But, just as importantly, Elixir brings a sense of enjoyment to parallel, functional programming. Your applications become fun to work with, and the language encourages you to experiment.

Part 1 covers the basics of writing sequential Elixir programs. We’ll look at the language, the tools, and the conventions.

Part 2 uses these skills to start writing concurrent code—applications that use all the cores on your machine, or all the machines on your network! And we do it both with and without OTP.

And Part 3 looks at the more advanced features of the language, from DSLs and code generation to extending the syntax.

By the end of this book, you’ll understand Elixir, and know how to apply it to solve your complex, modern problems.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

file. Your Turn Exercise: ModulesAndFunctions-4 Implement and run a function sum(n) that uses recursion to calculate the sum of the integers from 1 to n. You’ll need to write this function inside a module in a separate file. Then load up iex, compile that file, and try your function. Exercise: ModulesAndFunctions-5 Write a function gcd(x,y) that finds the greatest common divisor between two nonnegative integers. Algebraically, gcd(x,y) is x if y is zero; it’s gcd(y, rem(x,y))

end Let’s create a simple report: iex> report = %BugReport{owner: %Customer{name: "Dave", company: "Pragmatic"}, ...> details: "broken"} %BugReport{details: "broken", owner: %Customer{company: "Pragmatic", name: "Dave"}, severity: 1} The owner attribute of the report is itself a Customer struct. We can access nested fields using regular dot notation: iex> report.owner.company "Pragmatic" But now our customer complains the company name is incorrect—it should be

Collections—Enum and Stream * * * In this chapter, we’ll see The Enum module The Stream module The Collectable protocol Comprehensions Elixir comes with a number of types that act as collections. We’ve already seen lists and dictionaries. There are also things such as ranges, files, dictionaries, and even functions. And as we’ll discuss when we look at protocols, you can also define your own. Collections differ in their implementation. But they all share something: you can iterate

But if they don’t have what you need, you’ll have to add an external dependency. The next place to look is http://hex.pm, the Elixir/Erlang Package Manager. This is a (small, but growing) list of packages that integrate nicely with a mix-based project. If all else fails, Google and GitHub are your friends. Search for terms such as elixir http client or erlang distributed logger, and you’re likely to turn up the libraries you need. In our case, we need an HTTP client. We find that Elixir has

'Elixir.Sequence.SubSupervisor', 'Elixir.Sequence.Supervisor']}, {applications,[kernel,stdlib,elixir]}]}. This file contains an Erlang tuple that defines the app. Some of the information comes from the project and application section of mix.exs. Mix also automatically added a list of the names of all the compiled modules in our app (the .beam files) and a list of the apps our app depends on (kernel, stdlib, and elixir). That’s pretty smart. More on Application Parameters In the

Download sample

Download