Skip to content

Instantly share code, notes, and snippets.

View Mishkun's full-sized avatar
🏠

Mikhail Levchenko Mishkun

🏠
View GitHub Profile
// sealed class Option<T> // can be exchanged later for Nullable Type
// object None : Option<Nothing>
// class Some(val value: T) : Option<T>
// etc..
infix fun <T, R> Option<(T) -> R>.apply(option: Option<T>): Option<R> = when (this) { // apply from Applicative contract
is Some -> option.map (this.get())
is None -> None
}
@Mishkun
Mishkun / TEA-StateMachine.kt
Created April 26, 2018 06:52
Compiler would typecheck if we used the correct update function or not
// Message hierarchy
sealed class Msg
sealed class MsgLogined : Msg()
object Logout : MsgLogined()
object Greet : MsgLogined()
sealed class MsgLogouted : Msg()
data class Login(val username: String) : MsgLogouted()
// Model hierarchy
sealed class Model<T:Msg>(val messageType: Class<T>)
@Mishkun
Mishkun / CompositeFragment.kt
Last active May 23, 2018 11:45
My composite fragment
/**
* Basic delegate for [CompositeFragment], extend this class to make delegate that needs only context
* to operate, such as [SnackbarFragmentDelegate].
*
* @author themishkun on 19/04/2018.
*/
abstract class FragmentDelegate {
lateinit var context: Context
fun create(context: Context) {
@Mishkun
Mishkun / findrange.py
Created June 18, 2018 19:57
Given the list of integers, find sublist of it, so that sum of that sublist would be _k_. Trying to solve it in linear time
samples = [([1,2,3,4,5,6,7,8,9], [4,5,6,7]),
([1,-2,-3,-4,-5,-6,10], [-4,-5,-6,10])]
def solve(task, k):
i = 0
j = 0
s = 0
predicateI = lambda s, k: s < k
predicateJ = lambda s, k: s > k
if k < 0:
fun curry(f: (A, B) -> C) : (A) -> (B) -> C = { a: A -> { b: B -> f(a,b) } }
fun foo(x: X) = curry { y: Y, z: Z ->
// doSomething with x, y, z
}
val bar = foo(baz)(bazz)(bazzz)
data class User(val name: String, val surname: String)
class UserAdapter(val user: User){
override fun equals(other: Any?): Boolean {
return (other as? UserAdapter)?.user?.surname == user.surname
}
}
val users1 = listOf(User("A", "B"), User("DELETE", "C"))
val users2 = listOf(User("A", "C"))
class FooViewState: BaseViewState<FooView> : FooView{
val dsl = ViewStateDsl<FooView>()
//Выполнится при каждом restore
override fun bar(param: Param) {
dsl(this::bar){
view.state {
bar(param)
}
}

Now to pass some arguments to ViewModel we have to include them into the initialState, like in the example below.

data class MyState(val foo: Int) : MvRxState {
    constructor(args: MyArgs) : this(args.foo)
}

It works well in the simple examples, but sometimes we need some info that does not need to be rendered. An api parameter, some userId, for example. We have to use "dead" fields in state that make no sense for the View and only needed to construct a ViewModel or perform background operations.

@Parcelize
data class MyArgs(val userId: String)
@Mishkun
Mishkun / Kotlin-Pattern-Matching.kt
Created September 16, 2018 17:07
This is an example of how the pattern-matching could work in Kotlin, if all data classes implemented a `ComponentN` interface implicitly, additionaly to just generate the `componentN()` methods.
package com.mishkun.components
interface Component2<T1, T2> {
fun component1(): T1
fun component2(): T2
}
interface Matcher<T> {
fun matches(value: T): Boolean
}
package jmh
import org.openjdk.jmh.annotations.*
import org.openjdk.jmh.infra.Blackhole
@State(Scope.Benchmark)
@Fork(1)
open class KotlinLazy {
@State(Scope.Thread)