1
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.empty?

[1, 2, 3, 4, 5]
|> Enum.concat([6, 7, 8, 9, 10])
|> IO.inspect
[1, 2, 3, 4, 5 | [6, 7, 8, 9, 10]]
|> IO.inspect
[1, 2, 3, 4, 5] ++ [6, 7, 8, 9, 10]
|> IO.inspect
#[1, 2, 3, 4, 5] 
#|> Enum.concat(nil)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.into([11, 12, 13, 14, 15])
|> IO.inspect
[1, 2, 3, 4, 5, 2, 3, 4]
|> Enum.into(HashSet.new)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.to_list
|> IO.inspect
[1, 2, 3, 4, 5, 2, 3, 4]
|> Enum.concat(HashSet.new)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.split(3)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.partition(fn(x) -> 
	x == 3 
end)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.sum
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.min_by(fn(x) -> -x end)
|> IO.inspect
#[{1}, 2, 3, 4] 
#|> Enum.sum

[1, 2, 3, 4, 5]
|> Enum.join(",")
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.intersperse(",")
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.map(fn(x) -> 
	:math.pow(x, 2) 
end)
|> IO.inspect
#|> select([m], %{"id" => m.id, "name" => m.name})
#|> MyApp.Repo.all

[1, 2, 3, 4, 5]
|> Enum.map(fn(x) -> 
	x * 2 
end)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.flat_map(fn(x) -> 
	[x * 2] 
end)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.flat_map(fn(x) -> 
	[:a, x * 2] 
end)
|> IO.inspect
[11, 12, 13, 14, 15]
|> Enum.group_by(fn(x) -> 
	rem(x, 2) 
end)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.chunk(2)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.chunk(2, 1)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.chunk(2, 3)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.chunk_by(fn(x) -> 
	x == 3 
end)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.all?(fn(x) -> 
	x == 3 
end)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.any?(fn(x) -> 
	x == 3 
end)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.filter(fn(x) -> 
	x == 3 
end)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.reject(fn(x) -> 
	x == 3 
end)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.take(3)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.drop(3)
|> IO.inspect
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
|> Enum.take_while(fn(x) -> 
	x <= 3 
end)
|> IO.inspect
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
|> Enum.drop_while(fn(x) -> 
	x <= 3 
end)
|> IO.inspect
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
|> Enum.take_while(fn(x) -> 
	(x <= 3) 
	|> IO.inspect 
end)
|> IO.inspect
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
|> Enum.drop_while(fn(x) -> 
	(x <= 3) 
	|> IO.inspect 
end)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.take_every(2)
|> IO.inspect
[11, 12, 13, 14, 15]
|> Enum.with_index
|> IO.inspect
#[1, 2, 3, 4, 5]
#|> Enum.each(fn(x) -> 
#	IO.inspect 
#	elem(x, 1) 
#end)

[1, 2, 3, 4, 5]
|> Enum.random
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.take_random(3)
|> IO.inspect
[{3}, 4, {5}, {1}, 2]
|> Enum.sort
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.uniq(fn(x) -> 
	x == 3 
end)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.slice(1, 3)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.reverse_slice(1, 3)
|> IO.inspect
[11, 12, 13, 14, 15]
|> Enum.at(2)
|> IO.inspect
[11, 12, 13, 14, 15, 13]
|> Enum.fetch!(2)
|> IO.inspect
[11, 12, 13, 14, 15, 13]
|> Enum.find(fn(x) -> 
	x == 13 
end)
|> IO.inspect
[11, 12, 13, 14, 15, 13]
|> Enum.find_index(fn(x) -> 
	x == 13 
end)
|> IO.inspect
[11, 12, 13, 14, 15, 13]
|> Enum.find_value(fn(x) -> 
	x == 13 
end)
|> IO.inspect
[11, 12, 13, 14, 15, 13]
|> Enum.member?(13)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.reduce(fn(x, acc) -> 
	{x, acc} 
end)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.reduce(fn(x, acc) -> 
	x + acc 
end)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.map_reduce(1, fn(x, acc) -> 
	{x, acc + 1} 
end)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.map_reduce(1, fn(x, acc) -> 
	{x * acc, acc + 1} 
end)
|> IO.inspect
[1, 2]
|> Enum.scan(99, fn(x, acc) -> 
	{x, acc} 
end)
|> IO.inspect
[1, 2, 3, 4, 5]
|> Enum.scan(99, fn(x, acc) -> 
	{x, acc} 
end)
|> IO.inspect




実行結果

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[11, 12, 13, 14, 15, 1, 2, 3, 4, 5]
#HashSet<[2, 3, 4, 1, 5]>
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 2, 3, 4]
{[1, 2, 3], [4, 5]}
{[3], [1, 2, 4, 5]}
15
5
"1,2,3,4,5"
[1, ",", 2, ",", 3, ",", 4, ",", 5]
[1.0, 4.0, 9.0, 16.0, 25.0]
[2, 4, 6, 8, 10]
[2, 4, 6, 8, 10]
[:a, 2, :a, 4, :a, 6, :a, 8, :a, 10]
%{0 => [12, 14], 1 => [11, 13, 15]}
[[1, 2], [3, 4]]
[[1, 2], [2, 3], [3, 4], [4, 5]]
[[1, 2], [4, 5]]
[[1, 2], [3], [4, 5]]
false
true
[3]
[1, 2, 4, 5]
[1, 2, 3]
[4, 5]
[1, 2, 3]
[4, 5, 1, 2, 3, 4, 5]
true
true
true
false
[1, 2, 3]
true
true
true
false
[4, 5, 1, 2, 3, 4, 5]
[1, 3, 5]
[{11, 0}, {12, 1}, {13, 2}, {14, 3}, {15, 4}]
1
[2, 5, 3]
[2, 4, {1}, {3}, {5}]
[1, 3]
[2, 3, 4]
[1, 4, 3, 2, 5]
13
13
13
2
true
true
{5, {4, {3, {2, 1}}}}
15
{[1, 2, 3, 4, 5], 6}
{[1, 4, 9, 16, 25], 6}
[{1, 99}, {2, {1, 99}}]
[
  {1, 99},
  {2, {1, 99}},
  {3, {2, {1, 99}}},
  {4, {3, {2, {1, 99}}}},
  {5, {4, {3, {2, {1, 99}}}}}
]

成果物

以上。

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