Learn Elixir - 9 Code Examples & CST Typing Practice Test
Elixir is a functional, concurrent, fault-tolerant programming language built on the Erlang VM (BEAM). It enables highly scalable, distributed, and resilient applications - ideal for real-time systems, telecom-grade reliability, and web applications via Phoenix.
Learn ELIXIR with Real Code Examples
Updated Nov 18, 2025
Code Sample Descriptions
Elixir Counter and Theme Toggle
defmodule Counter do
def run do
count = 0
is_dark = false
update_ui = fn ->
IO.puts("Counter: #{count}")
IO.puts("Theme: #{if is_dark, do: \"Dark\", else: \"Light\"}")
end
increment = fn ->
count = count + 1
update_ui.()
end
decrement = fn ->
count = count - 1
update_ui.()
end
reset = fn ->
count = 0
update_ui.()
end
toggle_theme = fn ->
is_dark = !is_dark
update_ui.()
end
# Simulate actions
update_ui.()
increment.()
increment.()
toggle_theme.()
decrement.()
reset.()
end
end
Counter.run()
Demonstrates a simple counter with theme toggling using Elixir variables and console output.
Elixir Simple Calculator
add = fn a, b -> a + b end
subtract = fn a, b -> a - b end
multiply = fn a, b -> a * b end
divide = fn a, b -> a / b end
IO.puts("Add 5 + 3: #{add.(5,3)}")
IO.puts("Subtract 5 - 3: #{subtract.(5,3)}")
IO.puts("Multiply 5 * 3: #{multiply.(5,3)}")
IO.puts("Divide 6 / 2: #{divide.(6,2)}")
Demonstrates addition, subtraction, multiplication, and division with anonymous functions.
Elixir FizzBuzz
Enum.each(1..15, fn n ->
IO.puts cond do
nrem(n, 15) == 0 -> "FizzBuzz"
nrem(n, 3) == 0 -> "Fizz"
nrem(n, 5) == 0 -> "Buzz"
true -> Integer.to_string(n)
end
end)
Classic FizzBuzz problem using pattern matching and anonymous functions.
Elixir Factorial
factorial = fn
0 -> 1
n -> n * factorial.(n - 1)
end
IO.puts("Factorial of 5: #{factorial.(5)}")
Recursive factorial function in Elixir.
Elixir Fibonacci
fibonacci = fn
0 -> 0
1 -> 1
n -> fibonacci.(n-1) + fibonacci.(n-2)
end
Enum.each(0..9, fn n -> IO.puts(fibonacci.(n)) end)
Recursive Fibonacci sequence generator.
Elixir List Comprehension
numbers = 1..10
squares = for n <- numbers, rem(n, 2) == 0, do: n * n
IO.inspect(squares)
Using list comprehension to square even numbers.
Elixir Map Filtering
scores = %{alice: 10, bob: 5, charlie: 12}
high_scores = for {k, v} <- scores, v >= 10, into: %{}, do: {k, v}
IO.inspect(high_scores)
Filtering a map based on value conditions.
Elixir Anonymous Functions with Capture
add = &(&1 + &2)
IO.puts(add.(3, 7))
Using the capture operator to shorten anonymous functions.
Elixir Reduce Example
numbers = [1,2,3,4,5]
sum = Enum.reduce(numbers, 0, fn x, acc -> x + acc end)
IO.puts(sum)
Summing a list of numbers using Enum.reduce.
Frequently Asked Questions about Elixir
What is Elixir?
Elixir is a functional, concurrent, fault-tolerant programming language built on the Erlang VM (BEAM). It enables highly scalable, distributed, and resilient applications - ideal for real-time systems, telecom-grade reliability, and web applications via Phoenix.
What are the primary use cases for Elixir?
Highly concurrent real-time systems. Scalable web backends (Phoenix). Distributed and fault-tolerant applications. Event-driven architectures. Messaging systems. Telecom-level resilience workloads. IoT and device message brokers
What are the strengths of Elixir?
Massively scalable. Extremely fault tolerant. Low-latency real-time features. Easy concurrency model. Rock-solid reliability from BEAM
What are the limitations of Elixir?
Smaller ecosystem compared to Python/JS. Learning curve for FP + OTP. Not ideal for heavy numerical computing. Limited mobile/desktop tooling. Requires BEAM mental model
How can I practice Elixir typing speed?
CodeSpeedTest offers 9+ real Elixir code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.