Elixir typing test
Elixir runs on the Erlang virtual machine and borrows Ruby's friendly do ... end blocks. What makes it distinctive to type is the pipe operator |>, functions defined by pattern matching, tuples in braces and the & capture syntax, as in &Tree.insert(&2, &1).
What you'll type in Elixir
|>to pass a value through a chain of functions.do ... endblocks, and, do:one-liners likedef in_order(nil), do: [].->incondclauses.{ }for tuples, as in{left, v, right}.&,&1and&2for short anonymous functions.defmoduleanddefto define modules and functions.
Example: FizzBuzz in Elixir
cond do checks each condition in turn, with true -> as the catch-all at the end. Elixir uses rem(i, 15) rather than a % operator.
for i <- 1..15 do
cond do
rem(i, 15) == 0 -> IO.puts("FizzBuzz")
rem(i, 3) == 0 -> IO.puts("Fizz")
rem(i, 5) == 0 -> IO.puts("Buzz")
true -> IO.puts(i)
end
end
Tips for typing Elixir faster
|>is Shift + backslash, then Shift + full stop. Keep Shift held for both.- Watch the comma before
do:in one-line functions. It's easy to drop. - The binary search tree program defines
insertthree times, once for each pattern. The lines look alike, so read each one before you type it.
All 20 Elixir programs
Every program is real, working Elixir. Choose one to start typing it straight away.
Beginner
- Hello, World!Print a greeting.
- Count to fivePrint the numbers 1 to 5.
- Celsius to FahrenheitConvert 100 °C to Fahrenheit.
- Sum a listAdd up 3, 1, 4, 1, 5.
- Find the largestFind the biggest number in a list.
- Count vowelsCount the vowels in "keyboard".
Intermediate
- Reverse a stringTurn "hello" into "olleh".
- FizzBuzzFizz for 3s, Buzz for 5s, 1 to 15.
- FactorialA recursive function, called with 5.
- FibonacciPrint the first ten Fibonacci numbers.
- Greatest common divisorEuclid's algorithm for 48 and 18.
Advanced
- Primes to 20Print every prime up to 20.
- PalindromeCheck whether "racecar" reads the same backwards.
- Word countCount the words in a sentence.
- Bubble sortSort 5, 2, 9, 1, 7 by swapping neighbours.
- Binary searchFind 7 in a sorted list by halving.
Expert
- QuicksortSort a list by partitioning around a pivot.
- Binary search treeBuild a tree, then read the values back in order.
- Shortest path (BFS)Breadth-first search through a small graph.
- N-QueensCount the ways to place 6 queens so none attack.
How the test works
The timer starts on your first key and stops once every character is right. Press Enter for a new line and the indentation is filled in for you. A wrong key shows in red and counts as a mistake; press Backspace to go back and fix it. Your best time for each program is saved in your browser. Read more about how speed and accuracy are calculated, or get tips for typing code faster.