Last active
July 8, 2016 21:53
-
-
Save bennycwong/6c985365c07a4f8e3c90af4ce0dca9e3 to your computer and use it in GitHub Desktop.
Elixir Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Array do | |
| def sum([]) do | |
| 0 | |
| end | |
| def sum([ head | tail ]) do | |
| head + sum(tail) | |
| end | |
| def map([], _func) do | |
| [] | |
| end | |
| def map([ head | tail ], func) do | |
| [ func.(head) | map(tail, func) ] | |
| end | |
| end | |
| # Run this in iex: | |
| # iex -t array.ex | |
| # [1, 2, 3, 4, 5] |> Array.map(fn x -> x * x end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
iex(3)> [3, 5, 1, 5, 9] |> Array.map(fn x -> x * x)
** (SyntaxError) iex:3: unexpected token: ")". "fn" starting at line 3 is missing terminator "end"
......
[3, 5, 1, 5, 9] |> Array.map(fn x -> x * x end)