Skip to content

Instantly share code, notes, and snippets.

View MarcinMoskala's full-sized avatar

Marcin Moskała MarcinMoskala

View GitHub Profile
package com.google.firebase.udacity.friendlychat
import com.google.firebase.database.ChildEventListener
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
class _ChildEventListener(
private val _onCancelled: (DatabaseError) -> Unit,
private val _onChildMoved: (DataSnapshot, String?) -> Unit,
private val _onChildChanged: (DataSnapshot, String?) -> Unit,
@MarcinMoskala
MarcinMoskala / TestCoroutineContext.kt
Created January 29, 2018 15:34 — forked from streetsofboston/TestCoroutineContext.kt
Kotlin Unit Tests Util for having testable functions using Coroutines: TestCoroutineContext
@file:Suppress("PackageDirectoryMismatch")
/*
* Copyright (c) 2018 Intrepid Pursuits,Inc. All rights reserved.
*/
package kotlinx.coroutines.experimental.intrepid
import kotlinx.coroutines.experimental.*
import java.util.concurrent.PriorityBlockingQueue
import java.util.concurrent.TimeUnit
@MarcinMoskala
MarcinMoskala / genesis_public_key
Created February 21, 2018 06:21
Genesis public key
040bce15188234923d3df6c460629e3af0d3d4d714d7da37d49d2d33de236f9ac5e1430a7d77617e6014b6884420a843dbdf27eac9f19a07976e57aeadc462211f
@MarcinMoskala
MarcinMoskala / Inline.kt
Created April 7, 2018 09:07
Compare inline and not inline functions for collection processing
import kotlin.system.measureNanoTime
data class Product(val price: Double, val bought: Boolean)
fun main(args: Array<String>) {
val users = (1..50_000_000).map { Product(10.0, true) }
measureNanoTime { code1(users) }
measureNanoTime { code2(users) }
@MarcinMoskala
MarcinMoskala / TestInline.kt
Created April 7, 2018 09:21
Inline and noinline repeat
import kotlin.system.measureNanoTime
fun main(args: Array<String>) {
measureNanoTime { code1() }
measureNanoTime { code2() }
val tries = 100
var sum1 = 0L
var sum2 = 0L
for (i in 1..tries) {
@MarcinMoskala
MarcinMoskala / Test.kt
Created May 25, 2018 19:47
Blackjack game simulation
import Decision.*
import org.jetbrains.kotlin.backend.common.pop
fun generateDeck(): List<Int> = (List(4) { 10 } + (2..9) + 11) * 4
fun generateDealerDeck() = (generateDeck() * 6).shuffled()
private operator fun <E> List<E>.times(num: Int) = (1..num).flatMap { this }
fun List<Int>.trueCount(): Int = -sumBy(::cardValue) * 52 / size
fun decide(hand: Hand, casinoCard: Int, firstTurn: Boolean): Decision = when {
firstTurn && hand.canSplit && hand.cards[0] == 11 -> SPLIT
firstTurn && hand.canSplit && hand.cards[0] == 9 && casinoCard !in listOf(7, 10, 11) -> SPLIT
firstTurn && hand.canSplit && hand.cards[0] == 8 -> SPLIT
firstTurn && hand.canSplit && hand.cards[0] == 7 && casinoCard <= 7 -> SPLIT
firstTurn && hand.canSplit && hand.cards[0] == 6 && casinoCard <= 6 -> SPLIT
firstTurn && hand.canSplit && hand.cards[0] == 4 && casinoCard in 5..6 -> SPLIT
firstTurn && hand.canSplit && hand.cards[0] in 2..3 && casinoCard <= 7 -> SPLIT
hand.unusedAces >= 1 && hand.points >= 19 -> STAND
hand.unusedAces >= 1 && hand.points == 18 && casinoCard < 9 -> STAND
fun getBetSize(trueCount: Int, bankroll: Double): Double {
val bettingUnit = bankroll / 1000
return (bettingUnit * (trueCount - 1)).coerceIn(25.0, 1000.0)
}
fun List<Int>.trueCount(): Int = -sumBy(::cardValue) * 52 / size
fun cardValue(card: Int) = when (card) {
in 2..6 -> 1
10, 11 -> -1
else -> 0
}