https://github.com/melling/scala
Scala Language Examples
https://github.com/melling/scala
Last synced: 18 days ago
JSON representation
Scala Language Examples
- Host: GitHub
- URL: https://github.com/melling/scala
- Owner: melling
- Created: 2010-12-29T03:48:36.000Z (over 15 years ago)
- Default Branch: master
- Last Pushed: 2019-12-04T03:25:56.000Z (over 6 years ago)
- Last Synced: 2025-02-23T01:35:37.560Z (over 1 year ago)
- Language: Scala
- Homepage:
- Size: 15.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Hello World
```scala
object HW extends App {
println("Hello world")
}
```
```sh
scalac foo.scala
scala HW
```
## Command Line Args
```scala
args.foreach { arg => println(arg) }
```
## val vs var
```scala
val x = 3 // Immutable
x = x + 1 // Illegal
var y = 4 // Mutable
y = y + 1 // OK
```
## Loops
```scala
val range = 0.until(10) // Range type
```
##
```scala
val l = List(1,2,3,4)
l.permutations.foreach(println)
```
##
```scala
def f = 3
def g = f _ // prevent eval of f()
def g() = f // Assign function
def g = f // error
```
## Case Classes
### Can omit val usage: class Noun(val word: String, val gender: String)
```scala
case class Noun(word: String, gender: String)
val w = Noun("hombre", "el")
println(w)
println(w.word)
println(w.gender)
```
## File I/O
```scala
val fileLines = io.Source.fromFile("README.org").getLines.toList
fileLines.foreach(println)
fileLines.foreach(l => println(l))
```