Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Elixir Functions As Arguments

More than 5 years have passed since last update.

Elixir Functions As Arguments

概要

Elixir の Function を引数で渡します。

サンプル

2つの数値を合計して、基数を指定して返却する関数を返却する
関数を作成し、それを関数に渡して実行します。

sum_to_s_f = fn (base) -> 
  fn (a, b) ->
     Integer.to_string(a + b, base)
  end
end

defmodule Sum do
  def call_sum(fun, a, b) do
    fun.(a, b)
  end
end

sum_base10 = sum_to_s_f.(10)
IO.puts Sum.call_sum(sum_base10, 10, 6)
sum_base2 = sum_to_s_f.(2)
IO.puts Sum.call_sum(sum_base2, 10, 6)
sum_base8 = sum_to_s_f.(8)
IO.puts Sum.call_sum(sum_base8, 10, 6)
sum_base16 = sum_to_s_f.(16)
IO.puts Sum.call_sum(sum_base16, 10, 6)
sum_base36 = sum_to_s_f.(36)
IO.puts Sum.call_sum(sum_base36, 10, 6)
  • 出力
16
10000
20
10
G
tbpgr
Rubyを扱う人事(研修開発、エンジニア採用) Learning Designer。 tbpgr の読み方は(てぃーびー) 個人ブログでも色々と情報を垂れ流してます。 http://tbpgr.hatenablog.com/
http://tbpgr.hatenablog.com/
studist
「伝えることを、もっと簡単に」をミッションにビジュアルSOPマネジメントプラットフォームのBtoB SaaS「Teachme Biz」を開発・運営するスタートアップ
https://medium.com/studist-dev
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away