LoginSignup
3
3

More than 5 years have passed since last update.

Elixir Comprehensions(内包表記) into

Last updated at Posted at 2014-10-15

Elixir Comprehensions(内包表記) into

概要

Elixir の Comprehensions(内包表記) into について。

Comprehensions(内包表記)の結果を別の型で持たせたい場合に、into を利用します。

into に指定するには Collectable protocol を実装している必要があり、
List / BitString / Map / HashDict / HashSet / IO(IO.Stream) が指定可能。

サンプル

サンプルコード

nums = 1..100 |> Enum.take(50)
ret = for num <- nums, into: [9999] do
  num * 3
end
IO.puts "nums = "
IO.inspect nums
IO.puts "ret = "
IO.inspect ret

ret = for num <- nums, into: "" do
  "#{num * 3}"
end

IO.puts "ret = "
IO.inspect ret
  • 出力
nums =
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
 43, 44, 45, 46, 47, 48, 49, 50]
ret =
[9999, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57,
 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99, 102, 105, 108, 111,
 114, 117, 120, 123, 126, 129, 132, 135, 138, 141, 144, 147, ...]
 ret =
"369121518212427303336394245485154576063666972757881848790939699102105108111114117120123126129132135138141144147150"

参照

http://elixir-lang.org/docs/stable/elixir/Collectable.html
http://elixir-lang.org/docs/stable/elixir/List.html
http://elixir-lang.org/docs/stable/elixir/Map.html
http://elixir-lang.org/docs/stable/elixir/HashDict.html
http://elixir-lang.org/docs/stable/elixir/HashSet.html
http://elixir-lang.org/docs/stable/elixir/IO.html
http://elixir-lang.org/getting_started/18.html#18.2-bitstring-generators

3
3
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
3
3