Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How To Make A Better Sandwich

How To Make A Better Sandwich

This was a lightning talk I gave at Chicago Kotlin User Group on July 17th. It attempts to explain higher order functions through the analogy of sandwiches (borrowed from Ruby Koans). This will hopefully give new ideas for what is possible in Kotlin when it comes to DRYing up various parts of your codebase.

Cody Engel

July 17, 2018
Tweet

More Decks by Cody Engel

Other Decks in Programming

Transcript

  1. val startTime = System.currentTimeMillis() for (i in 0..100000) { println("Person:

    ${Person()}") } val endTime = System.currentTimeMillis() val runTime = endTime - startTime @POTUS404 Sandwich Code
  2. val startTime = System.currentTimeMillis() for (i in 0..100000) { println("Person:

    ${Person()}") } val endTime = System.currentTimeMillis() val runTime = endTime - startTime @POTUS404 Sandwich Code
  3. val startTime = System.currentTimeMillis() for (i in 0..100000) { println("Person:

    ${Person()}") } val endTime = System.currentTimeMillis() val runTime = endTime - startTime @POTUS404 Sandwich Code
  4. val startTime = System.currentTimeMillis() for (i in 0..100000) { println("Person:

    ${Person()}") } val endTime = System.currentTimeMillis() val runTime = endTime - startTime @POTUS404 Sandwich Code
  5. Higher Order Functions • Takes one or more functions as

    arguments. • Returns a function as a result.
  6. class Benchmark { fun benchmark(block: () -> Unit): Long {

    // assemble sandwich here } } @POTUS404 Higher Order Functions
  7. fun benchmark(block: () -> Unit): Long { val startTime =

    System.currentTimeMillis() block.invoke() return System.currentTimeMillis() - startTime } @POTUS404 Higher Order Functions
  8. fun benchmark(block: () -> Unit): Long { val startTime =

    System.currentTimeMillis() block.invoke() return System.currentTimeMillis() - startTime } @POTUS404 Higher Order Functions
  9. fun benchmark(block: () -> Unit): Long { val startTime =

    System.currentTimeMillis() block.invoke() return System.currentTimeMillis() - startTime } @POTUS404 Higher Order Functions
  10. val runtime = Benchmark().benchmark { for (i in 0..100000) {

    println("Person: ${Person()}") } } println("Executed in ${runtime}ms") @POTUS404 Higher Order Functions
  11. class Benchmark { fun benchmark(block: () -> Unit): Long {

    // assemble sandwich here } } @POTUS404 A Word About Keywords
  12. public final class Benchmark { public final long benchmark(@NotNull Function0

    block) { Intrinsics.checkParameterIsNotNull(block, "block"); long startTime = System.currentTimeMillis(); block.invoke(); return System.currentTimeMillis() - startTime; } } @POTUS404 A Word About Keywords
  13. val runtime = Benchmark().benchmark { for (i in 0..100000) {

    println("Person: ${Person()}") } } println("Executed in ${runtime}ms") @POTUS404 A Word About Keywords
  14. long runtime = (new Benchmark()).benchmark((Function0)null.INSTANCE); String var2 = "Executed in

    " + runtime + "ms"; System.out.println(var2); @POTUS404 A Word About Keywords
  15. class Benchmark { inline fun benchmark(block: () -> Unit): Long

    { // assemble sandwich here } } @POTUS404 A Word About Keywords
  16. public final class Benchmark { public final long benchmark(@NotNull Function0

    block) { Intrinsics.checkParameterIsNotNull(block, "block"); long startTime = System.currentTimeMillis(); block.invoke(); return System.currentTimeMillis() - startTime; } } @POTUS404 A Word About Keywords
  17. val runtime = Benchmark().benchmark { for (i in 0..100000) {

    println("Person: ${Person()}") } } println("Executed in ${runtime}ms") @POTUS404 A Word About Keywords
  18. new Benchmark(); long startTime$iv = System.currentTimeMillis(); int var5 = 0;

    for(int var6 = 100001; var5 < var6; ++var5) { String var7 = "Person: " + new Person((String)null, (String)null, 0, 7, (DefaultConstructorMarker)null); System.out.println(var7); } long runtime = System.currentTimeMillis() - startTime$iv; String var2 = "Executed in " + runtime + "ms"; System.out.println(var2); @POTUS404 A Word About Keywords
  19. new Benchmark(); long startTime$iv = System.currentTimeMillis(); int var5 = 0;

    for(int var6 = 100001; var5 < var6; ++var5) { String var7 = "Person: " + new Person((String)null, (String)null, 0, 7, (DefaultConstructorMarker)null); System.out.println(var7); } long runtime = System.currentTimeMillis() - startTime$iv; String var2 = "Executed in " + runtime + "ms"; System.out.println(var2); @POTUS404 long runtime = (new Benchmark()).benchmark((Function0 )null.INSTANCE); String var2 = "Executed in " + runtime + "ms"; System.out.println(var2); (Without Inline) (With Inline) A Word About Keywords
  20. // Timing.kt public inline fun measureTimeMillis(block: () -> Unit) :

    Long { val start = System.currentTimeMillis() block() return System.currentTimeMillis() - start } @POTUS404 Examples From IRL
  21. @Test fun `when download contact list info completes loading contacts

    should be false`() { expectDownloadContactInfoResponse(Response()) initViewModel() assertViewState { currentState -> assertThat(currentState.loadingContacts).isFalse() } } private fun assertViewState(state: (currentState: ContactsViewState) -> Unit) { assertThat(viewModel.viewState.value).isNotNull state.invoke(viewModel.viewState.value!!) } @POTUS404 Examples From IRL
  22. fun waitFor(precondition: () -> Boolean) { var waitingFor = 0L

    while (precondition.invoke() && waitingFor < 1000) { Thread.sleep(25) waitingFor += 25 } } @POTUS404 Examples From IRL
  23. class KeyboardObserver(private val rootLayout: View) { var onKeyboardDisplayed: () ->

    Unit = {} var onKeyboardHidden: () -> Unit = {} init { rootLayout.viewTreeObserver.addOnGlobalLayoutListener({ if (keyboardHeight > screenHeight * 0.15) { if (!keyboardDisplayed) { onKeyboardDisplayed.invoke() } } else { if (!keyboardHidden) { onKeyboardHidden.invoke() } } }) } } @POTUS404 Examples From IRL