Kotlin extensions on commonly used structures for Math Camp
Kotlin has a great feature that allows for scoped extensions to already existing types. This includes primitives in the language like Functions and any optional.
Plus we have inline functions with reified generics on extension functions. There is ZERO runtime overhead because the lambdas are inlined!
The standard library was missing a few operations and structures that we found very useful:
Either<T, U>
is a disjoint union over two types T
and U
. It includes a right-biased map. This is useful for propagating errors safely without exceptions:
fun attemptParseInt(str: String) -> Either<ParseError, Int>
attemptParseInt("5").map { it + 1 } // 6
attemptParseInt("oops").map { it + 1 } // ParseError as a value!
// f1 o f2 is { x -> f2(f1(x)) }
fun addOne(x: Int): Int
fun addTwo(x: Int): Int
val addThree = addOne o addTwo
Unfortunately it looks like the latest version of Kotlin chokes on the type inference here. We used to use this operator often.
Always returns an infinite iterable that always returns the same value
// useful for zipping
listOf(1,2).zip(always(5)) // [(5, 1), (5, 2)]
See the code for more Iterables
operators
// useful with JSON
json.getAs<String, Object, String>("name")
Monadic optionals (like in Swift) are very useful. See the bind
and pure
operators in Options. Options.kt
also has many more functions.
Sometimes it's useful to apply a T -> U
transformation to make a (T, T)
a (U, U)
We needed to display our time information in a specific way for Roll. Ourlogic lives here.
Kotlin only provides Pair
and Triple
. We also have Tuple4
and Tuple5
.