Scala typing test
These programs use Scala 3 and its optional braces: blocks are marked by indentation and keywords like then, do and match, and only one program needs curly brackets at all. What's left to type is @main, <- in for loops, => in case arms and a colon before every type.
What you'll type in Scala
@main defto mark the entry point.<-in loops likefor i <- 1 to 15 do.=>incasearms.:before types, as invalue: Int.[ ]for type parameters, as inList[Int]: Scala uses square brackets, not angle brackets.then,doandmatchin place of braces.
Example: FizzBuzz in Scala
No braces at all: if ... then ... else builds the result, and indentation does the rest. The indentation is filled in for you, so you only type the words and symbols.
@main def fizzBuzz(): Unit =
for i <- 1 to 15 do
val out =
if i % 15 == 0 then "FizzBuzz"
else if i % 3 == 0 then "Fizz"
else if i % 5 == 0 then "Buzz"
else i.toString
println(out)
Tips for typing Scala faster
<-and=>are both arrows, but they point different ways and start on different keys. Say "from" and "to" in your head as you type them.- Java and C# habits reach for angle brackets. In Scala, generic types use square brackets:
List[Int]. foldLeft(Leaf: Tree)(insert)in the tree program has two sets of brackets back to back. It's a good accuracy check.
All 20 Scala programs
Every program is real, working Scala. 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.