1
1

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 5 years have passed since last update.

Elixir 基本データ型

Last updated at Posted at 2014-09-24

Elixir 基本データ型

概要

Elixir の基本データ型について

Integer, Float

http://elixir-lang.org/docs/stable/elixir/Integer.html
http://elixir-lang.org/docs/stable/elixir/Float.html

iex(1)> 1
1
iex(2)> 1.0
1.00000000000000000000e+00
iex(3)> 1+1.1
2.10000000000000008882e+00
iex(4)> 2*3
6
iex(5)> 4/2
2.00000000000000000000e+00
iex(6)> to_char_list(97)
'97'

Boolean

iex(1)> true
true
iex(2)> false
false
iex(3)> (1==1)
true
iex(4)> (1==2)
false

Atoms

Atoms は他言語で言うところの Symbol

iex(1)> :hoge
:hoge
iex(2)> :hoge==:hoge
true
iex(3)> :hoge==:hige
false
iex(4)> true == :true
true
iex(5)> false == :false
true
iex(6)> to_char_list(:hoge)
'hoge'

Strings

# ダブルクォートで文字列を囲む
iex(1)> "hoge"
"hoge"
# 変数展開
iex(2)> variable = "var-hoge"
"var-hoge"
# Ruby と同じ変数展開
iex(3)> "hoge #{variable}"
"hoge var-hoge"
# byte_size でバイト数を数える
iex(4)> byte_size("hoge")
4
iex(5)> byte_size("ほげ")
6
# String.length で文字数を数える
iex(6)> String.length("hoge")
4
iex(7)> String.length("ほげ")
2
# ? で codepoint の取得
iex(8)> ?A
65
iex(9)> ?a
97
iex(10)> ?あ
12354
# \x で16進数のコードポイントから文字列に変換
iex(11)> "\x{0041}\x{0061}"
"Aa"
# 空白除去
iex(12)> String.strip(" hOgE ")
"hOgE"
# 小文字変換
iex(13)> String.downcase("hOgE")
"hoge"
# 大文字変換
iex(14)> String.upcase("hOgE")
"HOGE"
# 先頭大文字、残りは小文字
iex(15)> String.capitalize("hOgE")
"Hoge"
# 文字列の繰り返し
iex(16)> String.duplicate("hoge", 1)
"hoge"
iex(17)> String.duplicate("hoge", 2)
"hogehoge"
# 初めの文字を取得
iex(18)> String.first("hoge")
"h"
iex(19)> String.first("ほげ")
"ほ"
# 最後の文字を取得
iex(20)> String.last("hoge")
"e"
iex(21)> String.last("ほげ")
"げ"
iex(22)> String.replace("hoge","o","i")
"hige"
# 文字列置換
iex(23)> String.replace("hogehoge","o","i")
"higehige"
iex(24)> String.replace("hogehoge","o","i", global: false)
"higehoge"
# 文字列の切り取り
iex(25)> String.slice("hoge", 1, 2)
"og"
# 文字列の分割(デフォルトはスペース)
iex(26)> String.split("hoge hige hage")
["hoge","hige","hage"]
iex(27)> String.split("hoge hige hage", ",")
["hoge hige hage"]
iex(28)> String.split("hoge,hige,hage", ",")
["hoge","hige","hage"]

Anonymous functions

無名関数

iex(1)> multiply = fn a, b -> a * b end
# Function<erl_eval.12.82930912>
iex(2)> multiply.(2,4)
8
iex(3)> multiply.(3,4)
12
iex(4)> (fn a, b -> a * b end).(2,4)
8
iex(5)> (fn a, b -> a * b end).(3,4)
12

(Linked) Lists

リスト

iex(1)> [1, "hoge", :hoge, fn a -> a*2 end]
[1,"hoge",:hoge,#Function<erl_eval.6.82930912>]
iex(2)> length [1, 2, 3]
# リスト同士の加算
iex(3)> [1,2,3]++[1,2]
[1,2,3,1,2]
# リスト同士の減算
iex(4)> [1,2,3]--[1,2]
[3]
iex(5)> list = ["hoge", "hage", "hige"]
["hoge","hage","hige"]
# 先頭要素の返却
iex(6)> hd list
"hoge"
# 先頭要素より後の要素の返却
iex(7)> tl list
["hage","hige"]

# 要素の削除
iex(8)> List.delete([1,2,3], 1)
[2,3]

# 繰り返し
iex(9)> List.duplicate([1,2,3], 1)
[[1,2,3]]
iex(10)> List.duplicate([1,2,3], 2)
[[1,2,3],[1,2,3]]

# 平坦化
iex(11)> List.flatten([[1,2,3],[1,2,3]])
[1,2,3,1,2,3]

# 畳み込み(左) : fold left
# Rubyだと
# [1,2,3,4].reduce(10) { |a, e|a*=e;a }
# にあたる処理
iex(12)> List.foldl([1,2,3,4], 10, fn (x, acc) -> x * acc end)
240

# 畳み込み(右) fold right
# Rubyだと
# [1,2,3,4].reduce(10) { |a, e|a*=e;a }
# にあたる処理
iex(12)> List.foldr([1], 0, fn (x, acc) -> x - acc end)
# 1 - 0
1
iex(13)> List.foldr([1, 2], 0, fn (x, acc) -> x - acc end)
# (0 - 2) - 1
-1
iex(14)> List.foldr([1,2,3], 0, fn (x, acc) -> x - acc end)
# (0 - 3) - 2 - 1
2
iex(15)> List.foldr([1,2,3,4], 0, fn (x, acc) -> x - acc end)
-2>
iex(16)> List.foldl([1,2,3,4], 0, fn (x, acc) -> x - acc end)
2

Tuples

タプル

iex(1)> tuple = {:v1, :v2, :v3}
{:v1,:v2,:v3}
iex(2)> tuple
{:v1,:v2,:v3}
iex(3)> tuple_size(tuple)
3
iex(4)> elem(tuple,1)
:v2
iex(5)> elem(tuple,2)
:v3

Lists or tuples?

List は linked list 構造。
Tuple は隣接メモリに配置される。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?