Skip to content

Instantly share code, notes, and snippets.

@nomisRev
Created August 27, 2021 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nomisRev/6eb934335c284bbc54d906193e713a0d to your computer and use it in GitHub Desktop.
Save nomisRev/6eb934335c284bbc54d906193e713a0d to your computer and use it in GitHub Desktop.
Collection of small explanations I gave on Slack.

In Kotlin there is the concept of “co-pure”. The Kotlin compiler has a powerful inliner, which is very commonly used in Kotlin. An inliner takes the code from an inline fun, and puts it in the place of the call-site. So if an inline fun is pure in it’s definition, and it takes a lambda then the resulting code is pure if and only if the passed lambda is also pure. Otherwise the resulting code is impure. So the whether the resulting code pure or not depends 100% on the passed lambda.

fun pureExample(): Unit = 
  listOf(1, 2, 3).map { it + 1 } // [2, 3, 4] & pure

suspend fun impureExample(): Unit =
   listOf(1, 2, 3).map { delay(it * 100) } //[Unit, Unit, Unit] & impure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment