Last active
June 20, 2019 06:03
-
-
Save takuoka/7799b6c996582178aa38 to your computer and use it in GitHub Desktop.
Elixir 文法 メモ ref: https://qiita.com/taku_oka/items/185bdecbc87f6a9c6f07
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add = fn a, b -> a + b end | |
is_function add, 1 # false | |
add.(1, 2) # 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(fn -> x = 0 end).() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if true do | |
"ok" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if false do | |
"this won't be seen" | |
else | |
"this will" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if true do | |
a = 1 + 2 | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if true, do: 1 + 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if false, do: "this", else: "that" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if true, do: ( | |
a = 1 + 2 | |
a + 10 | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
is_number(if true do | |
1 + 2 | |
end) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> [a: a] = [a: 1] | |
[a: 1] | |
iex> a | |
1 | |
iex> [a: a] = [a: 1, b: 2] | |
** (MatchError) no match of right hand side value: [a: 1, b: 2] | |
iex> [b: b, a: a] = [a: 1, b: 2] | |
** (MatchError) no match of right hand side value: [a: 1, b: 2] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> %{} = %{:a => 1, 2 => :b} | |
%{:a => 1, 2 => :b} | |
iex> %{:a => a} = %{:a => 1, 2 => :b} | |
%{:a => 1, 2 => :b} | |
iex> a | |
1 | |
iex> %{:c => c} = %{:a => 1, 2 => :b} | |
** (MatchError) no match of right hand side value: %{2 => :b, :a => 1} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> map = %{:a => 1, 2 => :b} | |
%{:a => 1, 2 => :b} | |
iex> map.a | |
1 | |
iex> %{map | :a => 2} | |
%{:a => 2, 2 => :b} | |
iex> %{map | :c => 3} | |
** (ArgumentError) argument error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
length([1,[2],3]) = 3 | |
** (CompileError) iex:1: illegal pattern |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> keyword = [] | |
[] | |
iex> map = %{} | |
%{} | |
iex> Dict.put(keyword, :a, 1) | |
[a: 1] | |
iex> Dict.put(map, :a, 1) | |
%{a: 1} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Math do | |
def sum(a, b) do | |
a + b | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Math do | |
def zero?(0) do | |
true | |
end | |
def zero?(x) when is_number(x) do | |
false | |
end | |
end | |
Math.zero?(0) #=> true | |
Math.zero?(1) #=> false | |
Math.zero?([1,2,3]) | |
#=> ** (FunctionClauseError) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Concat do | |
def join(a, b, sep \\ " ") do | |
a <> sep <> b | |
end | |
end | |
IO.puts Concat.join("Hello", "world") #=> Hello world | |
IO.puts Concat.join("Hello", "world", "_") #=> Hello_world |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Concat do | |
def join(a, b \\ nil, sep \\ " ") | |
def join(a, b, _sep) when is_nil(b) do | |
a | |
end | |
def join(a, b, sep) do | |
a <> sep <> b | |
end | |
end | |
IO.puts Concat.join("Hello", "world") #=> Hello world | |
IO.puts Concat.join("Hello", "world", "_") #=> Hello_world | |
IO.puts Concat.join("Hello") #=> Hello |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> Enum.reduce([1, 2, 3], 0, fn(x, acc) -> x + acc end) | |
6 | |
iex> Enum.map([1, 2, 3], fn(x) -> x * 2 end) | |
[2, 4, 6] | |
# キャプチャ構文 | |
iex> Enum.reduce([1, 2, 3], 0, &+/2) | |
6 | |
iex> Enum.map([1, 2, 3], &(&1 * 2)) | |
[2, 4, 6] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> Enum.map(1..3, fn x -> x * 2 end) | |
[2, 4, 6] | |
iex> Enum.reduce(1..3, 0, &+/2) | |
6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1..100_000 |> Enum.map(&(&1 * 3)) |> Enum.filter(odd?) |> Enum.sum |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> s = 1..1000000000 |> Stream.map(&(&1 * 3)) | |
#Stream<[enum: 1..1000000000, funs: [#Function<45.113986093/1 in Stream.map/2>]]> | |
> Enum.take(s, 3) | |
[3, 6, 9] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> stream = Stream.cycle([1, 2, 3]) | |
#Function<15.16982430/2 in Stream.cycle/1> | |
iex> Enum.take(stream, 10) | |
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case {1, 2, 3} do | |
{4, 5 ,6} -> "この場合マッチしない" | |
{1, x, 3} -> "これにマッチする" | |
_ -> "どれでもマッチ" | |
end | |
# 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
send self(), {:abc, "dope"} | |
# {:abc, "dope"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
receive do | |
{:abc, msg} -> msg | |
{:woo, msg} -> "aaa" | |
end | |
# "dope" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
receive do | |
{:wtf, msg} -> msg | |
{:woo, msg} -> "aaa" | |
after | |
1000 -> "1秒待ったけど何も来なかった..." | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> send self(), :hello | |
:hello | |
iex> flush() | |
:hello | |
:ok |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule KV do | |
def start_link do | |
{:ok, spawn_link(fn -> loop(%{}) end)} | |
end | |
defp loop(map) do | |
receive do | |
{:get, key, caller} -> | |
send caller, Map.get(map, key) | |
loop(map) | |
{:put, key, value} -> | |
loop(Map.put(map, key, value)) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case File.read(file) do | |
{:ok, body} -> # handle ok | |
{:error, r} -> # handle error | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> IO.puts :stdio, "hello" | |
hello | |
:ok | |
iex> IO.puts Process.group_leader, "hello" | |
hello | |
:ok |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Math do | |
alias Math.List, as: List | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Math do | |
def plus(a, b) do | |
alias Math.List | |
# ... | |
end | |
def minus(a, b) do | |
# ... | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> Integer.is_odd(3) | |
** (CompileError) iex:1: you must require Integer before invoking the macro Integer.is_odd/1 | |
iex> require Integer | |
nil | |
iex> Integer.is_odd(3) | |
true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> x = 1 | |
1 | |
iex> case 10 do | |
...> ^x -> "Won't match" | |
...> _ -> "Will match" | |
...> end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> import List, only: [duplicate: 2] | |
nil | |
iex> duplicate :ok, 3 | |
[:ok, :ok, :ok] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> is_atom(String) | |
true | |
iex> to_string(String) | |
"Elixir.String" | |
iex> :"Elixir.String" | |
String |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> :lists.flatten([1,[2],3]) | |
[1, 2, 3] | |
iex> mod = :lists | |
:lists | |
iex> mod.flatten([1,[2],3]) | |
[1,2,3] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Foo do | |
defmodule Bar do | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Elixir.Foo do | |
defmodule Elixir.Foo.Bar do | |
end | |
alias Elixir.Foo.Bar, as: Bar | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Math do | |
@moduledoc """ | |
Provides math-related functions. | |
## Examples | |
iex> Math.sum(1, 2) | |
3 | |
""" | |
@doc """ | |
Calculates the sum of two numbers. | |
""" | |
def sum(a, b), do: a + b | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyServer do | |
@initial_state %{host: "147.0.0.1", port: 3456} | |
IO.inspect @initial_state | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule User do | |
defstruct name: "john", age: 27 | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> is_map(%User{}) | |
true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> john = %User{} | |
%User{age: 27, name: "john"} | |
iex> john.name | |
"john" | |
iex> meg = %{john | name: "meg"} | |
%User{age: 27, name: "meg"} | |
iex> %{meg | oops: :field} | |
** (ArgumentError) argument error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> case {1, 2, 3} do | |
...> {1, x, 3} when x > 0 -> "マッチする" | |
...> _-> "マッチしない" | |
...> end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> %User{name: name} = john | |
%User{age: 27, name: "john"} | |
iex> name | |
"john" | |
iex> %User{} = %{} | |
** (MatchError) no match of right hand side value: %{} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> john.__struct__ | |
User |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defprotocol Blank do | |
@doc "Returns true if data is considered blank/empty" | |
def blank?(data) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 整数は決してブランクにならない - Integers are never blank | |
defimpl Blank, for: Integer do | |
def blank?(_), do: false | |
end | |
# 空リストのときだけブランクになる - Just empty list is blank | |
defimpl Blank, for: List do | |
def blank?([]), do: true | |
def blank?(_), do: false | |
end | |
# 空のマップのときだけブランクになる - Just empty map is blank | |
defimpl Blank, for: Map do | |
# Keep in mind we could not pattern match on %{} because | |
# it matches on all maps. We can however check if the size | |
# is zero (and size is a fast operation). | |
def blank?(map), do: map_size(map) == 0 | |
end | |
# アトムがfalseとnilのときだけブランクになる - Just the atoms false and nil are blank | |
defimpl Blank, for: Atom do | |
def blank?(false), do: true | |
def blank?(nil), do: true | |
def blank?(_), do: false | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> Blank.blank?(0) | |
false | |
iex> Blank.blank?([]) | |
true | |
iex> Blank.blank?([1, 2, 3]) | |
false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defimpl Blank, for: User do | |
def blank?(_), do: false | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defprotocol Blank do | |
@fallback_to_any true | |
def blank?(data) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defimpl Blank, for: Any do | |
def blank?(_), do: false | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> Enum.map [1, 2, 3], fn(x) -> x * 2 end | |
[2,4,6] | |
iex> Enum.reduce 1..3, 0, fn(x, acc) -> x + acc end | |
6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> to_string :hello | |
"hello" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> case :ok do | |
...> :error -> "Won't match" | |
...> end | |
# ** (CaseClauseError) no case clause matching: :ok |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> "age: #{25}" | |
"age: 25" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> tuple = {1, 2, 3} | |
{1, 2, 3} | |
iex> "tuple: #{tuple}" | |
** (Protocol.UndefinedError) protocol String.Chars not implemented for {1, 2, 3} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> "tuple: #{inspect tuple}" | |
"tuple: {1, 2, 3}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> :foo + 1 | |
** (ArithmeticError) bad argument in arithmetic expression | |
:erlang.+(:foo, 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> raise "oops" | |
** (RuntimeError) oops |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> raise ArgumentError, message: "invalid argument foo" | |
** (ArgumentError) invalid argument foo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> defmodule MyError do | |
iex> defexception message: "default message" | |
iex> end | |
iex> raise MyError | |
** (MyError) default message | |
iex> raise MyError, message: "custom message" | |
** (MyError) custom message |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> try do | |
...> raise "oops" | |
...> rescue | |
...> e in RuntimeError -> e | |
...> end | |
%RuntimeError{message: "oops"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> try do | |
...> Enum.each -50..50, fn(x) -> | |
...> if rem(x, 13) == 0, do: throw(x) | |
...> end | |
...> "Got nothing" | |
...> catch | |
...> x -> "Got #{x}" | |
...> end | |
"Got -39" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> spawn_link fn -> exit(1) end | |
#PID<0.56.0> | |
** (EXIT from #PID<0.56.0>) 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> f = fn | |
...> x, y when x > 0 -> x + y | |
...> x, y -> x * y | |
...> end | |
#Function<12.71889879/2 in :erl_eval.expr/5> | |
iex> f.(1, 3) | |
4 | |
iex> f.(-1, 3) | |
-3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> try do | |
...> exit "I am exiting" | |
...> catch | |
...> :exit, _ -> "not really" | |
...> end | |
"not really" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try do | |
raise "oops" | |
after | |
IO.puts "あふたー" | |
end | |
↓ | |
あふたー | |
** (MatchError) no match of right hand side value: {:error, :enoent} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> for n <- [1, 2, 3, 4], do: n * n | |
[1, 4, 9, 16] | |
iex> for n <- 1..4, do: n * n | |
[1, 4, 9, 16] | |
iex> values = [good: 1, good: 2, bad: 3, good: 4] | |
iex> for {:good, n} <- values, do: n * n | |
[1, 4, 16] | |
iex> require Integer | |
iex> for n <- 1..4, Integer.is_odd(n), do: n * n | |
[1, 9] | |
for dir <- dirs, | |
file <- File.ls!(dir), | |
path = Path.join(dir, file), | |
File.regular?(path) do | |
File.rm!(path) | |
end | |
iex> pixels = <<213, 45, 132, 64, 76, 32, 76, 0, 0, 234, 32, 15>> | |
iex> for <<r::8, g::8, b::8 <- pixels>>, do: {r, g, b} | |
[{213,45,132},{64,76,32},{76,0,0},{234,32,15}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> for <<c <- " hello world ">>, c != ?\s, into: "", do: <<c>> | |
"helloworld" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> cond do | |
...> 2 + 2 == 5 -> | |
...> "This will not be true" | |
...> 2 * 2 == 3 -> | |
...> "Nor this" | |
...> 1 + 1 == 2 -> | |
...> "But this will" | |
...> true -> | |
...> "全部falseだった場合" #何もtrueにならないとエラーになるからこうする | |
...> end | |
"But this will" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> cond do | |
...> hd([1,2,3]) -> | |
...> "nilとfalse以外はtrue" | |
...> end | |
"nilとfalse以外はtrue" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment