💻 Programming

Elixir

A functional, concurrent programming language

📖 Detailed Explanation

Elixir is a functional programming language created in 2011 by Jose Valim while he was working on the Ruby on Rails core team. It runs on the Erlang VM (BEAM), so it can directly leverage stability proven over several decades in fields that require high availability, such as telecommunications, finance, and IoT.

Elixir's core philosophy is 'scalability and maintainability.' It is based on immutable data and pure functions, and through the actor model it can run millions of lightweight processes concurrently. This is how WhatsApp was able to handle 2 million concurrent connections on a single server.

The Phoenix framework is Elixir's killer app and is specialized for developing real-time web applications. With LiveView, you can implement a real-time UI through server-side rendering without JavaScript, which greatly reduces front-end complexity.

In practice, Elixir is used at Discord (handling concurrent connections), Pinterest (API servers), PepsiCo (IoT platform), and others. Features such as pattern matching, the pipe operator (|>), and OTP (Open Telecom Platform) supervision trees automate failure recovery, realizing the "let it crash" philosophy.

💻 Code Example

# 1. 기본 문법과 패턴 매칭
defmodule Calculator do
  # 패턴 매칭으로 함수 오버로딩
  def add(a, b) when is_number(a) and is_number(b), do: a + b
  def add(a, b) when is_list(a) and is_list(b), do: a ++ b
  def add(_, _), do: {:error, "지원하지 않는 타입"}
end

# 파이프 연산자로 데이터 변환 체이닝
result = [1, 2, 3, 4, 5]
|> Enum.filter(&rem(&1, 2) == 0)  # 짝수 필터: [2, 4]
|> Enum.map(&(&1 * &1))           # 제곱: [4, 16]
|> Enum.sum()                     # 합계: 20

IO.puts("짝수 제곱의 합: #{result}")

# 2. 동시성 - GenServer 프로세스
defmodule Counter do
  use GenServer

  # 클라이언트 API
  def start_link(initial \\ 0), do: GenServer.start_link(__MODULE__, initial)
  def increment(pid), do: GenServer.call(pid, :increment)
  def get(pid), do: GenServer.call(pid, :get)

  # 서버 콜백
  @impl true
  def init(count), do: {:ok, count}

  @impl true
  def handle_call(:increment, _from, count), do: {:reply, count + 1, count + 1}
  def handle_call(:get, _from, count), do: {:reply, count, count}
end

# 3. Phoenix LiveView 예시 (실시간 웹)
defmodule MyAppWeb.CounterLive do
  use Phoenix.LiveView

  def mount(_params, _session, socket) do
    {:ok, assign(socket, count: 0)}
  end

  def handle_event("increment", _, socket) do
    {:noreply, update(socket, :count, &(&1 + 1))}
  end

  def render(assigns) do
    ~H"""
    <button phx-click="increment">
      클릭 수: <%= @count %>
    </button>
    """
  end
end

🗣️ Say It Like This in Practice

💬 In a Meeting
"I think we should build the real-time notification system with Elixir Phoenix. With LiveView, we can build a real-time UI without managing WebSockets, and thanks to the BEAM VM, we can reliably handle hundreds of thousands of concurrent connections."
💬 In an Interview
"Elixir's advantage is fault isolation through OTP supervision trees. When a process crashes, the supervisor automatically restarts it, so a single bug does not affect the entire system. This is the heart of the 'let it crash' philosophy."
💬 In a code review
"I think using pattern matching instead of if-else here would make things clearer. If you handle each state explicitly with a case statement, the compiler will even warn you about missing cases."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
= is matching, not assignment

In Elixir, = is the pattern matching operator. After `x = 1`, `1 = x` succeeds, but `2 = x` raises a MatchError. If you want to rebind a variable, use the ^ pin operator.

❌
Data is immutable

When you "modify" a list or map, new data is always returned. You must bind the result to a new variable, as in `list = [1, 2] |> List.insert_at(0, 0)`.

✅
Using the pipe operator (|>)

Instead of `String.upcase(String.trim(" hello "))`, writing `" hello " |> String.trim() |> String.upcase()` lets you read the data flow from left to right, which improves readability.

🔗 Related Terms

📚 Learn More