---
title: ClojurianからみたElixir
tags:  #elixir #clojure #関数型プログラミング  
author: [Kent OHASHI](https://www.docswell.com/user/lagenorhynque)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/LE3NMQVPE5.jpg?width=480
description: Clojure使い(Clojurian)としてElixirに入門した話です(*&gt; ᴗ •*)ゞ
published: May 27, 19
canonical: https://www.docswell.com/s/lagenorhynque/K4QV7J-elixir-from-a-clojurians-perspective
---
# Page. 1

![Page Image](https://bcdn.docswell.com/page/LE3NMQVPE5.jpg)

Clojurian
Elixir


# Page. 2

![Page Image](https://bcdn.docswell.com/page/8EDW69837G.jpg)

lagénorhynque
(defprofile lagénorhynque
:id
@lagenorhynque
:reading
&quot;/laʒenɔʁɛ̃
k/&quot;
:aliases
[&quot;
&quot;]
:languages
[Clojure Haskell English français]
:interests
[programming language-learning law mathematics]
:commits
[&quot;github.com/lagenorhynque/duct.module.pedestal&quot;]
:contributes
[&quot;github.com/japan-clojurians/clojure-site-ja&quot;])


# Page. 3

![Page Image](https://bcdn.docswell.com/page/V7P9YZ8PJ8.jpg)

6 Lisp
Clojure
:
(*&gt; ᴗ •*)
REST API
cf. BOOTH https://booth.pm/ja/items/1317263


# Page. 4

![Page Image](https://bcdn.docswell.com/page/2JVMGWNVJQ.jpg)

1. Clojure
2.
Elixir
3. Clojure
4. Clojure
Elixir


# Page. 5

![Page Image](https://bcdn.docswell.com/page/5EGG8DK1EL.jpg)

Clojure Elixir


# Page. 6

![Page Image](https://bcdn.docswell.com/page/4JQX91NNEP.jpg)

Clojure
: Rich Hickey
: 2007
: 1.10.0
:
Java VM (JVM)
Lisp
/


# Page. 7

![Page Image](https://bcdn.docswell.com/page/K746XRG3E1.jpg)

Elixir
: José Valim
: 2011
: 1.8.2
:
Erlang VM (EVM; BEAM)
Ruby
Clojure


# Page. 8

![Page Image](https://bcdn.docswell.com/page/LJ1VNQDZEG.jpg)

e.g.
20
Clojure
;; REPL (read-eval-print loop)
user&gt; (-&gt;&gt; (iterate (fn [[a b]] [b (+ a b)]) [0 1])
(map first)
(take 20))
(0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181)


# Page. 9

![Page Image](https://bcdn.docswell.com/page/GJWL4MY6E2.jpg)

Elixir
# IEx (Interactive Elixir)
iex(1)&gt; Stream.iterate({0, 1}, fn {a, b} -&gt; {b, a+b} end) |&gt;
...(1)&gt;
Stream.map(&amp;elem(&amp;1, 0)) |&gt;
...(1)&gt;
Enum.take(20)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610,
987, 1597, 2584, 4181]
#
iex(2)&gt; Stream.unfold({0, 1}, fn {a, b} -&gt; {a, {b, a+b}} end) |&gt;
...(2)&gt;
Enum.take(20)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610,
987, 1597, 2584, 4181]


# Page. 10

![Page Image](https://bcdn.docswell.com/page/4EZM2QXRJ3.jpg)

Elixir
à la Clojure?


# Page. 11

![Page Image](https://bcdn.docswell.com/page/Y76NV6417V.jpg)

Clojure
2


# Page. 12

![Page Image](https://bcdn.docswell.com/page/G755D6QL74.jpg)

cf.
: Programming Clojure, Third Edition


# Page. 13

![Page Image](https://bcdn.docswell.com/page/9J2N8MP3ER.jpg)

Elixir


# Page. 14

![Page Image](https://bcdn.docswell.com/page/DEYP6X58EM.jpg)

cf.
: Programming Elixir ≥ 1.6


# Page. 15

![Page Image](https://bcdn.docswell.com/page/VJN5Z1N978.jpg)

7
7
Ruby, Io, Prolog, Scala, Erlang, Clojure, Haskell


# Page. 16

![Page Image](https://bcdn.docswell.com/page/YE9YZ5R3J3.jpg)

Seven More Languages in Seven Weeks
Lua, Factor, Elixir, Elm, Julia, MiniKanren, Idris


# Page. 17

![Page Image](https://bcdn.docswell.com/page/GE861YWL7D.jpg)

Seven Concurrency Models in Seven Weeks
Chapter 3, 4, 6: Clojure
Chapter 5: Elixir


# Page. 18

![Page Image](https://bcdn.docswell.com/page/LEL9Q4NQJR.jpg)

Mastering Clojure Macros


# Page. 19

![Page Image](https://bcdn.docswell.com/page/4JMZ1KQKEW.jpg)

Metaprogramming Elixir


# Page. 20

![Page Image](https://bcdn.docswell.com/page/PJR6LX8679.jpg)

Elixir
Erlang/OTP
Elixir


# Page. 21

![Page Image](https://bcdn.docswell.com/page/PEXMW68D7X.jpg)

Elixir
Elixir


# Page. 22

![Page Image](https://bcdn.docswell.com/page/3EKGV1KDJD.jpg)

Elixir
Getting Started


# Page. 23

![Page Image](https://bcdn.docswell.com/page/L73NMQZP75.jpg)

Erlang/Elixir Syntax: A Crash Course


# Page. 24

![Page Image](https://bcdn.docswell.com/page/87DW69R3JG.jpg)

Clojure


# Page. 25

![Page Image](https://bcdn.docswell.com/page/VJP9YZWPE8.jpg)



# Page. 26

![Page Image](https://bcdn.docswell.com/page/2EVMGW8VEQ.jpg)



# Page. 27

![Page Image](https://bcdn.docswell.com/page/57GG8D517L.jpg)

Clojure
user&gt; &#039;(1 2 3)
(1 2 3)
user&gt; [1 2 3]
[1 2 3]
user&gt; {:a 1 :b 2 :c 3}
{:a 1, :b 2, :c 3}
;
※
;
;
Elixir
iex(2)&gt; [1, 2, 3]
[1, 2, 3]
iex(3)&gt; {1, 2, 3}
{1, 2, 3}
iex(4)&gt; %{a: 1, b: 2, c: 3}
%{a: 1, b: 2, c: 3}
cf.
#
#
#
(Clojure
)


# Page. 28

![Page Image](https://bcdn.docswell.com/page/4EQX91ZN7P.jpg)

/
Clojure
user&gt; (ns example)
nil
example&gt; (defn square [x]
(* x x))
#&#039;example/square
example&gt; (in-ns &#039;user)
#namespace[user]
user&gt;
;
;
`example`
`square`
;
`user`
Elixir
iex(5)&gt; defmodule Example do #
`Example`
...(5)&gt;
def square(x) do
#
`square`
...(5)&gt;
x * x
...(5)&gt;
end
...(5)&gt; end
{:module, Example,
&lt;&lt;70, 79, 82, 49, 0, 0, 4, 100, 66, 69, 65, 77, 65, 116, 85, 56,
0, 0, 0, 14, 14, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97,
101, 8, 95, 95, 105, 110, 102, 111, 95, ...&gt;&gt;, {:square, 1}}


# Page. 29

![Page Image](https://bcdn.docswell.com/page/KJ46XR3371.jpg)

Clojure
user&gt; (example/square 3) ; `example`
`square`
9
user&gt; (map example/square (range 1 (inc 10)))
(1 4 9 16 25 36 49 64 81 100)
Elixir
iex(6)&gt; Example.square(3) # `Example`
`square`
9
iex(7)&gt; Enum.map(1..10, &amp;Example.square/1)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]


# Page. 30

![Page Image](https://bcdn.docswell.com/page/LE1VNQ1Z7G.jpg)

/
Clojure
user&gt; (map (fn [x] (* x x)) (range 1 (inc 10)))
(1 4 9 16 25 36 49 64 81 100)
user&gt; (map #(* % %) (range 1 (inc 10)))
(1 4 9 16 25 36 49 64 81 100)
Elixir
iex(8)&gt; Enum.map(1..10, fn(x) -&gt; x * x end)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
iex(8)&gt; Enum.map(1..10, &amp;(&amp;1 * &amp;1))
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]


# Page. 31

![Page Image](https://bcdn.docswell.com/page/GEWL4M8672.jpg)

­&gt;&gt; (threading macro) vs |&gt; (pipe operator)
Clojure
user&gt; (-&gt;&gt; (range 1 (inc 10))
(map #(* % %))
(filter odd?)
(apply +))
165
Elixir
iex(9)&gt; require Integer
Integer
iex(10)&gt; 1..10 |&gt;
...(10)&gt;
Enum.map(&amp;(&amp;1 * &amp;1)) |&gt;
...(10)&gt;
Enum.filter(&amp;Integer.is_odd/1) |&gt;
...(10)&gt;
Enum.sum
165
cf.


# Page. 32

![Page Image](https://bcdn.docswell.com/page/47ZM2Q8RE3.jpg)



# Page. 33

![Page Image](https://bcdn.docswell.com/page/YJ6NV6P1JV.jpg)

Clojure
user&gt; (defprotocol Shape
(area [x]))
Shape
;
;
`Shape`
`area`
Elixir
iex(1)&gt; defprotocol Shape do #
`Shape`
...(1)&gt;
def area(x)
#
`area`
...(1)&gt; end
{:module, Shape, ..., {:__protocol__, 1}}


# Page. 34

![Page Image](https://bcdn.docswell.com/page/GJ55D6KLJ4.jpg)

/
Clojure
user&gt; (defrecord Triangle [x h]
Shape
(area [{:keys [x h]}] (/
user.Triangle
user&gt; (area (map-&gt;Triangle {:x 3
3
;
`Triangle`
;
(* x h) 2)))
:h 2}))
user&gt; (defrecord Rectangle [x y] ;
Shape
;
(area [{:keys [x y]}] (* x y)))
user.Rectangle
user&gt; (area (map-&gt;Rectangle {:x 2 :y 3}))
6
`Rectangle`


# Page. 35

![Page Image](https://bcdn.docswell.com/page/LE3NMQZDE5.jpg)

Elixir
iex(2)&gt; defmodule Triangle do #
`Triangle`
...(2)&gt;
defstruct [:x, :h]
...(2)&gt; end
{:module, Triangle, ..., %Triangle{h: nil, x: nil}}
iex(3)&gt; defimpl Shape, for: Triangle do #
...(3)&gt;
def area(%Triangle{x: x, h: h}), do: x * h / 2
...(3)&gt; end
{:module, Shape.Triangle, ..., {:__impl__, 1}}
iex(4)&gt; Shape.area(%Triangle{x: 3, h: 2})
3.0
iex(5)&gt; defmodule Rectangle do #
`Rectangle`
...(5)&gt;
defstruct [:x, :y]
...(5)&gt; end
{:module, Rectangle, ..., %Rectangle{x: nil, y: nil}}
iex(6)&gt; defimpl Shape, for: Rectangle do #
...(6)&gt;
def area(%Rectangle{x: x, y: y}), do: x * y
...(6)&gt; end
{:module, Shape.Rectangle, ..., {:__impl__, 1}}
iex(7)&gt; Shape.area(%Rectangle{x: 2, y: 3})
6


# Page. 36

![Page Image](https://bcdn.docswell.com/page/8EDW69R27G.jpg)

Clojure
user&gt; (defrecord Circle [r]) ;
`Circle`
user.Circle
user&gt; (area (map-&gt;Circle {:r 3}))
Execution error (IllegalArgumentException) at user/eval5412$fn$G
(REPL:47).
No implementation of method: :area of protocol: #&#039;user/Shape fou
nd for class: user.Circle
Elixir
iex(8)&gt; defmodule Circle do #
`Circle`
...(8)&gt;
defstruct [:r]
...(8)&gt; end
{:module, Circle, ..., %Circle{r: nil}}
iex(9)&gt; Shape.area(%Circle{r: 3})
** (Protocol.UndefinedError) protocol Shape not implemented for
%Circle{r: 3}
iex:2: Shape.impl_for!/1
iex:3: Shape.area/1


# Page. 37

![Page Image](https://bcdn.docswell.com/page/V7P9YZWLJ8.jpg)

Clojure
user&gt; (extend-protocol Shape ;
Circle
(area [{:keys [r]}] (* Math/PI r r)))
nil
user&gt; (area (map-&gt;Circle {:r 3}))
28.274333882308138
Elixir
iex(10)&gt; defimpl Shape, for: Circle do #
...(10)&gt;
def area(%Circle{r: r}), do: :math.pi() * r * r
...(10)&gt; end
{:module, Shape.Circle, ..., {:__impl__, 1}}
iex(11)&gt; Shape.area(%Circle{r: 3})
28.274333882308138


# Page. 38

![Page Image](https://bcdn.docswell.com/page/2JVMGW86JQ.jpg)



# Page. 39

![Page Image](https://bcdn.docswell.com/page/5EGG8D52EL.jpg)

AST
Clojure
user&gt; &#039;(when (= 1 1)
(println &quot;Truthy!&quot;))
(when (= 1 1) (println &quot;Truthy!&quot;))
;;
user&gt; (quote
(when (= 1 1)
(println &quot;Truthy!&quot;)))
(when (= 1 1) (println &quot;Truthy!&quot;))


# Page. 40

![Page Image](https://bcdn.docswell.com/page/4JQX91Z9EP.jpg)

Elixir
iex(1)&gt; quote do
...(1)&gt;
if 1 == 1 do
...(1)&gt;
IO.puts &quot;Truthy!&quot;
...(1)&gt;
end
...(1)&gt; end
{:if, [context: Elixir, import: Kernel],
[
{:==, [context: Elixir, import: Kernel], [1, 1]},
[
do: {{:., [], [{:__aliases__, [alias: false], [:IO]}, :puts
]}, [],
[&quot;Truthy!&quot;]}
]
]}


# Page. 41

![Page Image](https://bcdn.docswell.com/page/K746XRDRE1.jpg)

Clojure
user&gt; (ns example)
nil
example&gt; (defmacro my-when-not [test &amp; body]
`(if ~test
nil
(do ~@body)))
#&#039;example/my-when-not
example&gt; (in-ns &#039;user)
#namespace[user]
user&gt;


# Page. 42

![Page Image](https://bcdn.docswell.com/page/LJ1VNQZVEG.jpg)

Elixir
iex(2)&gt; defmodule Example do
...(2)&gt;
defmacro my_unless(test, do: body) do
...(2)&gt;
quote do
...(2)&gt;
if unquote(test), do: nil, else: unquote(body)
...(2)&gt;
end
...(2)&gt;
end
...(2)&gt; end
{:module, Example, ..., {:my_unless, 2}}


# Page. 43

![Page Image](https://bcdn.docswell.com/page/GJWL4M93E2.jpg)

Clojure
user&gt; (example/my-when-not (= 1 2)
(println &quot;Falsy!&quot;)
42)
Falsy!
42
user&gt; (example/my-when-not (= 1 1)
(println &quot;Falsy!&quot;)
42)
nil


# Page. 44

![Page Image](https://bcdn.docswell.com/page/4EZM2Q9GJ3.jpg)

Elixir
iex(3)&gt;
Example
iex(4)&gt;
...(4)&gt;
...(4)&gt;
...(4)&gt;
Falsy!
42
iex(5)&gt;
...(5)&gt;
...(5)&gt;
...(5)&gt;
nil
require Example
Example.my_unless 1 == 2 do
IO.puts &quot;Falsy!&quot;
42
end
Example.my_unless 1 == 1 do
IO.puts &quot;Falsy!&quot;
42
end


# Page. 45

![Page Image](https://bcdn.docswell.com/page/Y76NV6KQ7V.jpg)

Clojure
user&gt; (macroexpand-1 ;
1
&#039;(example/my-when-not (= 1 2)
(println &quot;Falsy!&quot;)
42))
(if (= 1 2) nil (do (println &quot;Falsy!&quot;) 42))


# Page. 46

![Page Image](https://bcdn.docswell.com/page/G755D6P274.jpg)

Elixir
iex(6)&gt; quote do
...(6)&gt;
Example.my_unless 1 == 2 do
...(6)&gt;
IO.puts &quot;Falsy!&quot;
...(6)&gt;
42
...(6)&gt;
end
...(6)&gt; end |&gt; Macro.expand_once(__ENV__) |&gt;
...(6)&gt;
Macro.to_string |&gt; IO.puts
if(1 == 2) do
nil
else
IO.puts(&quot;Falsy!&quot;)
42
end
:ok
#
# AST
1


# Page. 47

![Page Image](https://bcdn.docswell.com/page/9J2N8M61ER.jpg)

Clojure


# Page. 48

![Page Image](https://bcdn.docswell.com/page/DEYP6X94EM.jpg)

vs
cf. Simple Made Easy (by Rich Hickey)
REPL
cf. REPL
(REPL-driven development)


# Page. 49

![Page Image](https://bcdn.docswell.com/page/VJN5Z1L678.jpg)

Elixirists
Clojurians (?)


# Page. 50

![Page Image](https://bcdn.docswell.com/page/YE9YZ54RJ3.jpg)

Further Reading


# Page. 51

![Page Image](https://bcdn.docswell.com/page/GE861YQG7D.jpg)

https://clojure.org
: https://japanclojurians.github.io/clojure-site-ja/
https://elixir-lang.org
: https://elixir-lang.jp
Getting Started
Erlang/Elixir Syntax: A Crash Course


# Page. 52

![Page Image](https://bcdn.docswell.com/page/LEL9Q4XXJR.jpg)

Clojure
2
: Programming Clojure, Third Edition
Elixir
: Programming Elixir ≥ 1.6
7
7
Seven More Languages in Seven Weeks


# Page. 53

![Page Image](https://bcdn.docswell.com/page/4JMZ1KL3EW.jpg)

Seven Concurrency Models in Seven Weeks
Scala/Akka
(Chapter 5)
Mastering Clojure Macros
Metaprogramming Elixir


# Page. 54

![Page Image](https://bcdn.docswell.com/page/PJR6LXKR79.jpg)

Elixir
Elixir
Elixir
Elixir
Erlang/OTP


# Page. 55

![Page Image](https://bcdn.docswell.com/page/PEXMW6LY7X.jpg)

https://scrapbox.io/lagenorhynque/Elixir
: lagenorhynque/programming-elixir


