Scala
2004fragletMCP + fragletc
Scalable + Language = Scala. Scala is a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages. It is also fully interoperable with Java so that libraries written in either language may be referenced directly in Scala or Java code, giving Scala programmers access to the full breadth of Java libraries.
Hello World
#!/usr/bin/env sh
cd /hello-world
scala Main.scala "$@"Coding Guide
Language Version
Scala 3 (Dotty), JVM-based
Execution Model
- The container runs your fraglet as a Scala script:
scala Main.scala [args...] - Your fraglet is the entire runnable object: it replaces the whole
object Main { ... }between the injection markers - Use
object Mainwithdef main(args: Array[String]): Unit— conventional, and you getargsfor free - Stdin is available via
scala.io.Source.stdin; arguments are inargs
Fragment Authoring
Write a complete object Main with a main method. The fraglet is not “snippet inside main” — it is the full object. That gives you:
- Arguments:
args: Array[String]inmain(args: Array[String])— same as any JVM main - Stdin:
scala.io.Source.stdin.getLines()orscala.io.Source.stdin.mkString - Full freedom to define classes, traits, functions, and top-level logic inside the object
You can name the object something else (e.g. object App) if you prefer; Main is recommended so examples and tooling stay consistent.
Stdin and Args (for AI / scripts)
- Read stdin:
scala.io.Source.stdin.getLines().foreach(println)or process lines as needed - Use args:
args.foreach(println)orargs.mkString(" ")— they are passed through from the fraglet invocation
Key Characteristics
- JVM-based, statically typed, type inference
- Case-sensitive; semicolons optional
- Immutable by default (
valvsvar) - Pattern matching, case classes, for comprehensions
Common Patterns
- Print:
println("message") - String interpolation:
s"Total: $count"ors"Total: ${expression}" - Args:
args.mkString(" ")orargs.toList - Stdin:
scala.io.Source.stdin.getLines().toList - Lists:
List(1, 2, 3).sum - Pattern matching:
x match { case 1 => ... }
Examples
Each example below is a full object you can use as the fraglet as-is.
object Main {
def main(args: Array[String]): Unit = {
println("Hello, World!")
}
}object Main {
def main(args: Array[String]): Unit = {
def greet(name: String): String = s"Hello, $name!"
println(greet("Alice"))
}
}object Main {
def main(args: Array[String]): Unit = {
val numbers = List(1, 2, 3, 4, 5)
val squared = numbers.map(x => x * x)
println(s"Sum of squares: ${squared.sum}")
}
}object Main {
def main(args: Array[String]): Unit = {
val multiply = (a: Int, b: Int) => a * b
println(s"5 * 3 = ${multiply(5, 3)}")
}
}object Main {
def main(args: Array[String]): Unit = {
class Calculator {
def add(a: Int, b: Int): Int = a + b
}
val calc = new Calculator()
println(s"10 + 20 = ${calc.add(10, 20)}")
}
}object Main {
def main(args: Array[String]): Unit = {
// Command-line arguments
println("Args: " + args.mkString(" "))
}
}object Main {
def main(args: Array[String]): Unit = {
// Stdin (e.g. echo "hello" | fragletc --vein=scala script.scala)
scala.io.Source.stdin.getLines().foreach(line => println(line.toUpperCase))
}
}object Main {
def main(args: Array[String]): Unit = {
val name = "Scala"
val version = 3
println(s"Welcome to $name $version!")
}
}object Main {
def main(args: Array[String]): Unit = {
case class Person(name: String, age: Int)
val person = Person("Bob", 30)
println(s"${person.name} is ${person.age} years old")
}
}object Main {
def main(args: Array[String]): Unit = {
val x = 5
val result = x match {
case n if n < 0 => "negative"
case 0 => "zero"
case _ => "positive"
}
println(s"x is $result")
}
}object Main {
def main(args: Array[String]): Unit = {
val numbers = List(1, 2, 3, 4, 5)
val doubled = for (n <- numbers) yield n * 2
println(s"Doubled: ${doubled.mkString(", ")}")
}
}Notes
- Use
object Mainanddef main(args: Array[String]): Unitfor a standard entry point argsand stdin are available; no extra setup needed- Use
valfor immutables,varonly when needed - Scala 3 syntax; some Scala 2 constructs (e.g. implicit class) still work
Fraglet Scripts
Echo Args
#!/usr/bin/env -S fragletc --vein=scala
object Main {
def main(args: Array[String]): Unit = {
println("Args: " + args.mkString(" "))
}
}Stdin Upper
#!/usr/bin/env -S fragletc --vein=scala
object Main {
def main(args: Array[String]): Unit = {
scala.io.Source.stdin.getLines().foreach(line => println(line.toUpperCase))
}
}Test
#!/usr/bin/env -S fragletc --vein=scala
object Main {
def main(args: Array[String]): Unit = {
println("Hello World!")
}
}