On Github stanch / codebits2014
“Plumbing’s just Lego, innit? Water Lego...”
— Superhans, Peep Show
val x = 4
val x: Int = 4
def add(x: Int, y: Int) = x + y
def add(x: Int, y: Int): Int = { x + y }
(x: Int) ⇒ x + 3
x ⇒ x + 3
_ + 3
5.add(5) ⇔ 5 add 5
bird.♫("lala") ⇔ bird ♫ "lala"
say("Hi!") ⇔ say { "Hi!" }
Given a list of cities,
select those with a river,
take top-5 of
the most populated ones, and
put each of them on the map.
cities .filter(_.hasRiver) .sortBy(-_.population) .take(5) .foreach { c ⇒ addToMap(c.name, c.coordinates) }
Declare a property you need,
but do not call any APIs
before the UI is ready
lazy val title = getArguments.getString("title")
lazy val title = Option(getArguments) .flatMap(a ⇒ Option(a.getString("title"))) .getOrElse("Hello")
Run a difficult computation
outside the main thread
and do something with the result
when it’s ready.
val result = Future { val x = longComputation(41.5) x + 5 }
result onComplete { case Success(x) ⇒ doSomething(x) case Failure(t) ⇒ handle(t) }
“Don’t put off until tomorrow what you can do today”
— Benjamin Franklin
val fortyTwo = Future { ... } val fortyThree = fortyTwo.map(_ + 1) fortyThree onComplete { ... }
val euros = async { val future1 = getDollarsFromDb val future2 = googleConversionRate val d = await(future1) val r = await(future2) d * r }
Experimental modular functional UI language for Android, written in Scala.
w[Button]
l[LinearLayout]( w[Button], w[TextView] )
val brick1 = w[Button] val brick2 = w[TextView] l[LinearLayout]( brick1, brick2 )
w[TextView] <~ text("Hello")
def largeText(str: String) = text(str) + TextSize.large + padding(left = 8 dp) w[TextView] <~ largeText("Hello")
var greeting = slot[TextView] l[LinearLayout]( w[Button] <~ text("Greet me!") <~ On.click { greeting <~ show }, w[TextView] <~ text("Hello there") <~ wire(greeting) <~ hide )
val textStyle = widerThan(400 dp) ? TextSize.large | TextSize.medium w[TextView] <~ textStyle
? <~ ?
onlySeenInHD <~ (hdpi ? show)
List(textView1, textView2) <~ hide
val futureCaption = Future { Thread.sleep(5000) text("Hello") } myTextView <~ futureCaption
name := "Project-name" version := "1.0" platformTarget in Android := "android-19" libraryDependencies ++= Seq( "com.loopj.android" % "android-async-http" % "1.4.4", "com.android.support" % "support-v13" % "19.1.0", aar("com.google.android.gms" % "play-services" % "4.0.30") )
sbt > gen-idea
(live IDE demo)
↓
↓
This presentation: http://stanch.github.io/codebits2014