Skip to content

Instantly share code, notes, and snippets.

@wang-zhijun
Last active March 15, 2016 10:07
Show Gist options
  • Save wang-zhijun/8de12d63191f19be0011 to your computer and use it in GitHub Desktop.
Save wang-zhijun/8de12d63191f19be0011 to your computer and use it in GitHub Desktop.
Elixirの関数に無名関数を定義して、List.foldlに使う例
defmodule Hhfuns do
def a(o, p, q) do
fn(num, acc) ->
IO.puts "#{inspect o}, #{inspect p} #{inspect q}"
num + acc
end
end
end
x = Hhfuns.a("ooo", "ppp", "qqq")
IO.puts "x = #{inspect x}" ## x = #Function<0.111962536/2 in Hhfuns.a/3>
y = List.foldl([1,2,3], 0, x)
IO.puts "y = #{inspect y}" ## y = 6
## 実行結果
## $ elixir hhfuns.ex
## x = #Function<0.111962536/2 in Hhfuns.a/3>
## "ooo", "ppp" "qqq"
## "ooo", "ppp" "qqq"
## "ooo", "ppp" "qqq"
## y = 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment