This file contains hidden or 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
| fun sumOfFactors(number: Int) = factorsOf(number).sum() | |
| fun factorsOf(number: Int) = (1 to number).toList().filter { isFactor(number, it) } | |
| fun isFactor(number: Int, potential: Int) = number % potential == 0 |
This file contains hidden or 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
| class FactorCalculator { | |
| val sumCache = hashMapOf<Int, Int>() | |
| val factorCache = hashMapOf<Int, List<Int>>() | |
| fun sumOfFactors(number: Int) = sumCache.getOrPut(number, { | |
| factorsOf(number).sum() | |
| }) | |
| fun isFactor(number: Int, potential: Int) = number % potential == 0 |
This file contains hidden or 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
| tailrec fun <T> filter(l: List<T>, res: List<T>, f: (T) -> Boolean): List<T> { | |
| if (l.isEmpty()) { | |
| return res | |
| } else { | |
| return filter(l.tail(), if (f(l.head())) { res + listOf(l.head())} else { res }, f) | |
| } | |
| } |
This file contains hidden or 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
| fun <T> filter(l: List<T>, res: List<T>, f: (T) -> Boolean): List<T> { | |
| if (l.isEmpty()) { | |
| return res | |
| } else { | |
| return filter(l.tail(), if (f(l.head())) { res + listOf(l.head())} else { res }, f) | |
| } | |
| } |
This file contains hidden or 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
| fun <T> List<T>.tail() = drop(1) | |
| fun <T> List<T>.head() = first() | |
| fun <T> filter(l: List<T>, f: (T) -> Boolean): List<T> { | |
| if (l.isEmpty()) { | |
| return listOf() | |
| } else { | |
| return if (f(l.head())) { | |
| listOf(l.head()) + filter(l.tail(), f) | |
| } else { |
This file contains hidden or 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
| fun <T> filter(l: List<T>, f: (T) -> Boolean): MutableList<T> { | |
| val res = mutableListOf<T>() | |
| l.forEach { if (f(it)) { res += it } } | |
| return res | |
| } |
This file contains hidden or 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
| override fun getAll() = listOf(SuperHero("IronMan"), SuperHero("Spider-Man"), | |
| SuperHero("Batman"), SuperHero("Goku"), SuperHero("Vegeta"), SuperHero("SuperMan"), | |
| SuperHero("Ant-Man"), SuperHero("Krilin"), SuperHero("Super Mario"), | |
| SuperHero("Wolverine"), SuperHero("Massacre"), SuperHero("Jake Wharton"), | |
| SuperHero("Jesus Christ"), SuperHero("Donald Trump (villain)")) |
This file contains hidden or 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
| fun getHeroes() = Reader<GetHeroesContext, List<SuperHero>> { | |
| it.heroesDataSources[0].getAll() | |
| } |
This file contains hidden or 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
| fun getSuperHeroes(): Reader<GetHeroesContext, List<SuperHero>> = | |
| Reader.ask<GetHeroesContext>().flatMap { ctx -> ctx.heroesRepository.getHeroes() } |
This file contains hidden or 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
| fun getSuperHeroes(): Reader<GetHeroesContext, Unit> = | |
| Reader.ask<GetHeroesContext>().flatMap { | |
| ctx -> ctx.getSuperHeroesInteractor.getSuperHeroes().map { | |
| if (it.isEmpty()) ctx.view.showHeroesNotFoundError() | |
| else ctx.view.drawHeroes(it.map { SuperHeroViewModel(it.name) }) | |
| } | |
| } |