Created
December 13, 2020 06:44
-
-
Save Moire9/543695d814c09ebb36367a66c538904a to your computer and use it in GitHub Desktop.
Customizable Kotlin FizzBuzz program that uses CLI args
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@file:Suppress("NOTHING_TO_INLINE") | |
inline fun e(i: Int): Nothing = throw IllegalArgumentException("Invalid FizzMap ($i)") | |
inline fun String.l() = toLongOrNull() | |
fun main(fm: Array<String>) = when { | |
fm.isEmpty() -> e(0) | |
fm.size % 2 == 1 -> Triple(1L, fm[fm.size-1].l() ?: e(1),2) | |
else -> Triple(fm[fm.size-2].l() ?: e(1), fm[fm.size-1].l() ?: e(1), 3) | |
}.apply { | |
val map = mapOf(*(fm.slice(0..fm.size - third).indices step 2).map { (fm[it].l() ?: e(2)) to fm[it+1] }.toTypedArray()) | |
(first..second).map { | |
val sb = StringBuilder() | |
map.forEach { (x, y) -> if (it % x == 0L) sb.append(y) } | |
(if (sb.isEmpty()) it else sb).toString() | |
}.forEach(::println) | |
}.let{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment