Skip to content

Instantly share code, notes, and snippets.

View JorgeCastilloPrz's full-sized avatar
🎉
Mostly Android stuff now, also FP

Jorge Castillo JorgeCastilloPrz

🎉
Mostly Android stuff now, also FP
View GitHub Profile
@JorgeCastilloPrz
JorgeCastilloPrz / factorcalculatorpure.kt
Last active May 12, 2017 14:25
factorcalculatorpure.kt
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
@JorgeCastilloPrz
JorgeCastilloPrz / factorcalculatorclass.kt
Last active May 12, 2017 14:15
factor calculator class for a blog post
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
@JorgeCastilloPrz
JorgeCastilloPrz / tailrec2.kt
Created May 5, 2017 22:14
asdk asokd kosadokas
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)
}
}
@JorgeCastilloPrz
JorgeCastilloPrz / filterTailRec.kt
Last active May 5, 2017 22:14
tail rec filter for blog post
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)
}
}
@JorgeCastilloPrz
JorgeCastilloPrz / filterRec.kt
Last active May 5, 2017 20:54
standard recursive filter function for blog post
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 {
@JorgeCastilloPrz
JorgeCastilloPrz / filter.kt
Last active May 5, 2017 20:29
imperative filter method for blog snippet
fun <T> filter(l: List<T>, f: (T) -> Boolean): MutableList<T> {
val res = mutableListOf<T>()
l.forEach { if (f(it)) { res += it } }
return res
}
@JorgeCastilloPrz
JorgeCastilloPrz / stubds.kt
Last active July 12, 2018 08:12
for an article
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)"))
@JorgeCastilloPrz
JorgeCastilloPrz / repository.kt
Created March 31, 2017 17:24
gist for article
fun getHeroes() = Reader<GetHeroesContext, List<SuperHero>> {
it.heroesDataSources[0].getAll()
}
fun getSuperHeroes(): Reader<GetHeroesContext, List<SuperHero>> =
Reader.ask<GetHeroesContext>().flatMap { ctx -> ctx.heroesRepository.getHeroes() }
@JorgeCastilloPrz
JorgeCastilloPrz / presentermethod.kt
Last active March 31, 2017 16:45
gist for article
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) })
}
}