val nameList = listOf("bob", "carol", "dave") Mapping maybeName?.let { it.toUpperCase() } nameList.map { it.toUpperCase() } Filtering maybeName?.let { if ("a" in it) it else null } nameList.filter { "a" in it }
it sealed class Shape { class Rect(val p1: Point, val p2: Point) : Shape() class Circle(val centre: Point, val radius: Double) : Shape() } Shape s = getTheShape() val area = when(s) { is Shape.Rect -> abs(s.p2.x - s.p1.x) * abs(s.p2.y - s.p1.y) is Shape.Circle -> PI * s.radius * s.radius } fun Shape.area(): Double = when (this) { is Shape.Rect -> abs(p2.x - p1.x) * abs(p2.y - p1.y) is Shape.Circle -> PI * radius * radius }
{ val callable = object: Callable<Unit> { override fun call() { println("Doing a thing") } } assertEquals(Unit, callable.call()) } @Test fun `and even better as a lambda`() { val callable = Callable<Unit> { println("Doing a thing") } assertEquals(Unit, callable.call()) }
name = "Alice" } val bob = object { val name = "Bob" } fun example() { val carol = object { val name = "Carol" } assert( alice.name == "Alice" ) assert( bob.name == "Bob" ) assert( carol.name == "Carol" ) } Error: Unresolved reference: name
named James", want = "to know things are running") { val james = actorNamed("James") @Test fun `The server is running`() { given (james) { loadsTheHomePage() } then (james) { shouldSee(::`the page title`, equalTo("Journal")) } } @Test fun `Search returns some results`() { given (james) { loadsTheHomePage() } then (james) { shouldSee(::`the results table`, isntThere) } wheN (james) { searchesForTitle("fred") } then (james) { shouldSee(::`the results table`, hasSomeContent) } } }