Skip to content

Instantly share code, notes, and snippets.

@Sloy
Last active October 31, 2018 11:44
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 Sloy/12affca1e49ba052053ab4c8bfc5bcb4 to your computer and use it in GitHub Desktop.
Save Sloy/12affca1e49ba052053ab4c8bfc5bcb4 to your computer and use it in GitHub Desktop.
Fibonacci Dependencies
fun fibKotlinClass(n: Int): String {
return when (n) {
1, 2 -> "class Fib$n"
else -> "class Fib$n(val fibM1: Fib${n - 1}, val fibM2: Fib${n - 2})"
}
}
fun fibKotlinKoinFactory(n: Int): String {
return when (n) {
1, 2 -> "factory { Fib$n() }"
else -> "factory { Fib$n(get(), get()) }"
}
}
fun fibKotlinKodeinBinding(n: Int): String {
return when (n) {
1, 2 -> "bind<Fib$n>() with provider { Fib$n() }"
else -> "bind<Fib$n>() with provider { Fib$n(instance(), instance()) }"
}
}
fun fibKotlinDaggerProvider(n: Int): String {
return when (n) {
1, 2 -> "@Provides fun provideFib$n() = Fib$n()"
else -> "@Provides fun provideFib$n(fib${n - 1}: Fib${n - 1}, fib${n - 2}: Fib${n - 2}) = Fib$n(fib${n - 1}, fib${n - 2})"
}
}
fun fibJavaClass(n: Int): String {
return when (n) {
1, 2 -> "public static class Fib$n {}"
else -> """
|public static class Fib$n {
| Fib$n(Fib${n - 1} fibM1, Fib${n - 2} fibM2) {}
|}
""".trimMargin()
}
}
fun fibJavaKoinFactory(n: Int): String {
return when (n) {
1, 2 -> "factory { Fibonacci.Fib$n() }"
else -> "factory { Fibonacci.Fib$n(get(), get()) }"
}
}
fun fibJavaKodeinBinding(n: Int): String {
return when (n) {
1, 2 -> "bind<Fibonacci.Fib$n>() with singleton { Fibonacci.Fib$n() }"
else -> "bind<Fibonacci.Fib$n>() with provider { Fibonacci.Fib$n(instance(), instance()) }"
}
}
fun fibJavaDaggerProvider(n: Int): String {
return when (n) {
1, 2 -> "@Provides fun provideFib$n() = Fibonacci.Fib$n()"
else -> "@Provides fun provideFib$n(fib${n - 1}: Fibonacci.Fib${n - 1}, fib${n - 2}: Fibonacci.Fib${n - 2}) = Fibonacci.Fib$n(fib${n - 1}, fib${n - 2})"
}
}
fun printKotlinClasses(size: Int) {
println("/**\n" +
" * Generated with https://gist.github.com/Sloy/12affca1e49ba052053ab4c8bfc5bcb4\n" +
" */\n")
(1..size).forEach {
println(fibKotlinClass(it) + "\n")
}
}
fun printKotlinKoin(size: Int) {
println("import org.koin.dsl.module.module\n\n")
println("/**\n" +
" * Generated with https://gist.github.com/Sloy/12affca1e49ba052053ab4c8bfc5bcb4\n" +
" */\n")
println("val koinKotlinModule = module {\n")
(1..size).forEach {
println(fibKotlinKoinFactory(it) + "\n")
}
println("}\n")
}
fun printKotlinKodein(size: Int) {
println("import org.kodein.di.Kodein\n" +
"import org.kodein.di.erased.bind\n" +
"import org.kodein.di.erased.instance\n" +
"import org.kodein.di.erased.provider\n" +
"import org.kodein.di.erased.singleton\n\n")
println("/**\n" +
" * Generated with https://gist.github.com/Sloy/12affca1e49ba052053ab4c8bfc5bcb4\n" +
" */\n")
println("val kodeinKotlinModule = Kodein.Module(\"fib\") {\n")
(1..size).forEach {
println(fibKotlinKodeinBinding(it) + "\n")
}
println("}\n")
}
fun printKotlinDagger(size: Int) {
println("import dagger.Module\n" +
"import dagger.Provides\n\n")
println("/**\n" +
" * Generated with https://gist.github.com/Sloy/12affca1e49ba052053ab4c8bfc5bcb4\n" +
" */\n")
println("@Module class KotlinDaggerModule {\n")
(1..size).forEach {
println(fibKotlinDaggerProvider(it) + "\n")
}
println("}\n")
}
fun printJavaClasses(size: Int) {
println("/**\n" +
" * Generated with https://gist.github.com/Sloy/12affca1e49ba052053ab4c8bfc5bcb4\n" +
" */\n")
println("public class Fibonacci {\n")
(1..size).forEach {
println(fibJavaClass(it) + "\n")
}
println("}\n")
}
fun printJavaKoin(size: Int) {
println("import org.koin.dsl.module.module\n\n")
println("/**\n" +
" * Generated with https://gist.github.com/Sloy/12affca1e49ba052053ab4c8bfc5bcb4\n" +
" */\n")
println("val koinJavaModule = module {\n")
(1..size).forEach {
println(fibJavaKoinFactory(it) + "\n")
}
println("}\n")
}
fun printJavaKodein(size: Int) {
println("import org.kodein.di.Kodein\n" +
"import org.kodein.di.erased.bind\n" +
"import org.kodein.di.erased.instance\n" +
"import org.kodein.di.erased.provider\n" +
"import org.kodein.di.erased.singleton\n\n")
println("/**\n" +
" * Generated with https://gist.github.com/Sloy/12affca1e49ba052053ab4c8bfc5bcb4\n" +
" */\n")
// Kodein
println("val kodeinJavaModule = Kodein.Module(name = \"fib\") {\n")
(1..size).forEach {
println(fibJavaKodeinBinding(it) + "\n")
}
println("}\n")
}
fun printJavaDagger(size: Int) {
println("import dagger.Module\n" +
"import dagger.Provides\n\n")
println("/**\n" +
" * Generated with https://gist.github.com/Sloy/12affca1e49ba052053ab4c8bfc5bcb4\n" +
" */\n")
println("@Module class JavaDaggerModule {\n")
(1..size).forEach {
println(fibJavaDaggerProvider(it) + "\n")
}
println("}\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment