# Elixir

**URL:** <https://metaruby.com/t/elixir/192>\
**Category:** General Programming\
**Created:** [June 26, 2015, 12:50pm UTC](https://metaruby.com/t/elixir/192 "2015-06-26T12:50:37Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![RedFred7](https://metaruby.com/user_avatar/metaruby.com/redfred7/32/838_2.png) [@RedFred7](https://metaruby.com/u/RedFred7)\
**Post date:** [July 2, 2015, 10:24pm UTC](https://metaruby.com/t/elixir/192/22 "2015-07-02T22:24:37Z")

</div>

Sounds good AJ. I already have the Introducing Elixir book, that seems as good a place to start as any.

> [@AstonJ](#):
>
> At this stage, I just want to see if I can get my heard around FP… I’m not the brightest spark you see!

Join the club 😄

> [@AstonJ](#):
>
> Maybe we can use this thread to see how we’re getting on?

Sounds like a plan 👍

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [July 2, 2015, 10:26pm UTC](https://metaruby.com/t/elixir/192/23 "2015-07-02T22:26:12Z")

</div>

Awesome - what’s it like? How far you into it? I just bought it btw, apparently Elixir in Action is quite advanced.

This looks like a good video: [chrismccord](http://www.chrismccord.com/blog/2014/05/27/all-aboard-the-elixir-express)

---

<div class="post-metadata">

**Author:** ![RedFred7](https://metaruby.com/user_avatar/metaruby.com/redfred7/32/838_2.png) [@RedFred7](https://metaruby.com/u/RedFred7)\
**Post date:** [July 3, 2015, 7:15am UTC](https://metaruby.com/t/elixir/192/24 "2015-07-03T07:15:27Z")

</div>

> [@AstonJ](#):
>
> Awesome - what’s it like?

I just started reading it. Will get into it over the weekend and post some feedback next week.

That video quite sold it to me, too.

---

<div class="post-metadata">

**Author:** ![Ohm](https://metaruby.com/user_avatar/metaruby.com/ohm/32/828_2.png) [@Ohm](https://metaruby.com/u/Ohm)\
**Post date:** [July 3, 2015, 11:58am UTC](https://metaruby.com/t/elixir/192/25 "2015-07-03T11:58:03Z")

</div>

I find that I get around a language enough for a starters with [Project Euler](https://projecteuler.net/).

I’ve solved a couple of the firsts problems with Elixir. I’ll give the first two away, just so you guys can see how it works:

[Problem #1](https://projecteuler.net/problem=1)

> If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
> 
> Find the sum of all the multiples of 3 or 5 below 1000.

```elixir
defmodule Problem1 do
  def result do
    1..1000
    |> Enum.filter(&multiples_of_three_of_five/1)
    |> Enum.reduce(0, &+/2)
  end

  defp multiples_of_three_of_five(n) do
    rem(n, 3) == 0 || rem(n, 5) == 0
  end
end

IO.puts Problem1.result

```

[Problem #2](https://projecteuler.net/problem=2)

> Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
> 
> 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
> 
> By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

```elixir
defmodule Problem2 do
  import Integer, only: [is_even: 1]

  def result do
    Stream.iterate(0, &(&1+1))
    |> Stream.map(&fib/1)
    |> Stream.take_while(&(&1 < 4000000))
    |> Enum.filter(&is_even/1)
    |> Enum.sum
  end

  defp fib(0), do: 1
  defp fib(1), do: 2
  defp fib(n), do: fib(n-1) + fib(n-2)
end

IO.puts Problem2.result

```

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [July 3, 2015, 9:45pm UTC](https://metaruby.com/t/elixir/192/26 "2015-07-03T21:45:21Z")

</div>

Thanks Ohm - when I looked at that earlier I thought WTF!!?? Haha.

Just read the first two chapters of introducing Elixir and it’s making more sense 🙂

_/Btw - I merged both threads. Should make it easier keep track of stuff/_

---

<div class="post-metadata">

**Author:** ![Ohm](https://metaruby.com/user_avatar/metaruby.com/ohm/32/828_2.png) [@Ohm](https://metaruby.com/u/Ohm)\
**Post date:** [July 3, 2015, 9:57pm UTC](https://metaruby.com/t/elixir/192/27 "2015-07-03T21:57:08Z")

</div>

I really love the `|>` (pipe) construct. ❤

For those not in the know, the pipe will pass it’s left argument as the first parameter in the right argument, that is

```elixir
1..10 |> Enum.sum

```

is the same as

```elixir
Enum.sum(1..10)

```

This is awesome when you are doing multiple function calls, like in the above example, where I pass the result of one function directly into the next. With functional programming, you’ll strive to create small functions with one purpose and then reuse that small function.

* * *

Another awesome thing is the pattern matching in the parameters. The `fib` function above shows this nicely. Instead of a big ugly `if`-statement, we just say "if my first parameter is `0`, return `1`, if it’s `1` return `2`, else return `fib(n-1) + fib(n-2)`.

---

<div class="post-metadata">

**Author:** ![Ohm](https://metaruby.com/user_avatar/metaruby.com/ohm/32/828_2.png) [@Ohm](https://metaruby.com/u/Ohm)\
**Post date:** [July 3, 2015, 9:57pm UTC](https://metaruby.com/t/elixir/192/28 "2015-07-03T21:57:54Z")

</div>

Add guards to that and the fact that you can do this with even more complex stuff 💓

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [July 3, 2015, 10:27pm UTC](https://metaruby.com/t/elixir/192/29 "2015-07-03T22:27:30Z")

</div>

Haven’t got to guards yet 😜

I thought pipes were cool too 😄

However, not sure of default values for parameters:

```auto
def some_function(one, two // 555) do
end

```

Where `555` is the default value for `two`. Any idea why they went for that? I guess cos that’s how Erlang does it?

And how would you add a default value for `one` too?

---

<div class="post-metadata">

**Author:** ![Ohm](https://metaruby.com/user_avatar/metaruby.com/ohm/32/828_2.png) [@Ohm](https://metaruby.com/u/Ohm)\
**Post date:** [July 4, 2015, 7:40am UTC](https://metaruby.com/t/elixir/192/30 "2015-07-04T07:40:17Z")

</div>

For one, it has to be backslashes (`\\`)

```elixir
defmodule Test do
  def test(one \\ 555, two \\ 666) do
    IO.puts one
    IO.puts two
    IO.puts
  end
end

Test.test()
Test.test(111)
Test.test(222, 333)

```

will print:

```auto
555
666

111
666

222
333

```

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [July 4, 2015, 9:13am UTC](https://metaruby.com/t/elixir/192/31 "2015-07-04T09:13:19Z")

</div>

Any idea why tho, over this:

```auto
defmodule Test do
  def test(one=555, two=666) do
    IO.puts one
    IO.puts two
    IO.puts
  end
end

```

Which do you prefer?

---

<div class="post-metadata">

**Author:** ![Ohm](https://metaruby.com/user_avatar/metaruby.com/ohm/32/828_2.png) [@Ohm](https://metaruby.com/u/Ohm)\
**Post date:** [July 4, 2015, 12:58pm UTC](https://metaruby.com/t/elixir/192/32 "2015-07-04T12:58:55Z")

</div>

I don’t know, but I would guess that it’s because in Elixir (and Erlang) the `=` (equal sign) doesn’t mean “this is set to that”, but rather “this must match that”. So your example defines a method called `test`, which takes only two arguments, namely `555` and `666`.

```elixir
defmodule Test do
  def test(one=555, two=666) do
    IO.puts one
    IO.puts two
    IO.puts ""
  end
end

Test.test(555, 666)

```

prints

```auto
555
666

```

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [July 4, 2015, 2:21pm UTC](https://metaruby.com/t/elixir/192/33 "2015-07-04T14:21:13Z")

</div>

Good point - I forgot that haha.

I don’t mind the // just wondered what the thinking was behind it.

---

<div class="post-metadata">

**Author:** ![Ohm](https://metaruby.com/user_avatar/metaruby.com/ohm/32/828_2.png) [@Ohm](https://metaruby.com/u/Ohm)\
**Post date:** [July 4, 2015, 2:49pm UTC](https://metaruby.com/t/elixir/192/34 "2015-07-04T14:49:58Z")

</div>

> [@AstonJ](#):
>
> I don’t mind the // […]

^^ still the other way around 😏

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [July 4, 2015, 4:03pm UTC](https://metaruby.com/t/elixir/192/35 "2015-07-04T16:03:14Z")

</div>

Whoops! I can see that happening quite often 😬

Wonder why they didn’t go for something like

```auto
  def test(one/ 555, two/666) do
    IO.puts one
    IO.puts two
    IO.puts
  end

```

Where the slash directly after a param indicates it has a default value.

I guess they’ve thought about this tho and have the bigger picture in mind (whereas as a nube, I don’t).

**Edit** : To answer my own question, I think it is because of arity. Where `something/1` to Elixir is the name of a function and `something/2` is actually considered a different function.

Also, every time I see `iex` I think of internet explorer 😝 I reckon they could have called it `eli’ instead - Eli the interactive elixir 😁

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [July 4, 2015, 9:59pm UTC](https://metaruby.com/t/elixir/192/36 "2015-07-04T21:59:05Z")

</div>

This is a good video by Prag Dave, but I don’t recommend watching it until after you’ve at least read through the basics of Elixir - the first time I watched it it actually put me off Elixir as I was like omgwtf!

[![](https://metaruby.com/uploads/default/original/2X/4/4031d8cee90197d8db53a6d95cc1f701192ea11b.jpeg "LoneStarRuby Conf 2013 - Elixir: Power of Erlang, Joy of Ruby by Dave Thomas") ](https://www.youtube.com/watch?v=KQwEmdOH-GM)

Also, arity in Elixir sounds really neat:

> [@](#):
>
> Note: Functions in Elixir are identified by name and by number of arguments (i.e. arity). Therefore, is\_boolean/1 identifies a function named is\_boolean that takes 1 argument. is\_boolean/2 identifies a different (nonexistent) function with the same name but different arity.

---

<div class="post-metadata">

**Author:** ![Ohm](https://metaruby.com/user_avatar/metaruby.com/ohm/32/828_2.png) [@Ohm](https://metaruby.com/u/Ohm)\
**Post date:** [July 5, 2015, 8:29am UTC](https://metaruby.com/t/elixir/192/37 "2015-07-05T08:29:42Z")

</div>

And remember that some of those functions can be private, so you can have a public `is_boolean/1`, but a private `is_boolean/2`. You can use this to write nice tail-recursive functions, like this length function:

```elixir
defmodule Sequence do
  def len([]), do: 0
  def len(list), do: len(list, 0)
  defp len([], i), do: i
  defp len([_|tail], i), do: len(tail, i+1)
end

```

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [July 5, 2015, 9:12am UTC](https://metaruby.com/t/elixir/192/38 "2015-07-05T09:12:01Z")

</div>

I kinda know what’s going on there @Ohm, but could you explain each line for us please?

---

<div class="post-metadata">

**Author:** ![Ohm](https://metaruby.com/user_avatar/metaruby.com/ohm/32/828_2.png) [@Ohm](https://metaruby.com/u/Ohm)\
**Post date:** [July 5, 2015, 11:52am UTC](https://metaruby.com/t/elixir/192/39 "2015-07-05T11:52:11Z")

</div>

Sure. In this example:

```elixir
defmodule Sequence do
  def len([]), do: 0
  def len(list), do: len(list, 0)
  defp len([], acc), do: acc
  defp len([_|tail], acc), do: len(tail, acc + 1)
end

```

we define a module, `Sequence`, with two functions, namely `len/1` and `len/2`. `len/1` is public, whilst `len/2` is private (that’s what the `defp` is for).

Users of our function can only call `len/1` from the outside, doing something like this:

```elixir
Sequence.len [1, 2, 3]

```

Underneath we do some magic to make this length function work with tail-recursion.

First we do some pattern matching on the empty list, because the empty list has length `0`. (Note, this can actually be removed, but I like it to tell the story)

If the list is not the empty one, we call our own private function, `len/2`. `len/2` takes the list and an accumulator. If the list is empty, we can just return our accumulator as the result. If it’s not, then it must be a head and a tail. The `[_|tail]` in the pattern means: "A list with something I don’t care about as head and a tail, which is bound to `tail`". In order to find the length, we remove the head and add one to our accumulator, and then continue finding the length of the tail.

* * *

So if we run with our example from before:

```elixir
Sequence.len [1, 2, 3]

```

We actually get this:

```auto
len([1,2,3])
-> len([1,2,3], 0)
-> len([2,3], 1)
-> len([3], 2)
-> len([], 3)
-> 3

```

* * *

We can remove the first pattern match, like so:

```elixir
defmodule Sequence do
  def len(list), do: len(list, 0)
  defp len([], acc), do: acc
  defp len([_|tail], acc), do: len(tail, acc + 1)
end

```

because now we will always hit the public function, but as `list` is the empty list, we will match the first pattern and just return our initial accumulator, which is `0`.

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [July 5, 2015, 12:17pm UTC](https://metaruby.com/t/elixir/192/40 "2015-07-05T12:17:55Z")

</div>

Nice one, thanks @ohm 👍

How long did it take you to learn Elixir or ‘get’ the concepts of it?

---

<div class="post-metadata">

**Author:** ![Ohm](https://metaruby.com/user_avatar/metaruby.com/ohm/32/828_2.png) [@Ohm](https://metaruby.com/u/Ohm)\
**Post date:** [July 5, 2015, 12:45pm UTC](https://metaruby.com/t/elixir/192/41 "2015-07-05T12:45:25Z")

</div>

I have a masters degree in computer science. We used SML for learning about functional programming. Elixir was easy going from Ruby. 😏

I started to look at Elixir about half a year ago. I won’t say that I am a master in Elixir, but I get the concepts.

[Previous page](https://metaruby.com/t/elixir/192.md?page=1)

[Next page](https://metaruby.com/t/elixir/192.md?page=3)
