Getting Started
4 snippetsYour first steps with Scala basics
Hello World
@main def hello(): Unit =
println("Hello, World!")Comments
// Single line comment
/* Multi-line
comment */String Interpolation
val name = "Scala"
println(s"Hello, $name!") // Hello, Scala!User Input
import scala.io.StdIn
val input = StdIn.readLine("Enter: ")
println(s"You entered: $input")Variables & Data Types
7 snippetsStore and manage data in Scala
Val (Immutable)
val name = "John"Var (Mutable)
var count = 0Type Annotation
val age: Int = 25List
val nums = List(1, 2, 3)Map
val ages = Map("John" -> 30, "Jane" -> 25)Tuple
val pair = (1, "hello")Option
val name: Option[String] = Some("John")Operators
5 snippetsSymbols for calculations and comparisons
Arithmetic
5 + 3 // 8 (addition)
5 - 3 // 2 (subtraction)
5 * 3 // 15 (multiplication)
5 / 3 // 1 (integer division)
5 % 3 // 2 (modulus)Comparison
5 == 3 // false (equal)
5 != 3 // true (not equal)
5 > 3 // true (greater)
5 < 3 // false (less)
5 >= 3 // true (greater or equal)
5 <= 3 // false (less or equal)Logical
true && false // false (AND)
true || false // true (OR)
!true // false (NOT)Compound Assignment
var x = 5
x += 3 // x = 8
x -= 2 // x = 6
x *= 2 // x = 12
x /= 3 // x = 4String Operators
"Hello" + " World" // concatenation
"abc" * 3 // "abcabcabc"Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Loops
7 snippetsRepeat code efficiently with iterations
For Range
for (i <- 1 to 5) {
println(i) // 1, 2, 3, 4, 5
}For Until
for (i <- 0 until 5) {
println(i) // 0, 1, 2, 3, 4
}For Collection
val fruits = List("apple", "banana")
for (fruit <- fruits) {
println(fruit)
}For Yield
val doubled = for (n <- nums) yield n * 2While
var i = 0
while (i < 5) {
println(i)
i += 1
}Foreach
nums.foreach(n => println(n))
nums.foreach(println) // shorthandFor with Guard
for (i <- 1 to 10 if i % 2 == 0) {
println(i) // 2, 4, 6, 8, 10
}Control Flow
2 snippetsDirect program execution with conditions
If Expression
val max = if (a > b) a else bMatch
x match {
case 1 => "one"
case 2 | 3 => "two or three"
case _ => "other"
}Functions
5 snippetsReusable blocks of code with lambdas
Def
def greet(name: String): String = {
s"Hello, $name!"
}Short Form
def add(a: Int, b: Int) = a + bDefault Param
def greet(name: String = "World") = s"Hello, $name!"Lambda
val add = (a: Int, b: Int) => a + bCurrying
def add(a: Int)(b: Int) = a + bCollections
6 snippetsFunctional transformations on data
Map
nums.map(_ * 2)Filter
nums.filter(_ > 0)FlatMap
nums.flatMap(n => List(n, n * 2))Reduce
nums.reduce(_ + _)Fold
nums.foldLeft(0)(_ + _)GroupBy
people.groupBy(_.city)Classes & OOP
5 snippetsDefine custom types and objects
Class
class Dog(val name: String) {
def bark(): String = s"$name says woof!"
}Case Class
case class Person(name: String, age: Int)Object
object Singleton {
def doSomething(): Unit = {}
}Trait
trait Greetable {
def greet(): String
}With Trait
class Person extends Animal with GreetablePattern Matching
3 snippetsPowerful matching expressions
Case Class Match
person match {
case Person(name, age) => s"$name is $age"
case _ => "unknown"
}Option Match
opt match {
case Some(value) => value
case None => "default"
}Guard
x match {
case n if n > 0 => "positive"
case _ => "non-positive"
}