# Functional Hangman in Kotlin with Arrow (part 2)

DevFeed: [Functional Hangman in Kotlin with Arrow (part 2)](<https://devfeed.tech/articles/functional-hangman-in-kotlin-with-arrow-part-2-25868.md>)

Original publisher: [Read original article](<http://lordraydenmk.github.io//2018/functional-hangman-in-kotlin-with-arrow-part-2/>)

Author: Stojan Anastasov

Published: 2018-12-25T00:00:00Z

Content type: tutorial

Language: en

Sources: [Stojan Anastasov's blog](<https://devfeed.tech/sources/stojan-anastasov-s-blog.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [IO](<https://devfeed.tech/topics/io.md>), [Error Handling](<https://devfeed.tech/topics/error-handling.md>)

Tags: [arrowkt](<https://devfeed.tech/tags/arrowkt.md>), [code](<https://devfeed.tech/tags/code.md>), [error-handling](<https://devfeed.tech/tags/error-handling.md>), [exception-handling](<https://devfeed.tech/tags/exception-handling.md>), [fp](<https://devfeed.tech/tags/fp.md>), [functional](<https://devfeed.tech/tags/functional.md>), [io](<https://devfeed.tech/tags/io.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>)

## AI overview

This tutorial explains the second part of converting a functional Hangman game from Scala with ZIO to Kotlin with Arrow. It replaces a hard-coded IO data type with a polymorphic design using Kind and introduces MonadDefer to represent capabilities such as lazy evaluation, exception handling, and completion with a result.

## Source excerpt

Converting a Functional Hangman game from Scala (ZIO) to Kotlin (with Arrow) was a nice exercise. I enjoyed working on it and I learned a lot. When I asked for feedback on the #arrow channel, one of the maintainers, Leandro had an interesting suggestion. Instead of hard-coding the data type IO I should try and make the program polymorphic and use Kind instead. That means writing the code focusing on the domain logic, using abstractions, and deferring the decision for the concrete data type like IO or Single (from RxJava) until the main function. The journey I was not familiar with that style of programming so I used this example from the excellent Arrow documentation as a guide. Writing to the the console In the previous article I used IO<A> to interact with the console. IO<A> represents an operation that can be executed lazily, fail with an exception (the exception is captured inside IO), run forever or return a single A. Let's take a look at the original implementation: fun putStrLn(line: String): IO<Unit> = IO { println(line) } // Usage in main() putStrLn("Hello world!").unsafeRunSync() putStrLn is a function that take a String and return a IO<Unit>. IO takes a lambda that is lazily evaluated at the end of the world, when we call unsafeRunSync(). If we want to achieve the same thing with Single we could use Single.fromCallable wrap our lambda and evaluate it in the main function when we call subscribe(). fun putStrLn(line: String): Single<Unit> = Single.fromCallable { println(line) } // Usage in main() putStrLn("Hello World").subscribe() Here bothIO and Single have something in common. A set of capabilities like: lazy evaluation, exception handling, and running forever or completing with a result of type A. IO and Single do a lot more, but for this use case, we want something as simple as possible that has the same capabilities. There is a type-class in Arrow that can do just that and it's called MonadDefer(more info). After a few iterations, and feedback from th