Skip to content

Instantly share code, notes, and snippets.

View taitruong's full-sized avatar

mr-t | Ark Protocol taitruong

View GitHub Profile
@taitruong
taitruong / gist:9393266
Last active August 29, 2015 13:57
Simple Scala Exampe: "Hello, Scala".toList.foreach(print(_)); println
val list = "Hello, Scala".toList //> list : List[Char] = List(H, e, l, l, o, ,, , S, c, a, l, a)
list.foreach(print(_)) //> Hello, Scala
println //>
@taitruong
taitruong / gist:9465189
Last active August 29, 2015 13:57
infix, prefix, and postfix operators
object ScalaHackSession {
//method calls instead of infix operators
(1).+(1) //> res0: Int(2) = 2
1. + (1) //> res1: Double(2.0) = 2.0
"Hello, Scala".indexOf("a") //> res2: Int = 9
"Hello, Scala".indexOf("a", 10) //> res3: Int = 11
//method calls instead of unary prefix operators
true.unary_! //> res4: Boolean = false
(1 - 2).unary_+ //> res5: Int = -1
@taitruong
taitruong / gist:9465605
Last active August 29, 2015 13:57
val and var - the good and the the ugly
object ScalaHackSession {
val immutable: String = "Hello, Scala" //> immutable : String = Hello, Scala
var mutable = immutable //> mutable : String = Hello, Scala
mutable = "Hello, Hacker"
println(mutable) //> Hello, Hacker
}
@taitruong
taitruong / gist:9465709
Last active August 29, 2015 13:57
Variable scope: inner / outer
object ScalaHackSession {
val i = 0 //> i : Int = 0
val scope = "outer scope" //> scope : String = outer scope
if (i == 0) {
val scope = "inner scope"
println(scope + ": " + i)
} //> inner scope: 0
}
@taitruong
taitruong / gist:9466025
Last active August 29, 2015 13:57
one line operator, 1
object ScalaHackSession {
var a = 0 //> a : Int = 0
if (a < 10) a = a + 1
println(a) //> 1
if (a < 10) a = a +
1
println(a) //> 2
}
@taitruong
taitruong / gist:9466102
Created March 10, 2014 14:36
two line operator, 2
object ScalaHackSession {
var a = 0 //> a : Int = 0
if (a < 10) a = a + 1
println(a) //> 1
if (a < 10) a = a
+ 1 //> res0: Int = 1
println(a) //> 1
}
@taitruong
taitruong / gist:9466336
Last active August 29, 2015 13:57
two line operator, parenthesis, 3
object ScalaHackSession {
var a = 0 //> a : Int = 0
if (a < 10) a = a + 1
println(a) //> 1
if (a < 10) a = (a
+ 1)
println(a) //> 2
}
@taitruong
taitruong / gist:9466441
Last active August 29, 2015 13:57
two line operator, body in curled braces, 4
object ScalaHackSession {
var a = 0 //> a : Int = 0
if (a < 10) a = a + 1
println(a) //> 1
if (a < 10) {
a = a + 1
}
println(a) //> 2
}
@taitruong
taitruong / gist:9466514
Created March 10, 2014 14:55
two line operator, expression in curled braces, 5
object ScalaHackSession {
var a = 0 //> a : Int = 0
if (a < 10) a = a + 1
println(a) //> 1
if (a < 10) a = {
println("before: " + a)
a + 1
} //> before: 1
println("after: " + a) //> after: 2
@taitruong
taitruong / gist:9466924
Created March 10, 2014 15:17
class, definition, new
object ScalaHackSession {
class ScalaFun
new ScalaFun() //> res0: ScalaHackSession.ScalaFun = ScalaHackSession$ScalaFun@1f7ebc89
new ScalaFun //> res1: ScalaHackSession.ScalaFun = ScalaHackSession$ScalaFun@b886fb3
}