Skip to content

Instantly share code, notes, and snippets.

View d-kozak's full-sized avatar

David Kozak d-kozak

View GitHub Profile
{"lang":"cs","karelAndRoom":{"room":{"0":{"0":{"bricks":0,"mark":false,"inRoom":true},"1":{"bricks":0,"mark":false,"inRoom":true},"2":{"bricks":0,"mark":false,"inRoom":true},"3":{"bricks":0,"mark":false,"inRoom":true},"4":{"bricks":0,"mark":false,"inRoom":true},"5":{"bricks":0,"mark":false,"inRoom":true},"6":{"bricks":0,"mark":false,"inRoom":true},"7":{"bricks":0,"mark":false,"inRoom":true}},"1":{"0":{"bricks":0,"mark":false,"inRoom":true},"1":{"bricks":0,"mark":false,"inRoom":true},"2":{"bricks":0,"mark":false,"inRoom":true},"3":{"bricks":0,"mark":false,"inRoom":true},"4":{"bricks":0,"mark":false,"inRoom":true},"5":{"bricks":0,"mark":false,"inRoom":true},"6":{"bricks":0,"mark":false,"inRoom":true},"7":{"bricks":0,"mark":false,"inRoom":true}},"2":{"0":{"bricks":0,"mark":false,"inRoom":true},"1":{"bricks":0,"mark":false,"inRoom":true},"2":{"bricks":0,"mark":false,"inRoom":true},"3":{"bricks":0,"mark":false,"inRoom":true},"4":{"bricks":0,"mark":false,"inRoom":true},"5":{"bricks":0,"mark":false,"inRoom":true},"6
@d-kozak
d-kozak / NewMidClass.java
Last active April 7, 2021 11:44
Introduce an in the middle class to override the original getContent.
package example;
import java.util.HashMap;
import java.util.Map;
abstract class BaseBundle {
protected abstract Object handleGetKey(String key);
}
abstract class ListResourceBundle extends BaseBundle {
@d-kozak
d-kozak / ExternallyLoadableBundle.java
Created April 6, 2021 14:48
Idea how to introduce alternative source of data for resource bundles.
import java.util.HashMap;
import java.util.Map;
abstract class BaseBundle {
protected abstract Object handleGetKey(String key);
}
interface ExternallyLoadableBundle {
default Map<String, Object> tryLoadExternal() {
@d-kozak
d-kozak / Qt5Asynchronous.py
Created March 17, 2020 19:05
Example of asynchronous task handling in PyQt5
import sys
import time
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QThread
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
"""
Example how to make asynchronous computation in PyQt5
"""
/**
* Infers state of a combined reducer based on return types of individual reducers
*/
export type StateFromReducers<T> = {
[K in keyof T]: T[K] extends (...args: any) => any ? ReturnType<T[K]> : T[K]
};
@d-kozak
d-kozak / delegation.kt
Created July 11, 2019 10:58
How to use delegation to reduce the dto size while being able to present everything to the client.
/**
* Example how to use delegation to avoid sending unnecessary data between backend components while still
* being able to send all the data to the client.
*
* The architecture is the following:
* client -> gateway -> Array<Servers>
*
* We want to be able to tell the client which server the result came from without the need to include it in the dto sent between
* the gateway and the server. This information should be added to the result on the gateway, but without the need to provide
* a fully redundant copy of the ResponseDto class. To do this, delegation can be used.
@d-kozak
d-kozak / coroutines-runBlocking.kt
Last active July 11, 2019 08:41
Coroutines runBlocking example
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
fun fac(n: Int): Int = if (n == 0) 1 else n * fac(n - 1)
suspend fun calc(n: Int): Int {
println("[${Thread.currentThread()}] computing fac of $n")
delay(1500)
return fac(n).also {
@d-kozak
d-kozak / reflection.kt
Created July 7, 2019 18:51
Example of setting properties of objects using reflection in Kotlin.
import kotlin.reflect.KMutableProperty
import kotlin.reflect.full.declaredMemberProperties
data class Person(
var name: String,
var age: Int,
var address: Address
)
data class Address(
@d-kozak
d-kozak / invertedIndex.kt
Created April 5, 2019 13:35
Minimalistic implementation of inverted index
package io.dkozak.inverted.index
import java.util.*
/**
* Minimalistic example how inverted index can be implemented.
* The implementation is simplified, it expects the data to be small enough to fit in memory, which might not be the case
* Therefore real implementation should be much more memory efficient.
* This is just an example for educational purposes
*/
@d-kozak
d-kozak / microstreams.kt
Last active April 4, 2019 00:09
Minimalistic implementation of a subset of the Stream/Sequence API
package io.dkozak.microstreams
/**
* A very minimalistic implementation of a subset of the Stream(Java) / Sequence(Kotlin) API
*/
/**
* Represents a MicroStream, which is basically a chain of operations that should be performed over a collection
* of data. Streams are evaluated lazily, that is until you perform a terminal operation ( which is forEach() and toList() in the current implementation),
* nothing is actually executed.