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
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 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 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 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 Memoize1<in T, out R>(val f: (T) -> R) : (T) -> R { | |
private val values = mutableMapOf<T, R>() | |
override fun invoke(x: T): R { | |
return values.getOrPut(x, { f(x) }) | |
} | |
} | |
fun <T, R> ((T) -> R).memoize(): (T) -> R = Memoize1(this) | |
val memoizedSumFactors = { x: Int -> sumOfFactors(x) }.memoize() |
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 getCountryCode(maybePerson : Either<BizError, Person>): Either<BizError, String> = | |
Either.monadError<BizError>().binding { | |
val person = maybePerson.bind() | |
val address = person.address.toEither({ AddressNotFound(person.id) }).bind() | |
val country = address.country.toEither({ CountryNotFound(address.id) }).bind() | |
yields(country.code) | |
}.ev() |
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
interface DataSource { | |
fun allTasksByUser(user: User): Observable<List<Task>> | |
} | |
class LocalDataSource : DataSource { | |
private val localCache: Map<User, List<Task>> = | |
mapOf(User(UserId("user1")) to listOf(Task("LocalTask assigned to user1"))) | |
override fun allTasksByUser(user: User): Observable<List<Task>> = Observable.create { emitter -> | |
val cachedUser = localCache[user] |
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
interface DataSource { | |
fun allTasksByUser(user: User): Observable<List<Task>> | |
} |
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 LocalDataSource : DataSource { | |
private val localCache: Map<User, List<Task>> = | |
mapOf(User(UserId("user1")) to listOf(Task("LocalTask assigned to user1"))) | |
override fun allTasksByUser(user: User): Observable<List<Task>> = | |
Observable.create { emitter -> | |
val cachedUser = localCache[user] | |
if (cachedUser != null) { | |
emitter.onNext(cachedUser) | |
} 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
class TaskRepository(private val localDS: DataSource, | |
private val remoteDS: RemoteDataSource) { | |
fun allTasksByUser(user: User): Observable<List<Task>> = | |
localDS.allTasksByUser(user) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(Schedulers.computation()) | |
.onErrorResumeNext { _: Throwable -> remoteDS.allTasksByUser(user) } | |
} |
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 Module { | |
private val localDataSource: LocalDataSource = LocalDataSource() | |
private val remoteDataSource: RemoteDataSource = RemoteDataSource() | |
val repository: TaskRepository = TaskRepository(localDataSource, remoteDataSource) | |
} |