実験
Q.これは何を表すか
iex(1)> 1..10|> Enum.map(& &1 * 2)
A.1から10の各要素に対して2倍する
練習問題
Q.各要素に対して、2倍して+1
A.iex(2)> 1..10|> Enum.map(& &1 * 2 + 1)
[3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
or
A.iex(2)> 1..10|> Enum.map(& &1 * 2 + 1) |> Enum.map(& &1 + 1)
[3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
総和を求める_Enum.sum()
iex(3)> 1..10|> Enum.sum()
55
sum_help
iex(4)> h Enum.sum
def sum(enumerable)
@spec sum(t()) :: number()
Returns the sum of all elements.
Raises ArithmeticError if enumerable contains a non-numeric value.
## Examples
iex> Enum.sum([1, 2, 3])
6
reduce関数の動き
iex(5)> 1..4 |> Enum.reduce(fn x, acc -> [acc, x] end)
[[[1, 2], 3], 4]
1~10を3倍して総和
iex(6)> 1..10 |> Enum.map(& &1 * 3) |> Enum.sum()
165
or
iex(8)> 1..10 |> Enum.map(& &1 * 3) |> Enum.reduce(fn x, acc -> x + acc end)
165
同じ意味
iex(10)> Enum.sum([1,2,3])
6
iex(11)> [1,2,3] |> Enum.sum()
6
Q.リスト[1,2,3]の各要素に1を加えてから3倍したあと、総和を求める。
iex(12)> 1..3 |> Enum.map(& &1 + 1) |> Enum.map(& &1 * 3) |> Enum.reduce(fn x, acc -> x + acc end)
27
Enum.zip_&_IO.inspectの組みあわせ
iex(1)> Enum.zip([1,2,3],[4,5,6]) |> Enum.map(& IO.inspect &1)
{1, 4}
{2, 5}
{3, 6}
[{1, 4}, {2, 5}, {3, 6}]
Tuple.to_listでタプルをリスト化する。
iex(3)> Enum.zip([[1,2,3],[4,5,6],[7,8,9]]) |> Enum.map(& Tuple.to_list(&1)) |> Enum.map(& Enum.reduce(&1, 1, fn x, acc -> x*acc end))
[28, 80, 162]
[1,2,3],[4,5,6]を要素ごとに積和
iex(4)> Enum.zip([[1,2,3],[4,5,6]]) |> Enum.map(& Tuple.to_list(&1)) |> Enum.map(& Enum.reduce(&1, 1, fn x, acc -> x * acc end)) |> Enum.sum()
32
決められた長さに分割&結合する関数.
iex(9)> 1..23 |> Enum.chunk_every(2)
[
[1, 2],
[3, 4],
[5, 6],
'\a\b',
'\t\n',
'\v\f',
[13, 14],
[15, 16],
[17, 18],
[19, 20],
[21, 22],
[23]
]
iex(10)> 1..23 |> Enum.chunk_every(2) |> List.flatten()
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
1から23までのリストを2つずつ区切り、和を求める
iex(11)> 1..23 |> Enum.chunk_every(2) |> Enum.map(& Enum.sum(&1))
[3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 23]
Matrixの計算
matrix.ex
defmodule Matrix do
@moduledoc """
Documentation for Matrix.
"""
@doc """
## Examples
iex> Matrix.add_org([[1,2],[3,4]],[[5,6],[7,8]])
[[6,8],[10,12]]
"""
@doc """
## Examples
iex> Matrix.add(1,2)
3
iex> Matrix.add([1,2])
3
iex> Matrix.add([1,2],[3,4])
[4,6]
iex> Matrix.add([[1,2],[3,4]],[[5,6],[7,8]])
a # sample
"""
@doc """
## Examples
iex> point_wise_product(1,2)
2
iex> point_wise_product([1,2])
2
iex> point_wise_product([[1,2],[3,4]])
[3,8]
"""
def point_wise_product(a, b) do
point_wise_product([a, b])
end
#def point_wise_product(l) do
# Enum.reduce(l, fn (x, acc) -> x * acc end)
#end
def point_wise_product(l) when is_list(l) and is_number(hd(l)) do
Enum.reduce(l, fn (x, acc) -> x * acc end)
end
def point_wise_product(l) when is_list(l) do
Enum.zip(l)
|> Enum.map(& Tuple.to_list(&1))
|> Enum.map(& point_wise_product(&1))
end
def add(a, b) do
add([a, b])
end
# def add([a, b]) when is_list(a) and is_list(b) do
# Enum.zip(a, b)
# |> Enum.map(& Tuple.to_list(&1))
# |> Enum.map(& add(&1))
# end
# def add(l) do
# Enum.sum(l)
# end
def add(l) when is_list(l) and is_number(hd(l)) do
Enum.sum(l)
end
def add(l) when is_list(l) do
Enum.zip(l)
|> Enum.map(& Tuple.to_list(&1))
|> Enum.map(& add(&1))
end
def add_org(a, b) do
Enum.zip(a, b)
|> Enum.map(& Tuple.to_list(&1))
|> Enum.map(& IO.inspect(&1))
|> Enum.map(& Enum.zip(&1))
|> Enum.map(& IO.inspect(&1))
|> Enum.map(fn x -> x |> Enum.map(& Tuple.to_list(&1)) end)
|> Enum.map(fn x -> x |> Enum.map(& IO.inspect(&1)) end)
|> Enum.map(fn x -> x |> Enum.map(& Enum.sum(&1)) end)
|> Enum.map(fn x -> x |> Enum.map(& IO.inspect(&1)) end)
end
end
tips
今自分がどこの変数を扱っているかわからない場合、
IO.inspect(&1)
を使って表示する。
次回までの課題
パイプライン演算子に慣れる
mix test
ができるようにする