What is For Into in Elixir?

Into Operator

The Into operator in Elixir can return the result of list comprehensions as a different data structure. List comprehension is the technique of creating a list based on existing lists.

Usage

The following example shows how the for into operation is used in Elixir:

defmodule MyMod do
def mappify do
for {key, num} <- %{"a" => 1, "b" => 2, "c" => 3}, into: %{}, do: {key, num + num}
end
def stringify do
for c <- [69, 100, 117, 99, 97, 116, 105, 118, 101], into: "", do: <<c>>
end
end
IO.inspect MyMod.mappify
IO.puts MyMod.stringify

The Mappify function takes a map and returns the doubled values without changing the keys. The into: %{} part ensures that the result remains a map. Without it, the return value would be a list.

The Stringify function takes a list of integers and returns the ASCII representation of those integers as a single string. While this would work even without the into: "", it is a decent example of how strings can be returned from for-into commands.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved