Skip to content

Instantly share code, notes, and snippets.

@gervaisb
Last active November 29, 2017 08:20
Show Gist options
  • Save gervaisb/369fa367ec6ccbd5baf1dbd630ce5f3b to your computer and use it in GitHub Desktop.
Save gervaisb/369fa367ec6ccbd5baf1dbd630ce5f3b to your computer and use it in GitHub Desktop.
Kotlin idioms translated to Scala

An attempt to compare Kotlin with Scala. Sometimes the comparison is biased because Scala is a functionnal language.

import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
// Fixtures
val name = "Me"
val x:Any = ""
val value = 42
data class Foo(val v:Int)
data class Bar(val v:Int)
fun idioms() {
// Read-only list
val list = listOf("a", "b", "c")
// Mutable map
val map = mutableMapOf("a" to 1, "b" to 2, "c" to 3)
// Accessing a map
println(map["key"])
map["key"] = value
// Default values for functions parameters
fun foo(a: Int = 0, b: String = "") {
// ...
}
// Filtering a list
val positives = list.filter { it > 0 }
// String interpolation
println("Name $name")
// Instance checks
/* When is an expression that must returns something */
when (x) {
is Foo -> 1// ...
is Bar -> 2// ...
else -> 3// ...
}
// Traversing a map/list of pairs
for ((k, v) in map) {
println("$k -> $v")
}
// Using ranges
for (i in 1..100) { /* closed range: includes 100*/ }
for (i in 1 until 100) { /* half-open range: does not include 100*/ }
for (x in 2..10 step 2) { /**/ }
for (x in 10 downTo 1) { /**/ }
if (x in 1..10) { /**/ }
// Lazy property
val p by lazy {
// compute the string
name
}
// Extensions Functions
fun String.spaceToCamelCase() {
toString()
}
"Convert this to camel case".spaceToCamelCase()
// If not null shorthand
val files = File("Test").listFiles()
println(files?.size)
// If not null and else shorthand
println(files?.size ?: "empty")
// Executing a statement if null
val email = map["email"] ?: throw IllegalStateException("Email is missing!")
// Execute if not null
val maybe = null
maybe?.let {
// Execute this block if not null
}
// Map nullable value if not null
val mapped = maybe?.let {
// Transform
} ?: "Default"
// Return on when statement
fun transform(color: String): Int =when (color) {
"Red" -> 0
"Green" -> 1
"Blue" -> 2
else -> throw IllegalArgumentException("Invalid color param value")
}
// 'try/catch' expression
fun test() {
val result = try {
// something risky
} catch (e: ArithmeticException) {
throw IllegalStateException(e)
}
}
// 'if' expression
fun foo(param: Int) {
val result = if (param == 1) {
"one"
} else if (param == 2) {
"two"
} else {
"three"
}
}
// Builder-style usage of methods that return Unit
fun arrayOfMinusOnes(size: Int): IntArray {
return kotlin.IntArray(size).apply { fill(-1) }
}
// Single-expression functions
fun theAnswer() = 42
// Calling multiple methods on an object instance ('with')
class Turtle {
fun penDown() {}
fun penUp() {}
}
val turtle = Turtle()
with (turtle) {
penDown()
penUp()
}
// Java 7's try with resources
val stream = Files.newInputStream(Paths.get("/some/file.txt"))
stream.buffered().reader().use { reader ->
println(reader.readText())
}
}
// Creating DTOs
data class Customer(val name: String, val email: String)
// Creating a singleton
object Resource {
val name = "Name"
}
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
// Fixtures
val name = "Me"
val x:Any = ""
val value = 42
case class Foo(v:Int)
case class Bar(v:Int)
def idioms() {
// Read-only list
val list = List("a", "b", "c")
// Mutable map
val map = collection.mutable.Map("a" -> 1, "b" -> 2, "c" -> 3)
// Accessing a map
println(map("key"))
map("key") = value
// Default values for functions parameters
def foo(a: Int = 0, b: String = "") {
// ...
}
// Filtering a list
val positives = list.filter(_ > 0)
// String interpolation
println(s"Name $name")
// Instance checks
/* Match can extract case class attributes */
x match {
case Foo => // ..
case Bar => // ..
case _ => // ..
}
// Traversing a map/list of pairs
for ((k, v) <- map) {
println(s"$k -> $v")
}
// Using ranges
for (i <- 1 to 100) { /* closed range: includes 100*/ }
for (i <- 1 until 100) { /* half-open range: does not include 100*/ }
for (x <- 2 to 10 by 2) { /**/ }
for (x <- 10 to 1 by -1) { /**/ }
if (1 to 10 contains (x)) { /**/ }
// Lazy property
lazy val p = {
// compute the string
name
}
// Extensions Functions
case class BetterString(val str: String) {
def spaceToCamelCase() = {
str.toString
}
}
implicit def stringToBetterString(s: String) =
BetterString(s)
"Convert this to camel case".spaceToCamelCase()
// If not null shorthand
val files = Option(new File("Test").listFiles())
files.foreach(fs => println(fs.length))
// If not null and else shorthand
println(files.map(_.length).getOrElse("empty"))
// Executing a statement if null
/* Scala way is to use `Try` or `Either` */
val email = map.get("email") match {
case None => throw new IllegalStateException("Email is missing!")
}
// Execute if not null
val maybe = Option("")
maybe.foreach { s =>
// Execute this block if not null
}
// Map nullable value if not null
maybe.map { s =>
// Transform
}.getOrElse("Default")
// Return on when statement
def transform(color: String): Int = color match {
case "Red" => 0
case "Green" => 1
case "Blue" => 2
case _ => throw new IllegalArgumentException("Invalid color parameter")
}
// 'try/catch' expression
def test(): Unit = {
val result = try {
// something risky
} catch {
case e:ArithmeticException => throw new IllegalStateException(e)
}
}
// 'if' expression
def foo(param: Int) {
val result = if (param == 1) {
"one"
} else if (param == 2) {
"two"
} else {
"three"
}
}
// Builder-style usage of methods that return Unit
/* Not a builder-style, the `fill` methods exists as static */
def arrayOfMinusOnes(size: Int): Array[Int] =
Array.fill(5)(-1)
// Single-expression functions
def theAnswer() = 42
// Calling multiple methods on an object instance ('with')
/* No equivalent, `Some` or `for` can be used. */
class Turtle {
def penDown() {}
def penUp() {}
}
val turtle = new Turtle()
Some(turtle).foreach { t =>
t.penDown()
t.penUp()
}
// Java 7's try with resources
/*
* No equivalent
*/
}
// Creating DTOs
case class Customer(name: String, email: String)
// Creating a singleton
object Resource {
val name = "Name"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment