@Test fun list() {
println(measureTimeMillis {
println((0..10000000)
.filter { it % 2 == 0}
.map { it * it }
.map { it - 10 }.size)
})
}
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 MutableErrorLiveData<T> : MutableLiveData<Notification<T>>() { | |
fun observe(owner: LifecycleOwner, onNext: (T) -> Unit, onError: (Throwable) -> Unit) { | |
observe(owner, Observer { notification -> | |
notification ?: return@Observer | |
if (notification.isOnNext) { | |
onNext(notification.value!!) | |
} else if (notification.isOnError) { | |
onError(notification.error!!) | |
} | |
}) |
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 UserRepository( | |
userDao: UserDao | |
) { | |
private val userDaoWithCache = Cache(userDao::getUser) | |
fun getUser(id: Int, name: String): Single<User> { | |
return userDaoWithCache.fetchOrCache(id, name) | |
} | |
} |
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 UserRepository2( | |
userDao: UserDao | |
) { | |
private val userDaoWithCache = Cache2(userDao::getUser) | |
private val userDao2WithCache = Cache2(userDao::getUser2) | |
fun getUser(id: Int, name: String): Single<User> = | |
userDaoWithCache(id, name) | |
fun getUser2(id: Int, name: String, password: String): Single<User> = |
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 UserRepository4( | |
original: UserDao | |
) { | |
private val withCache = cache(original::getUser) | |
private val withCache2 = cache(original::getUser2) | |
fun getUser(id: Int, name: String): Single<User> = withCache(id, name) | |
} | |
interface UserDao { |
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 HogeActivity : Activity() { | |
… | |
… | |
private fun isHeavyUser(loginCount: Int, firstAccess: Boolean) : Boolean() { | |
return firstAccess || (loginCount >= 10 && loginCount <= 100) | |
} | |
} |
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 HogeActivity : Activity() { | |
private fun isHeavyUser(loginCount: Int, firstAccess: Boolean) : Boolean() { | |
// hyper HeavyUser if loginCount > 100. hyper HeavyUser doesn’t contain a HeavyUser. | |
return firstAccess || (loginCount >= 10 && loginCount <= 100) | |
} | |
} |
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
data class User(private val loginCount: Int, private val firstAccess: Boolean) { | |
private fun isHeavyUser() : Boolean() { | |
// hyper HeavyUser if loginCount > 100. hyper HeavyUser doesn’t contain a HeavyUser. | |
return firstAccess || (loginCount >= 10 && loginCount <= 100) | |
} | |
private fun hyperHeavyUser(): Boolean() { /** */ } | |
} |
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
interface RRR { | |
@GET("data/2.5/weather") | |
fun _getWeather( | |
@Query("a1") a1: Int, | |
@Query("a2") a2: String | |
): Deferred<Resp> | |
} | |
val RRR.getWeather get() = ::_getWeather.toSuspend |
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
@UseExperimental(ExperimentalContracts::class) | |
fun <T : Activity> ActivityScenario<T>.onActivity2(block: (T) -> Unit) { | |
contract { | |
callsInPlace(block, InvocationKind.EXACTLY_ONCE) | |
} | |
onActivity { | |
block(it) | |
} | |
} |