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
public class ResizeAnimation extends Animation {
final int startWidth;
final int targetWidth;
View view;
public ResizeAnimation(View view, int targetWidth) {
this.view = view;
this.targetWidth = targetWidth;
startWidth = view.getWidth();
}
@JorgeCastilloPrz
JorgeCastilloPrz / reader.kt
Last active December 30, 2017 10:26
reader sample for medium article
class Reader<C, out A>(val run: (C) -> A) {
inline fun <B> map(crossinline fa: (A) -> B): Reader<C, B> = Reader {
c -> fa(run(c))
}
inline fun <B> flatMap(crossinline fa: (A) -> Reader<C, B>): Reader<C, B> = Reader {
c -> fa(run(c)).run(c)
}
@JorgeCastilloPrz
JorgeCastilloPrz / onresume.kt
Created March 31, 2017 16:42
snippet for article
override fun onResume() {
super.onResume()
presenter.getSuperHeroes().run(GetHeroesContext(this@MyActivity))
}
@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) })
}
}
fun getSuperHeroes(): Reader<GetHeroesContext, List<SuperHero>> =
Reader.ask<GetHeroesContext>().flatMap { ctx -> ctx.heroesRepository.getHeroes() }
@JorgeCastilloPrz
JorgeCastilloPrz / repository.kt
Created March 31, 2017 17:24
gist for article
fun getHeroes() = Reader<GetHeroesContext, List<SuperHero>> {
it.heroesDataSources[0].getAll()
}
@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 / 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 / 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 / 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)
}
}