14
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ElixirAdvent Calendar 2022

Day 7

今日のElixirSchoolメモ7「パイプ演算子」

Posted at

Elixir Schoolの学習メモです。

|>(パイプ)演算子

people = DB.find_customers
orders = Orders.for_customers(people)
tax = sales_tax(orders, 2018)
filing = peaple_filing(tax)

|>演算子は、左の項の式の結果をとって、右の関数呼び出しの第一パラメータとして渡す。
パイプ演算子を使うと下記のように書くことができる。

filing = DB.find_customers
            |> Orders.for_customers
            |> sales_tax(2018)
            |> prepare_filing

パイプラインの中では、関数のパラメータは常にカッコで囲まなければならない。

iex(1)> (1..10) |> Enum.map(&(&1*&1)) |> Enum.filter(&(&1 < 40))
[1, 4, 9, 16, 25, 36]

関数のアリティが1より多い場合にカッコを使わない場合、warningが出力される。

iex(3)> "elixir" |> String.ends_with? "ixir"
warning: parentheses are required when piping into a function call. For example:

    foo 1 |> bar 2 |> baz 3

is ambiguous and should be written as

    foo(1) |> bar(2) |> baz(3)

Ambiguous pipe found at:
  iex:3:10

true
14
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?