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
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(); | |
} |
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
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) | |
} |
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
override fun onResume() { | |
super.onResume() | |
presenter.getSuperHeroes().run(GetHeroesContext(this@MyActivity)) | |
} |
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
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) }) | |
} | |
} |
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
fun getSuperHeroes(): Reader<GetHeroesContext, List<SuperHero>> = | |
Reader.ask<GetHeroesContext>().flatMap { ctx -> ctx.heroesRepository.getHeroes() } |
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
fun getHeroes() = Reader<GetHeroesContext, List<SuperHero>> { | |
it.heroesDataSources[0].getAll() | |
} |
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
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 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 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 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) | |
} | |
} |