15. Part 3: Recap
We now come to the end of Part 3.
This part of Joy of Elixir was all about exploring the functions and other things that Elixir provides us.
In this part, we introduced built-in modules. These are collections of functions that help us work with certain data types in Elixir. We saw the String
, List
, Map
, Enum
and File
modules.
We can chain these functions together using the pipe operator, as we saw back in Chapter 10. Here's an example of us chaining together two functions from the String
module that we saw during that introduction:
iex> "hello pipe operator" |> String.upcase() |> String.reverse()
The pipe operator is one of Elixir's best features as it allows us to read the code from left-to-right, just as we would be reading this sentence.
We should now have a good understanding around the fundamental building blocks that Elixir provides us. We can work with strings, lists, maps and files now, and we know where we can find these functions too.
In Chapter 12 we looked at conditional statements as a way of deciding when to run a particular piece of code. We saw how we could pattern match on the function responses with case
, use cond
to check conditions and run code depending on that condition, and we used if
and unless
to perform similar checks to cond
. Finally, we wrapped up by covering with
, which runs code in the order we specify it in, and will only execute code if multiple conditions are met.
In Chapter 13 we saw where we could find additional functions, or information about functions. We saw that IEx provides a helper called h
that works like this:
iex> h Map.get/2
This then returns the documentation for the Map.get/2
function:
def get(map, key, default \\ nil)
Gets the value for a specific key in map.
If key is present in map with value value, then value is returned. Otherwise,
default is returned (which is nil unless specified otherwise).
## Examples
iex> Map.get(%{}, :a)
nil
iex> Map.get(%{a: 1}, :a)
1
iex> Map.get(%{a: 1}, :b)
nil
iex> Map.get(%{a: 1}, :b, 3)
3
In this same chapter, we learned about navigating through IEx prompts using the arrow keys on the keyboard, as well as the v
helper function in IEx that gives us the last value, or we could use v(3)
to get the value from the third line of IEx.
Finally in Chapter 13, we saw that there are two great documentation sites, the official documentation and Elixir School. You can use both of these resources to supplement your learning when you're reading this book.
In Chapter 14 we got to see how to create our own modules and structures (or "structs" for short). Creating modules allows us to group together similar functions, just like what is done for the String
and Map
built-in functions for Elixir.
In the final part of this book, we're going to be looking into how we can build an Elixir project just like the pros do by using a tool called Mix. We'll be looking at how we can start a new Mix project, bring in other people's code into our project, and then finally look at how we can ensure the code that we write is always working through the process of writing automated tests for that code.