From c3408fb74a8aba58549c027f43da6b32cbbec7f3 Mon Sep 17 00:00:00 2001 From: neingeist Date: Tue, 29 Nov 2016 19:36:24 +0100 Subject: [PATCH] add old scala exercises --- PipelineTest.scala | 18 ++++++++++++++++++ ScalaFXHello.scala | 23 +++++++++++++++++++++++ lazyvals.scala | 19 +++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 PipelineTest.scala create mode 100644 ScalaFXHello.scala create mode 100644 lazyvals.scala diff --git a/PipelineTest.scala b/PipelineTest.scala new file mode 100644 index 0000000..00f55ae --- /dev/null +++ b/PipelineTest.scala @@ -0,0 +1,18 @@ +object Pipelining { + implicit def toPipe[T](x : T) = new { def |> [U](f : T => U) = f(x) } +} + +object PipelineTest extends App { + + def minusTen(x: Int) = x-10 + + def timesTwo(x: Int) = x*2 + + assert(minusTen(timesTwo(3)) == -4) + + val f = timesTwo _ andThen minusTen _ + assert(f(3) == -4) + + val g = timesTwo compose minusTen + assert(g(3) == -4) +} diff --git a/ScalaFXHello.scala b/ScalaFXHello.scala new file mode 100644 index 0000000..c820599 --- /dev/null +++ b/ScalaFXHello.scala @@ -0,0 +1,23 @@ +import scalafx.Includes._ +import scalafx.application.JFXApp +import scalafx.scene.Scene +import scalafx.scene.paint.Color +import scalafx.scene.shape.Rectangle + +object ScalaFXHello extends JFXApp { + stage = new JFXApp.PrimaryStage { + title.value = "Hello Stage" + width = 600 + height = 450 + scene = new Scene { + fill = Color.LightGreen + content = new Rectangle { + x = 25 + y = 40 + width = 100 + height = 100 + fill <== when (hover) choose Color.Green otherwise Color.Red + } + } + } +} diff --git a/lazyvals.scala b/lazyvals.scala new file mode 100644 index 0000000..5dc9269 --- /dev/null +++ b/lazyvals.scala @@ -0,0 +1,19 @@ +// http://daily-scala.blogspot.de/2009/09/lazy-val.html + +object Lazyvals extends App { + val normalVal = { + println("---->>> Initializing normal val <<<----"); + "This is the normal val" + } + lazy val lazyVal = { + println("---->>> Initializing lazy val <<<----"); + "This is the lazy val" + } + println("\n\nno references have been made yet\n\n") + println("Accessing normal val : ") + println(normalVal) + println("\n\nAccessing lazy val : ") + println(lazyVal) + println("\n\nAccessing lazy val a second time, there should be no initialization now: ") + println(lazyVal) +} \ No newline at end of file