Skip to content

Instantly share code, notes, and snippets.

View charlag's full-sized avatar

Willow charlag

View GitHub Profile
private sealed class Event {
data class TodoCheckedEvent(val id: Long, val completed: Boolean) : Event()
data class NewTodoEvent(val text: String) : Event()
data class DbUpdateEvent(val todos: List<TodoEntity>) : Event()
}
private fun reducer(state: State, event: Event): State = when (event) {
is Event.TodoCheckedEvent -> {
val todos = state.todos.map { todo ->
if (todo.id == event.id) todo.copy(completed = event.completed)
else todo
}
state.copy(todos = todos)
}
is Event.DbUpdateEvent -> state.copy(todos = event.todos)
is Event.NewTodoEvent -> state.copy(
private val addNewTodoEffect = Transformer<State, Event, Event> { upstream ->
upstream.ofEventType(Event.NewTodoEvent::class)
.observeOn(Schedulers.io())
.switchMap { (_, _, event) ->
db.todoDao().add(
TodoEntity(
id = 0,
text = event.text,
completed = false
)
typealias ViewState = List<TodoViewData>
data class ViewStateEvent(val viewState: ViewState) : Event()
private val viewStateEffect = Transformer<State, Event, Event.ViewStateEvent> { upstream ->
upstream.distinctUntilChanged { old, new -> old.second == new.second }
.map { (_, state, _) ->
val viewState = state.todos.map {
TodoViewData(it.id, it.text, it.completed)
}
.sortedBy { it.completed }
Event.ViewStateEvent(viewState)
}
}
val eventsFromView = viewEvents.map { viewEvent ->
when (viewEvent) {
is SampleViewEvent.TodoCheckedViewEvent ->
Event.TodoCheckedEvent(viewEvent.id, viewEvent.isChecked)
is SampleViewEvent.TodoAddedViewEvent ->
Event.NewTodoEvent(viewEvent.text)
}
}
val dbEvents = db.todoDao().getAll().toObservable().map(::DbUpdateEvent)
val externalEvents = Observable.merge(eventsFromView, dbEvents)
@charlag
charlag / Memoize.kt
Created December 22, 2017 13:57
Simple memoization extension with capacity == 1.
package io.charlag.memoize
fun <A, E> ((A) -> E).memoize(): ((A) -> E) {
var arg: A? = null
var result: E? = null
return { a ->
if (a != arg) {
arg = a
result = this(a)
@charlag
charlag / update_kernel.sh
Last active December 17, 2018 09:33
Update Ubuntu to mainline kernel
echo 'http://kernel.ubuntu.com/~kernel-ppa/mainline/?C=N;O=D'
#read -p 'Version to install: ' KERNEL_VERSION
KERNEL_VERSION="4.19.9"
KDIR="kernel_$KERNEL_VERSION"
gio trash -f $KDIR
mkdir $KDIR
wget "http://kernel.ubuntu.com/~kernel-ppa/mainline/v${KERNEL_VERSION}/CHECKSUMS" -O $KDIR/CHECKSUMS -nv
HEADERS_ALL_LINE=$(grep -i -m 1 "linux-headers-.*_all.deb" $KDIR/CHECKSUMS)
HEADERS_ALL_ARR=($HEADERS_ALL_LINE)
@charlag
charlag / txt
Created October 12, 2018 16:37
UUUUUUGH
Let's say this is a top of my timeline:
"100883628340642811",
"100883628339152625",
"100883628129621553",
"100883627886489576",
"100883626425518196",
"100883626407487034",
"100883626392971820",
"100883626164206546",
@charlag
charlag / Org->GH markdown
Created October 23, 2019 20:01
gh-md.el
(defun gh-md ()
"Copy Replace current file's ORG contents replaced
with Github-flavoured ones. Works quite poorly for now."
(interactive)
(let ((real-buffer (current-buffer)))
(with-temp-buffer
(let ((temp-buffer (current-buffer)))
(with-current-buffer real-buffer
(copy-to-buffer temp-buffer (point-min) (point-max))
))