Skip to content

Instantly share code, notes, and snippets.

@raphait
Created June 21, 2018 22:44
Show Gist options
  • Save raphait/c4534d8cbcf2e045925bf6fbc23bc02e to your computer and use it in GitHub Desktop.
Save raphait/c4534d8cbcf2e045925bf6fbc23bc02e to your computer and use it in GitHub Desktop.
Kotlin solution for Functional Kubs June 2018 version 2 - https://www.meetup.com/FunctionalKubs/events/251727590/
package kubs
const val START = 0 // static final in Java for primitive
const val END = 4
fun main(vararg args: String) {
//val is immutable like final in Java
val start = setOf( // syntax sugar and immutable ish (read-only cause is a Java Collection)
Pair(0, 1), Pair(0, 3),
Pair(1, 1), Pair(1, 2), Pair(1, 3), Pair(1, 4),
Pair(2, 0), Pair(2, 1), Pair(2, 3), Pair(2, 4),
Pair(3, 0), Pair(3, 1), Pair(3, 2), Pair(3, 3), Pair(3, 4),
Pair(4, 0), Pair(4, 1), Pair(4, 2), Pair(4, 3), Pair(4, 4)
)
//argument names
play(initialGame = start, newEntries = game)
}
fun play(initialGame: Set<Pair<Int, Int>>, newEntries: List<Pair<Int, Int>>): Set<Pair<Int, Int>> {
//scope function
fun toggle(light: Pair<Int, Int>) =
with(light) { // used to avoid repetitions of light
setOf(
this,
Pair(first - 1, second),
Pair(first, second - 1),
Pair(first + 1, second),
Pair(first, second + 1)
)
}.filter { it.first in START..END && it.second in START..END }.toSet() // it is the value in the sequence, in this case Pair<In, Int>
return newEntries
.map(::toggle) // method reference ::myFunction
.fold(initialGame) { acc, toggles ->
//lambda {...}
acc.plus(toggles).subtract(acc.intersect(toggles)) // immutable sets being created
}
.print() //extension method
}
//extension method Type.myFunExtension
private fun <E> Set<E>.print(): Set<E> {
println("""
|Lights:
| ${this.joinToString(separator = "\n ")}
|There is $size lights on!""".trimMargin()
//string interpolation, $ used for value
//in method extension instead of use ${this.size}, you can drop "this." and just use properties and methods like $size
)
return this
}
private val game = listOf(
Pair(3, 2),
Pair(2, 4),
Pair(3, 3),
Pair(0, 3),
Pair(1, 2),
Pair(3, 1),
Pair(1, 4),
Pair(4, 0),
Pair(1, 4),
Pair(0, 1),
Pair(0, 1),
Pair(0, 2),
Pair(0, 2),
Pair(3, 1),
Pair(0, 4),
Pair(2, 2),
Pair(1, 3),
Pair(3, 1),
Pair(2, 2),
Pair(1, 1),
Pair(0, 0),
Pair(4, 3),
Pair(4, 4),
Pair(2, 3),
Pair(4, 3))
//@Test // a Junit 5 test
//fun `my light game should have 13 ligths on`() {
//
// val start = setOf( // syntax sugar and immutable ish (read-only cause is a Java Collection)
// Pair(0, 1), Pair(0, 3),
// Pair(1, 1), Pair(1, 2), Pair(1, 3), Pair(1, 4),
// Pair(2, 0), Pair(2, 1), Pair(2, 3), Pair(2, 4),
// Pair(3, 0), Pair(3, 1), Pair(3, 2), Pair(3, 3), Pair(3, 4),
// Pair(4, 0), Pair(4, 1), Pair(4, 2), Pair(4, 3), Pair(4, 4)
// )
//
// //argument names
// val finalGame = play(initialGame = start, newEntries = game)
//
// assertEquals(finalGame.size, 13)
//
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment