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

Live Coding: Simon Says Game in Kotlin

Live Coding: Simon Says Game in Kotlin

"Where do I start?" How many times have you asked yourself this when you are about to add a new functionality to your project?
Developers deal with daily decision problems that affect huge code bases and little time to come with solutions.
In this talk we will build the famous Simon Says game for Android to show how we can come up with a solution parting from requirements that are turned into very specific test cases.
Along the way we will see the advantages that Kotlin provides us and a small sample of the power that Arrow can add to your Android project incorporating functional programming.

Rocio Ortega Priego

December 21, 2019
Tweet

Other Decks in Programming

Transcript

  1. • Memory Skill game • Invented by Ralph H. Baer

    and Howard J Morrison in 1978. Some things about Simon Says Photo by Debaird: https://www.flickr.com/photos/debaird Simon Says Game - Android
  2. • Start Game! • 4 colours • Colour sequences Our

    Simon Says Version Photo by Debaird: https://www.flickr.com/photos/debaird Simon Says Game - Android
  3. Data Domain StartGameUseCase ColorSequenceRepository PositionRepository Presentation What we will do?

    GamePresenter GameFragment / View ValidateColorUseCase Simon Says Game - Android
  4. Coroutines The Kotlin team defines coroutines as “lightweight threads”. They

    are sort of tasks that the actual threads can execute. Simon Says Game - Android
  5. Launch public fun CoroutineScope.launch( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart

    = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit ): Job Coroutines Simon Says Game - Android
  6. withContext public suspend fun <T> withContext( context: CoroutineContext, block: suspend

    CoroutineScope.() -> T ): T Coroutines Simon Says Game - Android
  7. class GamePresenter() : CoroutineScope by MainScope() { fun onDetach() {

    cancel() } } Coroutines Simon Says Game - Android
  8. class GamePresenter() : CoroutineScope by MainScope() { fun onDetach() {

    cancel() } } @Suppress("FunctionName") public fun MainScope(): CoroutineScope = ContextScope(SupervisorJob() + Dispatchers.Main) Coroutines Simon Says Game - Android
  9. class GamePresenter() : CoroutineScope by MainScope() { fun onDummyFunction() {

    launch { val result = withContext(Dispatcher.IO) { doTakingLotOfTime() } doThingsInMainWith(result) } } } Coroutines Simon Says Game - Android
  10. A cold asynchronous data stream that sequentially emits values and

    completes normally or with an exception. Flow flow { colorSequence.forEach { value -> delay(delayTime) emit(value) } } Simon Says Game - Android
  11. Functional with Λrrow Option<A> Try<A> DataTypes Simon Says Game -

    Android data class Some<out A>(val a: A) : Option<T>() { override fun isEmpty() = false override fun toString(): String = “Some($a)" }
  12. Functional with Λrrow Option<A> Try<A> DataTypes Simon Says Game -

    Android data class Some<out T>(val t: T) : Option<T>() { override fun isEmpty() = false override fun toString(): String = "Some($t)" } object None : Option<Nothing>() { override fun isEmpty() = true override fun toString(): String = "None" }
  13. Validated<A,B> Functional with Λrrow Option<A> Try<A> DataTypes Simon Says Game

    - Android data class Valid<out B>(val b: B) : Validated<Nothing, B>()
  14. Validated<A,B> Functional with Λrrow Option<A> Try<A> DataTypes Simon Says Game

    - Android data class Valid<out B>(val b: B) : Validated<Nothing, B>() data class Invalid<out A>(val a: A) : Validated<A, Nothing>()
  15. Fold Functional with Λrrow Option<A> Try<A> DataTypes Validated<A,B> Simon Says

    Game - Android inline fun <R> fold(ifEmpty: () -> R, ifSome: (A) -> R): R = when (this) { is None -> ifEmpty() is Some<A> -> ifSome(t) }
  16. Fold Functional with Λrrow Option<A> Try<A> DataTypes Validated<A,B> Simon Says

    Game - Android inline fun <B> fold(fe: (E) -> B, fa: (A) -> B): B = when (this) { is Valid -> fa(a) is Invalid -> (fe(e)) }
  17. Fold fold({ /*failed block*/ },{ /*succeed block*/ }) Functional with

    Λrrow Option<A> Try<A> DataTypes Validated<A,B> Simon Says Game - Android
  18. Conclusion • Requirements into UnitTests • Coroutines in GamePresenter •

    Flow to emit Colors in Sequence Simon Says Game - Android
  19. Conclusion • Requirements into UnitTests • Coroutines in GamePresenter •

    Flow to emit Colors in Sequence • Arrow datatypes Simon Says Game - Android
  20. • Requirements into UnitTests • Coroutines in GamePresenter • Flow

    to emit Colors in Sequence • Arrow datatypes Conclusion Simon Says Game - Android