読者です 読者をやめる 読者になる 読者になる

日常

ケ・セラ・セラ

Elixir に触れた

  • インストール

Mac

$ brew install elixir

Ubuntu

$ wget http://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb && sudo dpkg -i erlang-solutions_1.0_all.deb
$ sudo apt-get update
$ sudo apt-get install elixir

iex という、Ruby での irb なものがある。 抜けるときは Ctrl-C して a

ex(145)>
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution

少しだけ触った

$ iex
Erlang/OTP 17 [erts-6.4.1] [source] [64-bit] [smp:3:3] [async-threads:10] [kernel-poll:false]

Interactive Elixir (1.0.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> h(IO.puts/1)

                def puts(device \\ :erlang.group_leader(), item)

Writes the argument to the device, similar to write/2, but adds a newline at
the end. The argument is expected to be a chardata.

iex(2)> puts 3
** (RuntimeError) undefined function: puts/1

iex(2)> IO.puts 3
3
:ok
iex(3)> 1+2
3
iex(4)> 1+2.0
3.0
iex(5)> "aaa"
"aaa"
iex(6)> aaa
** (RuntimeError) undefined function: aaa/0

iex(6)> 1 + "aaa"
** (ArithmeticError) bad argument in arithmetic expression
    :erlang.+(1, "aaa")
iex(6)> "111"
"111"
iex(7)> "111" + "aaa"
** (ArithmeticError) bad argument in arithmetic expression
    :erlang.+("111", "aaa")
iex(7)> 10 / 3
3.3333333333333335
iex(8)> div(10, 3)
3
iex(9)> div 10, 3
3
iex(10)> 0b0010
2
iex(11)> 0o0070
56
iex(12)> 0x0040
64
iex(13)> 1.0e-10
1.0e-10
iex(14)> true
true
iex(15)> false
false
iex(16)> nil
nil
iex(17)> true and false
false
iex(18)> true or false
true
iex(19)> not false
true
iex(20)> not true
false
iex(21)> true && false
false
iex(22)> true || false
true
iex(23)> 1 and true
** (ArgumentError) argument error: 1

iex(23)> 1 && true
true
iex(24)> 1 && false
false
iex(25)> 0 && false
false
iex(26)>
nil
iex(27)> nil && false
nil
iex(28)> not 1
** (ArgumentError) argument error
    :erlang.not(1)
iex(28)> !1
false
iex(29)> !!1
true
iex(30)> nil
nil
iex(31)> !nil
true
iex(32)> !!nil
false
iex(33)> x
** (RuntimeError) undefined function: x/0

iex(33)> true || x
true
iex(34)> true && x
** (RuntimeError) undefined function: x/0

iex(34)> is_number(1)
true
iex(35)> is_number(1.0)
true
iex(36)> is_number(true)
false
iex(37)> true == :true
true
iex(38)> 1 == 1
true
iex(39)> 1 == 2
false
iex(40)> is_atom(1)
false
iex(41)> is_atom(:false)
true
iex(42)> is_atom(false)
true
iex(43)> is_boolean(true)
true
iex(44)> is_boolean(false)
true
iex(45)> is_boolean(:false)
true
iex(46)> "foo" + "bar"
** (ArithmeticError) bad argument in arithmetic expression
    :erlang.+("foo", "bar")
iex(46)> "foo" <> "bar"
"foobar"
iex(47)> "1 + 2 = #{1+2}"
"1 + 2 = 3"
iex(48)> 'abc'
'abc'
iex(49)> '日本語'
[26085, 26412, 35486]
iex(50)> "日本語"
"日本語"
iex(51)> [1, true, :atom, "string", [1,2]]
[1, true, :atom, "string", [1, 2]]
iex(52)> {1, true, :atom, "string", [1,2], {:x, :y}}
{1, true, :atom, "string", [1, 2], {:x, :y}}
iex(53)> [1,2,3]
[1, 2, 3]
iex(54)> [1,2,3] ++ [3,4,5]
[1, 2, 3, 3, 4, 5]
iex(55)> [1,2,3] -- [3,4,5]
[1, 2]
iex(56)> hd [1,2,3,4,5]
1
iex(57)> tl [1,2,3,4,5]
[2, 3, 4, 5]
iex(58)> length [1,2,3]
3
iex(59)> elem({1,2,3}, 1)
2
iex(60)> elem({1,2,3}, 0)
1
iex(61)> tuple_size({1,2,3,4,5})
5
iex(62)> put_elem({1,2,3}, 2, 4)
{1, 2, 4}
iex(63)> put_elem({1,2,3}, 1, z)
** (RuntimeError) undefined function: z/0

iex(63)> put_elem({1,2,3}, 1, 9)
{1, 9, 3}
iex(64)> 1 == 1
true
iex(65)> 1 != 1
false
iex(66)> 2 <= 2
true
iex(67)> 2 < 2
false
iex(68)> 1 == 1.0
true
iex(69)> 1 === 1.0
false
iex(70)> 1 == true
false
iex(71)> 1 < :x
true
iex(72)> x = 1
1
iex(73)> x
1
iex(74)> 1 = x
1
iex(75)> 1
1
iex(76)> x
1
iex(77)> 2 = x
** (MatchError) no match of right hand side value: 1

iex(77)> x = 2
2
iex(78)> x
2
iex(79)> 2 = x
2
iex(80)> 1 = x
** (MatchError) no match of right hand side value: 2

iex(80)> ^x = 3
** (MatchError) no match of right hand side value: 3

iex(80)> x
2
iex(81)> x = 3
3
iex(82)> x
3
iex(83)> ^y = 5
** (CompileError) iex:83: unbound variable ^y
    (elixir) src/elixir_clauses.erl:26: :elixir_clauses.match/3
    (elixir) src/elixir_translator.erl:18: :elixir_translator.translate/2
iex(83)> y
** (RuntimeError) undefined function: y/0

iex(83)> y = 5
5
iex(84)> ^y = 5
5
iex(85)> {x, y, z} = {1, :hoge, "bar"}
{1, :hoge, "bar"}
iex(86)> x
1
iex(87)> y
:hoge
iex(88)> z
"bar"
iex(89)> {:name, x} = {name, "taro"}
** (RuntimeError) undefined function: name/0

iex(89)> {:name, x} = {:name, "taro"}
{:name, "taro"}
iex(90)> x
"taro"
iex(91)> {:name, x} = {:age, 1}
** (MatchError) no match of right hand side value: {:age, 1}

iex(91)> {x, y, x} = {1, 2, 3}
** (MatchError) no match of right hand side value: {1, 2, 3}

iex(91)> {l, m, n} = {1, 2, 3}
{1, 2, 3}
iex(92)> {l, m, n} = {1, 2, 1}
{1, 2, 1}
iex(93)> {x, y, x} = {1, 2, 1}
{1, 2, 1}
iex(94)> x = 1
1
iex(95)> {x, x} = {2, 1}
** (MatchError) no match of right hand side value: {2, 1}

iex(95)> {x, ^x} = {1, 1}
{1, 1}
iex(96)> {x, ^x} = {2, 2}
** (MatchError) no match of right hand side value: {2, 2}

iex(96)> {x, ^x} = {2, 1}
{2, 1}
iex(97)> x
2
iex(98)> {y, y, x} = {1, 2, 3}
** (MatchError) no match of right hand side value: {1, 2, 3}

iex(98)> {_, _, x} = {1, 2, 3}
{1, 2, 3}
iex(99)> x
3
iex(100)> _
** (CompileError) iex:100: unbound variable _

iex(100)> [x, y, z] = [1, :hoge, "bar"]
[1, :hoge, "bar"]
iex(101)> [head | tail] = [1, :hoge, "bar"]
[1, :hoge, "bar"]
iex(102)> head
1
iex(103)> tail
[:hoge, "bar"]
iex(104)> [head | tail] = [1]
[1]
iex(105)> head
1
iex(106)> tail
[]
iex(107)> [head | tail] = []
** (MatchError) no match of right hand side value: []

iex(107)> list = [1, 2, 3]
[1, 2, 3]
iex(108)> [0 | list]
[0, 1, 2, 3]
iex(109)> [1 | [2]\
...(109)> ]
[1, 2]
iex(110)> [1 | [2]]
[1, 2]
iex(111)> [1 | 2]
[1 | 2]
iex(112)> k_list = [x: 20, y: 30]
[x: 20, y: 30]
iex(113)> k_list[:x]
20
iex(114)> k_list[:z]
nil
iex(115)> k_list = [{x: 20}, {y: 30}]
** (SyntaxError) iex:115: syntax error before: x

iex(115)> k_list
[x: 20, y: 30]
iex(116)> k_list ++ [z: 40]
[x: 20, y: 30, z: 40]
iex(117)> k_list
[x: 20, y: 30]
iex(118)> [{:z, 40} | k_list]
[z: 40, x: 20, y: 30]
iex(119)> k_list
[x: 20, y: 30]
iex(120)> new_list = k_list ++ [z: 40, x: 10]
[x: 20, y: 30, z: 40, x: 10]
iex(121)> new_list[:x]
20
iex(122)>
nil
iex(123)>
nil
iex(124)>
nil
iex(125)>
nil
iex(126)> %{:x => 20, :y => 30}
%{x: 20, y: 30}
iex(127)> %{:x => 20, :x => 30}
%{x: 30}
iex(128)> %{x: 20, y: 30}
%{x: 20, y: 30}
iex(129)> %{name: n, age: x} = %{name: 'taro', age: 30}
%{age: 30, name: 'taro'}
iex(130)> n
'taro'
iex(131)> x
30
iex(132)> %{name: n} = %{name: 'taro', age: 30}
%{age: 30, name: 'taro'}
iex(133)> n
'taro'
iex(134)> x
30
iex(135)> %{name: n} = %{age: 30}
** (MatchError) no match of right hand side value: %{age: 30}

iex(135)> map = %{name: 'taro', age: 30}
%{age: 30, name: 'taro'}
iex(136)> map.name
'taro'
iex(137)> map.age
30
iex(138)> map.x
** (KeyError) key :x not found in: %{age: 30, name: 'taro'}

iex(138)> %{map | name: 'ziro'}
%{age: 30, name: 'ziro'}
iex(139)> map.name
'taro'
iex(140)> map
%{age: 30, name: 'taro'}
iex(141)> %{map | x: 'xxx'}
** (ArgumentError) argument error
    (stdlib) :maps.update(:x, 'xxx', %{age: 30, name: 'taro'})
    (stdlib) erl_eval.erl:255: anonymous fn/2 in :erl_eval.expr/5
    (stdlib) lists.erl:1261: :lists.foldl/3
iex(141)>
nil