もやもやエンジニア

IT系のネタで思ったことや技術系のネタを備忘録的に綴っていきます。フロント率高め。

Elixir 入門 その2 - 演算子

++ --

  • 2つのリストを結合したり差分をとったり
iex> [1,2,3] ++ [4,5,6]
[1,2,3,4,5,6]
iex> [1,2,3] -- [2]
[1,3]
iex> [1,2,2,3] -- [2]
[1,2,3]
iex> [1,3,2] -- [2,3]
[1]

<>

  • 文字列のconcat
  • 文字列同士でないと怒られる
iex> "foo" <> "bar"
"foobar"

iex> "foo" <> 1
argument error

論理演算子 ( and or not )

  • 条件式を繋げる
  • not は直後の条件の否定。
iex> true and true
true

iex> true and false
false

iex> true and not false
true

iex> 1 and true
argument error

iex> 1 == 1 and true
true

論理演算子 ( || && ! )

  • and or not と同じ
  • こちらはfalseとnil以外はtrueと判定する差異がある
iex> true && true
true

iex> true && false
false

iex> true && ! false
true

iex> 1 && true
true

iex> 1 == 1 && true
true

比較演算子 (==, !=, ===, !==, <=, >=, <, > )

  • この辺は他の言語と変わらない
  • == と === は厳密に型を見るかの違い
  • 型の違う値も比較できる
    • 型によって強弱が決まっている
    • number < atom < reference < functions < port < pid < tuple < maps < list < bitstring
iex> 1 == 1.0
true

iex 1 === 1.0
false

iex> 1 < :atom
true