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.
The following example shows how the for into
operation is used in Elixir:
defmodule MyMod dodef mappify dofor {key, num} <- %{"a" => 1, "b" => 2, "c" => 3}, into: %{}, do: {key, num + num}enddef stringify dofor c <- [69, 100, 117, 99, 97, 116, 105, 118, 101], into: "", do: <<c>>endendIO.inspect MyMod.mappifyIO.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