Skip to content

Instantly share code, notes, and snippets.

View juliuscanute's full-sized avatar
💭
I may be slow to respond.

juliuscanute

💭
I may be slow to respond.
View GitHub Profile
@juliuscanute
juliuscanute / AppConfig.kt
Created May 25, 2020 22:31
AppConfig & Config DSL
appConfig {
config("ALPHA") {
switch {/*...*/}
range {/*...*/}
editable {/*...*/}
choice {/*...*/}
}
config("BETA") {
switch {/*...*/}
range {/*...*/}
@juliuscanute
juliuscanute / Editable.kt
Created May 25, 2020 10:06
Editable DSL
editable {
key = "C"
description = "C-D"
currentValue = "C-V"
}
range {
key = "B"
description = "B-D"
min = 1
max = 100
currentValue = 50
}
switch {
key = "A"
description = "A-D"
switchValue = false
}
choice {
key = "D"
description = "D-D"
currentChoiceIndex = 0
item {
description = "E"
}
item {
description = "F"
}
@juliuscanute
juliuscanute / PollExample.kt
Last active January 12, 2020 20:34
[Poll vs Receive] #poll #coroutine #kotlin
fun main() {
val fruitArray = arrayOf("Apple", "Banana", "Pear", "Grapes", "Strawberry")
val kotlinChannel = Channel<String>()
runBlocking {
launch {
for (fruit in fruitArray) {
@juliuscanute
juliuscanute / OfferExample.kt
Last active January 12, 2020 20:33
[Offer vs Send] #kotlin #coroutine #offer
fun main() {
val fruitArray = arrayOf("Apple", "Banana", "Pear", "Grapes", "Strawberry")
val kotlinChannel = Channel<String>()
runBlocking {
launch {
for (fruit in fruitArray) {
val wasSent = kotlinChannel.offer(fruit)
@juliuscanute
juliuscanute / BufferedChannelExample.kt
Created January 12, 2020 20:27
[Buffered Channel] #kotlin #coroutine #buffered #channel
fun main() {
val fruitArray = arrayOf("Apple", "Banana", "Pear", "Grapes", "Strawberry")
val kotlinBufferedChannel = Channel<String>(2)
runBlocking {
launch {
for (fruit in fruitArray) {
kotlinBufferedChannel.send(fruit)
@juliuscanute
juliuscanute / FanIn.kt
Created January 12, 2020 20:25
[FanIn Multiplexer - Multiple Producer, Single Consumer] #kotlin #coroutine #fanin #multiplexer
@ExperimentalCoroutinesApi
fun main() {
data class Fruit(override val name: String, override val color: String) : Item
data class Vegetable(override val name: String, override val color: String) : Item
// ------------ Helper Methods ------------
fun isFruit(item: Item) = item is Fruit
fun isVegetable(item: Item) = item is Vegetable
@juliuscanute
juliuscanute / FanOut.kt
Created January 12, 2020 20:22
[FanOut Demultiplexer - Multiple Subscription] #fanout #demultiplexer #subscription #kotlin
/**
* This is a class which implements a demultiplexer. It send each items
* into the first channel with a predicate that evaluates true for it
*/
class Demultiplexer<E>(vararg val rules: Rule<E>) {
suspend fun consume(receiveChannel: ReceiveChannel<E>) {
for (item in receiveChannel) {
// Receive the data from the channel
for (rule in rules) {