|
| 1 | +# Scala Fraglet Guide |
| 2 | + |
| 3 | +## Language Version |
| 4 | +Scala (installed via Coursier) |
| 5 | + |
| 6 | +## Execution Model |
| 7 | +- Compiled, then executed |
| 8 | +- Uses `scala run` to compile and execute in one step |
| 9 | +- Requires an object with a `main` method |
| 10 | +- Code executes when `main()` is called |
| 11 | +- Can define functions, classes, traits, and objects outside `main()` |
| 12 | + |
| 13 | +## Key Characteristics |
| 14 | +- JVM-based language (runs on Java) |
| 15 | +- Statically typed with type inference |
| 16 | +- Case-sensitive |
| 17 | +- Semicolons optional |
| 18 | +- Supports both object-oriented and functional programming |
| 19 | +- Immutable by default (val vs var) |
| 20 | +- Pattern matching |
| 21 | + |
| 22 | +## Fragment Authoring |
| 23 | +Write valid Scala statements or expressions. Code executes inside `main()`, so you can write statements directly. You can also define functions, classes, traits, and objects outside `main()` and call them from within. |
| 24 | + |
| 25 | +## Available Libraries |
| 26 | +- Standard Scala library |
| 27 | +- Java standard library (via JVM) |
| 28 | +- No additional dependencies pre-installed |
| 29 | + |
| 30 | +## Common Patterns |
| 31 | +- Print: `println("message")` |
| 32 | +- String interpolation: `s"Total: $count"` or `s"Total: ${expression}"` |
| 33 | +- Lists: `List(1, 2, 3).sum` |
| 34 | +- Functions: `def methodName(): Unit = { }` |
| 35 | +- Classes: `class MyClass { }` |
| 36 | +- Objects: `object MyObject { }` |
| 37 | +- Traits: `trait MyTrait { }` |
| 38 | +- Pattern matching: `x match { case 1 => ... }` |
| 39 | +- Immutable variables: `val x = 5` |
| 40 | +- Mutable variables: `var x = 5` |
| 41 | +- Higher-order functions: `list.map(x => x * 2)` |
| 42 | +- For comprehensions: `for (i <- 1 to 10) println(i)` |
| 43 | + |
| 44 | +## Examples |
| 45 | + |
| 46 | +```scala |
| 47 | +// Simple output |
| 48 | +println("Hello, World!") |
| 49 | +``` |
| 50 | + |
| 51 | +```scala |
| 52 | +// Function definition |
| 53 | +def greet(name: String): String = { |
| 54 | + s"Hello, $name!" |
| 55 | +} |
| 56 | + |
| 57 | +println(greet("Alice")) |
| 58 | +``` |
| 59 | + |
| 60 | +```scala |
| 61 | +// List processing |
| 62 | +val numbers = List(1, 2, 3, 4, 5) |
| 63 | +val squared = numbers.map(x => x * x) |
| 64 | +println(s"Sum of squares: ${squared.sum}") |
| 65 | +``` |
| 66 | + |
| 67 | +```scala |
| 68 | +// Higher-order function |
| 69 | +val multiply = (a: Int, b: Int) => a * b |
| 70 | +println(s"5 * 3 = ${multiply(5, 3)}") |
| 71 | +``` |
| 72 | + |
| 73 | +```scala |
| 74 | +// Class definition |
| 75 | +class Calculator { |
| 76 | + def add(a: Int, b: Int): Int = { |
| 77 | + a + b |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +val calc = new Calculator() |
| 82 | +println(s"10 + 20 = ${calc.add(10, 20)}") |
| 83 | +``` |
| 84 | + |
| 85 | +```scala |
| 86 | +// String interpolation |
| 87 | +val name = "Scala" |
| 88 | +val version = 3 |
| 89 | +println(s"Welcome to $name $version!") |
| 90 | +``` |
| 91 | + |
| 92 | +```scala |
| 93 | +// Case class |
| 94 | +case class Person(name: String, age: Int) |
| 95 | + |
| 96 | +val person = Person("Bob", 30) |
| 97 | +println(s"${person.name} is ${person.age} years old") |
| 98 | +``` |
| 99 | + |
| 100 | +```scala |
| 101 | +// Pattern matching |
| 102 | +val x = 5 |
| 103 | +val result = x match { |
| 104 | + case n if n < 0 => "negative" |
| 105 | + case 0 => "zero" |
| 106 | + case _ => "positive" |
| 107 | +} |
| 108 | +println(s"x is $result") |
| 109 | +``` |
| 110 | + |
| 111 | +```scala |
| 112 | +// For comprehension |
| 113 | +val numbers = List(1, 2, 3, 4, 5) |
| 114 | +val doubled = for (n <- numbers) yield n * 2 |
| 115 | +println(s"Doubled: ${doubled.mkString(", ")}") |
| 116 | +``` |
| 117 | + |
| 118 | +```scala |
| 119 | +// Option handling |
| 120 | +val name: Option[String] = Some("Scala") |
| 121 | +name.foreach(n => println(s"Name: $n")) |
| 122 | + |
| 123 | +val empty: Option[String] = None |
| 124 | +println(empty.getOrElse("Default value")) |
| 125 | +``` |
| 126 | + |
| 127 | +```scala |
| 128 | +// Extension methods (Scala 3) or implicit classes (Scala 2) |
| 129 | +implicit class StringOps(s: String) { |
| 130 | + def double(): String = s + s |
| 131 | +} |
| 132 | + |
| 133 | +val text = "Hello" |
| 134 | +println(text.double()) |
| 135 | +``` |
| 136 | + |
| 137 | +## Notes |
| 138 | +- Use `val` for immutable variables, `var` for mutable |
| 139 | +- Type inference works well, but explicit types can improve clarity |
| 140 | +- Pattern matching is powerful for control flow |
| 141 | +- Case classes provide convenient data structures |
| 142 | +- For comprehensions are syntactic sugar for map/flatMap/filter |
| 143 | +- Scala 2 and Scala 3 syntax may differ slightly |
0 commit comments