Category: ‘Elixir’

Phoenix select と radio_button

2018年5月21日 Posted by PURGE

■ selectタグの場合

  <div class="form-group">
    <%= label f, :gender, class: "control-label" %>
    <%= select f, :gender_cd, ["男性": "1", "女性": "2", "その他": "0"], class: "control-label" %>
    <%= error_tag f, :team %>
  </div>

■ radio button タグの場合

  <div class="form-group">
    <%= label f, :gender, class: "control-label" %>
    <%= radio_button f, :gender_cd, "1", checked: true, class: "control-label" %>男性
    <%= radio_button f, :gender_cd, "2", class: "control-label" %>女性
    <%= radio_button f, :gender_cd, "0", class: "control-label" %>その他
    <%= error_tag f, :team %>
  </div>

Phoenix date_select フォーマット

2018年5月21日 Posted by PURGE

  <div class="form-group">
    <%= label f, :birthday, class: "control-label" %>
        <%= date_select f, :birthday, default: :calendar.local_time(), builder: fn b -> %>
          <%= b.(:month, [
                  options: [
                    {gettext("1月"), "1"},
                    {gettext("2月"), "2"},
                    {gettext("3月"), "3"},
                    {gettext("4月"), "4"},
                    {gettext("5月"), "5"},
                    {gettext("6月"), "6"},
                    {gettext("7月"), "7"},
                    {gettext("8月"), "8"},
                    {gettext("9月"), "9"},
                    {gettext("10月"), "10"},
                    {gettext("11月"), "11"},
                    {gettext("12月"), "12"},
                  ]
                ]) %>
        <% end %>
    <%= error_tag f, :birthday %>
  </div>

Phoenix date_select 期間とデフォルト(本日日付)の設定方法

2018年5月21日 Posted by PURGE

  <div class="form-group">
    <%= label f, :birthday, class: "control-label" %>
    <%= date_select f, :birthday, default: :calendar.local_time(), year: [options: 1950..2030], class: "form-control" %>
    <%= error_tag f, :birthday %>
  </div>

Phoenix Framwork select tag

2018年5月21日 Posted by PURGE

■member_controller.ex

def new(conn, _params) do
  changeset = Portal.change_member(%Member{})
  teams = Portal.list_teams()
  render(conn, "new.html", changeset: changeset, teams: teams)
end

def edit(conn, %{"id" => id}) do
  member = Portal.get_member!(id)
  teams = Portal.list_teams()
  changeset = Portal.change_member(member)

  render(conn, "edit.html", member: member, changeset: changeset, teams: teams)
end

■new.html.eex / edit.html.eex

<h2>New Member</h2>

<%= render "form.html", Map.put(assigns, :action, member_path(@conn, :create)) %>

<h2>Edit Member</h2>

<%= render "form.html", Map.put(assigns, :action, member_path(@conn, :update, @member)) %>

■form.html.eex

<div class="form-group">
  <%= label f, :teams, class: "control-label" %>
  <%= select f, :team_id, Enum.map(@teams, &{&1.team_name, &1.id}) ,  class: "control-label" %>
  <%= error_tag f, :team %>
</div>

<div class="form-group">
  <%= label f, :gender, class: "control-label" %>
  <%= select f, :gender_id, ["男性": "1", "女性": "2", "その他": "0"], class: "control-label" %>
  <%= error_tag f, :team %>
</div>

Elixir 演算子

2018年3月23日 Posted by PURGE

基本的な演算子。
= は、パターンマッチ。 == は、弱い比較。 === == は、強い比較。

iex(1)> 1 = 1
1
iex(2)> 1 == 1 
true
iex(3)> 1 === 1
true
iex(1)> 1 = 1.0
** (MatchError) no match of right hand side value: 1.0
iex(2)> 1 == 1.0
true
iex(3)> 1 === 1.0
false

nil の場合の値代入

2018年2月16日 Posted by PURGE

Ecto でDBから値を返す時、nilの場合がある。
この場合に、nilをreplaceしたい場合がある。その記述方法

result = %{total: nil}
total = result |> Map.get("total") || 0

この記述で、nil の場合には、0が代入される。

Elixir モジュール組み立て方

2017年10月21日 Posted by PURGE


# Elixirモジュールの組み立て方
defmodule Return do
    # 外部I/F定義
    def return(list) do
        #Listで引数を構成する
        _return(list, 0)
    end

    # Retrun値定義
    defp _return([], result), do: result

    # Logic定義 
    defp _return([head|tail], result) do
        #head値を処理
        _return(tail, (head + result))
    end
end

Elixir 自作のListのMax関数

2017年7月11日 Posted by PURGE

defmodule ListMax do
    def max(list), do: _max(list, 0) 
    defp _max([], max), do: max 
    defp _max([head | tail], max) do
        if head > max do
            result = head
        else
            result = max
        end
        _max(tail, result)
    end
end
iex(82)> IO.puts ListMax.max([471,193,433,263,102])
471

Phoenix linkタグ

2017年7月6日 Posted by PURGE

Railsで記述可能なことは、Phoenixでも可能だと信じて、やはり可能だった件。
linkタグの文字に、スタイルを適用したいという場合の対応。

    <%= link "LOGO", to: page_path(@conn, :index), class: "logo" %>

    <%= link to: page_path(@conn, :index), class: "logo" do %>
      <span class="logo"><b>LOGO</b></span>
    <% end %>

Elixir と Ruby のsumの比較

2017年7月5日 Posted by PURGE

Elixir と Ruby のsumの比較。
まだ、関数型プログラミングの記述、特に再帰には慣れていない。

Ruby

sum.rb

def sum(n)
  result = 0
  (1..n).each do |n|
    result = result + n
  end
  result
end

n = 5
puts sum(n)

# => 15

Elixir

sum.exs

defmodule Sum do
  def sum(0), do: 0
  def sum(n), do: n + sum(n - 1)
end

n = 5
IO.puts(Sum.sum(n))

# => 15

または、わかりやすく書くと

defmodule Sum do
  def sum(0) do
    0
  end
  def sum(n) do
    n + sum(n - 1)
  end
end

n = 5
IO.puts(Sum.sum(n))

# => 15