Skip to content

Instantly share code, notes, and snippets.

View bandrzejczak's full-sized avatar

Bartek Andrzejczak bandrzejczak

View GitHub Profile
String test = if(1 == 1) "a" else "b";
object ScalarTernaryTyped extends App{
class TernaryIfThenClause[X](c: => Boolean, tc: => X){
lazy val cond = c
lazy val thenClause = tc
}
implicit class TernaryAssoc(cond: => Boolean){
def ??[X](thenClause: => X) = new TernaryIfThenClause(cond, thenClause)
}
implicit class TernaryElseClause(elseClause: => Unit){
def ::(ifThenClause: TernaryIfThenClause) =
if(ifThenClause.cond) ifThenClause.thenClause else elseClause
}
implicit class TernaryAssoc(cond: => Boolean){
def ??(thenClause: => Unit) = new TernaryIfThenClause(cond, thenClause)
}
class TernaryIfThenClause(c: => Boolean, tc: => Unit){
lazy val cond = c
lazy val thenClause = tc
}
case class TernaryIfThenClause(c: => Boolean, tc: => Unit)
val list = 1 :: 2 :: 3 :: 4 :: Nil
object ScalarPuzzle extends App {
implicit class TernaryOps(cond: => Boolean) {
def ??(thenBlock: => Unit) = new {
def ::(elseBlock: => Unit): Unit =
if(cond) thenBlock else elseBlock
}
}
(1 + 1 == 2) ?? println("True") :: println("False")
Iteration no. Time[ms]
1 33656.406
2 16100.784
3 10734.691
4 13656.410
5 7297.651
6 7732.481
7 5081.496
8 3020.896
9 3126.210
Metrics Java C++ C++ optimized C++ off-heap C++ combined
Iterations 10 10 10 10 10
Min. iteration time [ms] 4030.221 7890.256 5136.895 6250.862 2656.214
Max. iteration time [ms] 9432.062 7941.776 5166.630 6286.141 2665.393
Avg. iteration time [ms] 6251.026 7910.600 5155.023 6267.842 2662.243