2
0

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 1 year has passed since last update.

概要

paiza.ioでelixirやってみた。
Enum使ってみた。

サンプルコード

Enum.all?([1, 2, 3])
|> IO.inspect
Enum.all?([1, nil, 3])
|> IO.inspect
Enum.all?([2, 4, 6], fn x -> 
	rem(x, 2) == 0 
end)
|> IO.inspect
Enum.all?([2, 3, 4], fn x -> 
	rem(x, 2) == 0 
end)
|> IO.inspect
Enum.any?([false, false, false])
|> IO.inspect
Enum.any?([false, true, false])
|> IO.inspect
Enum.any?([2, 4, 6], fn x -> 
	rem(x, 2) == 1 
end)
|> IO.inspect
Enum.any?([2, 3, 4], fn x -> 
	rem(x, 2) == 1 
end)
|> IO.inspect
Enum.at([2, 4, 6], 0)
|> IO.inspect
Enum.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))
|> IO.inspect
Enum.chunk_every([1, 2, 3, 4, 5, 6], 2)
|> IO.inspect
chunk_fun = fn element, acc ->
	if rem(element, 2) == 0 do
		{:cont, Enum.reverse([element | acc]), []}
	else
		{:cont, [element | acc]}
	end
end
after_fun = fn
	[] -> 
		{:cont, []}
	acc -> 
		{:cont, Enum.reverse(acc), []}
end
Enum.chunk_while(1..10, [], chunk_fun, after_fun)
|> IO.inspect
Enum.concat([1..3, 4..6, 7..9])
|> IO.inspect
Enum.concat(1..3, 4..6)
|> IO.inspect
Enum.count([1, 2, 3])
|> IO.inspect
Enum.count([1, 2, 3, 4, 5], fn x -> 
	rem(x, 2) == 0 
end)
|> IO.inspect
Enum.count_until(1..20, 5)
|> IO.inspect
Enum.count_until(1..20, fn x -> 
	rem(x, 2) == 0 
end, 7)
|> IO.inspect
Enum.dedup([1, 2, 3, 3, 2, 1])
|> IO.inspect
Enum.dedup_by([{1, :a}, {2, :b}, {2, :c}, {1, :a}], fn {x, _} -> 
	x 
end)
|> IO.inspect
Enum.drop([1, 2, 3], 2)
|> IO.inspect
Enum.drop_every(1..10, 2)
|> IO.inspect
Enum.drop_while([1, 2, 3, 2, 1], fn x -> 
	x < 3 
end)
|> IO.inspect
Enum.each(["some", "example"], fn x -> 
	IO.puts(x) 
end)
|> IO.inspect
Enum.empty?([])
|> IO.inspect
Enum.fetch([2, 4, 6], 0)
|> IO.inspect
Enum.fetch!([2, 4, 6], 0)
|> IO.inspect
Enum.filter([1, 2, 3], fn x -> 
	rem(x, 2) == 0 
end)
|> IO.inspect
Enum.find([2, 3, 4], fn x -> 
	rem(x, 2) == 1 
end)
|> IO.inspect
Enum.find_index([2, 4, 6], fn x -> 
	rem(x, 2) == 1 
end)
|> IO.inspect
Enum.find_index([2, 3, 4], fn x -> 
	rem(x, 2) == 1 
end)
|> IO.inspect
Enum.find_value([2, 3, 4], fn x ->
	if x > 2, do: x * x
end)
|> IO.inspect
Enum.flat_map([:a, :b, :c], fn x -> 
	[x, x] 
end)
|> IO.inspect
enumerable = 1..100
n = 3
Enum.flat_map_reduce(enumerable, 0, fn x, acc ->
	if acc < n, do: {[x], acc + 1}, else: {:halt, acc}
end)
|> IO.inspect
Enum.frequencies(~w{ant buffalo ant ant buffalo dingo})
|> IO.inspect
Enum.frequencies_by(~w{aa aA bb cc}, &String.downcase/1)
|> IO.inspect
Enum.group_by(~w{ant buffalo cat dingo}, &String.length/1)
|> IO.inspect
Enum.intersperse([1, 2, 3], 0)
|> IO.inspect
Enum.into([1, 2], [])
|> IO.inspect
Enum.into([2, 3], [3], fn x -> 
	x * 3 
end)
|> IO.inspect
Enum.join([1, 2, 3])
|> IO.inspect
Enum.map([1, 2, 3], fn x -> 
	x * 2 
end)
|> IO.inspect
Enum.map_every(1..10, 2, fn x -> 
	x + 1000 
end)
|> IO.inspect
Enum.map_intersperse([1, 2, 3], :a, &(&1 * 2))
|> IO.inspect
Enum.map_join([1, 2, 3], &(&1 * 2))
|> IO.inspect
Enum.map_reduce([1, 2, 3], 0, fn x, acc -> 
	{x * 2, x + acc} 
end)
|> IO.inspect
Enum.max([1, 2, 3])
|> IO.inspect
Enum.max_by(["a", "aa", "aaa"], fn x -> 
	String.length(x) 
end)
|> IO.inspect
Enum.member?(1..10, 5)
|> IO.inspect
Enum.min([1, 2, 3])
|> IO.inspect
Enum.min_by(["a", "aa", "aaa"], fn x -> 
	String.length(x) 
end)
|> IO.inspect
Enum.min_max([2, 3, 1])
|> IO.inspect
Enum.min_max_by(["aaa", "bb", "c"], fn x -> 
	String.length(x) 
end)
|> IO.inspect
Enum.product([])
|> IO.inspect
Enum.product([2, 3, 4])
|> IO.inspect
:rand.seed(:exsss, {100, 101, 102})
Enum.random([1, 2, 3])
|> IO.inspect
Enum.reduce([1, 2, 3, 4], fn x, acc -> 
	x * acc 
end)
|> IO.inspect
Enum.reduce([1, 2, 3], 0, fn x, acc -> 
	x + acc 
end)
|> IO.inspect
Enum.reduce_while(1..100, 0, fn x, acc ->
	if x < 5, do: {:cont, acc + x}, else: {:halt, acc}
end)
|> IO.inspect
Enum.reject([1, 2, 3], fn x -> 
	rem(x, 2) == 0 
end)
|> IO.inspect
Enum.reverse([1, 2, 3])
|> IO.inspect
Enum.reverse([1, 2, 3], [4, 5, 6])
|> IO.inspect
Enum.reverse_slice([1, 2, 3, 4, 5, 6], 2, 4)
|> IO.inspect
Enum.scan(1..5, &(&1 + &2))
|> IO.inspect
Enum.scan(1..5, 0, &(&1 + &2))
|> IO.inspect
:rand.seed(:exsss, {1, 2, 3})
Enum.shuffle([1, 2, 3])
|> IO.inspect
Enum.slice(1..100, 5..10)
|> IO.inspect
Enum.slice(1..100, 5, 10)
|> IO.inspect
Enum.sort([3, 2, 1])
|> IO.inspect
Enum.sort([1, 2, 3], &(&1 >= &2))
|> IO.inspect
Enum.sort_by(["some", "kind", "of", "monster"], &byte_size/1)
|> IO.inspect
Enum.split([1, 2, 3], 2)
|> IO.inspect
Enum.split_while([1, 2, 3, 4], fn x -> 
	x < 3 
end)
|> IO.inspect
Enum.split_with([5, 4, 3, 2, 1, 0], fn x -> 
	rem(x, 2) == 0 
end)
|> IO.inspect
Enum.sum([1, 2, 3])
|> IO.inspect
Enum.take([1, 2, 3], 2)
|> IO.inspect
Enum.take_every(1..10, 2)
|> IO.inspect
:rand.seed(:exsss, {1, 2, 3})
Enum.take_random(1..10, 2)
|> IO.inspect
Enum.take_while([1, 2, 3], fn x -> 
	x < 3 
end)
|> IO.inspect
Enum.to_list(1..3)
|> IO.inspect
Enum.uniq([1, 2, 3, 3, 2, 1])
|> IO.inspect
Enum.uniq_by([{1, :x}, {2, :y}, {1, :z}], fn {x, _} -> 
	x 
end)
|> IO.inspect
Enum.unzip([{:a, 1}, {:b, 2}, {:c, 3}])
|> IO.inspect
Enum.with_index([:a, :b, :c])
|> IO.inspect
Enum.zip([[1, 2, 3], [:a, :b, :c], ["foo", "bar", "baz"]])
|> IO.inspect
Enum.zip([1, 2, 3], [:a, :b, :c])
|> IO.inspect
enums = [[1, 1], [2, 2], [3, 3]]
Enum.zip_reduce(enums, [], fn elements, acc ->
	 [List.to_tuple(elements) | acc]
end)
|> IO.inspect
Enum.zip_reduce([1, 2], [3, 4], 0, fn x, y, acc -> 
	x + y + acc 
end)
|> IO.inspect
Enum.zip_with([[1, 2], [3, 4], [5, 6]], fn [x, y, z] -> 
	x + y + z 
end)
|> IO.inspect
Enum.zip_with([1, 2], [3, 4], fn x, y -> 
	x + y 
end)
|> IO.inspect


実行結果

true
false
true
false
false
true
false
true
2
[[1], [2, 2], [3], [4, 4, 6], '\a\a']
[[1, 2], [3, 4], [5, 6]]
[[1, 2], [3, 4], [5, 6], '\a\b', '\t\n']
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6]
3
2
5
7
[1, 2, 3, 2, 1]
[{1, :a}, {2, :b}, {1, :a}]
[3]
[2, 4, 6, 8, 10]
[3, 2, 1]
some
example
:ok
true
{:ok, 2}
2
[2]
3
nil
1
9
[:a, :a, :b, :b, :c, :c]
{[1, 2, 3], 3}
%{"ant" => 3, "buffalo" => 2, "dingo" => 1}
%{"aa" => 2, "bb" => 1, "cc" => 1}
%{3 => ["ant", "cat"], 5 => ["dingo"], 7 => ["buffalo"]}
[1, 0, 2, 0, 3]
[1, 2]
[3, 6, 9]
"123"
[2, 4, 6]
[1001, 2, 1003, 4, 1005, 6, 1007, 8, 1009, 10]
[2, :a, 4, :a, 6]
"246"
{[2, 4, 6], 6}
3
"aaa"
true
1
"a"
{1, 3}
{"c", "aaa"}
1
24
2
24
6
10
[1, 3]
[3, 2, 1]
[3, 2, 1, 4, 5, 6]
[1, 2, 6, 5, 4, 3]
[1, 3, 6, 10, 15]
[1, 3, 6, 10, 15]
[3, 2, 1]
[6, 7, 8, 9, 10, 11]
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
[1, 2, 3]
[3, 2, 1]
["of", "some", "kind", "monster"]
{[1, 2], [3]}
{[1, 2], [3, 4]}
{[4, 2, 0], [5, 3, 1]}
6
[1, 2]
[1, 3, 5, 7, 9]
[3, 1]
[1, 2]
[1, 2, 3]
[1, 2, 3]
[{1, :x}, {2, :y}]
{[:a, :b, :c], [1, 2, 3]}
[a: 0, b: 1, c: 2]
[{1, :a, "foo"}, {2, :b, "bar"}, {3, :c, "baz"}]
[{1, :a}, {2, :b}, {3, :c}]
[{1, 2, 3}, {1, 2, 3}]
10
'\t\f'
[4, 6]

#成果物

以上。

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?