Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

elixir

What is For Into in Elixir?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

RELATED TAGS

elixir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring