Skip to content

Instantly share code, notes, and snippets.

@bennycwong
Last active July 8, 2016 21:53
Show Gist options
  • Select an option

  • Save bennycwong/6c985365c07a4f8e3c90af4ce0dca9e3 to your computer and use it in GitHub Desktop.

Select an option

Save bennycwong/6c985365c07a4f8e3c90af4ce0dca9e3 to your computer and use it in GitHub Desktop.
Elixir Example
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)
@Morkrom
Copy link

Morkrom commented Jul 8, 2016

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)

@bennycwong
Copy link
Author

thanks @Morkrom. Updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment