Skip to content

Instantly share code, notes, and snippets.

View thomasnield's full-sized avatar

Thomas Nield thomasnield

  • Frisco, Texas (United States)
View GitHub Profile
@thomasnield
thomasnield / kotlin_puzzler_elvis_addition.kt
Created June 2, 2018 05:43
Kotlin Puzzler- Elvis Backfires
val x: Int? = 2
val y: Int = 3
val sum = x?:0 + y
println(sum)
@thomasnield
thomasnield / calling_kotlin_from_python.py
Created June 2, 2018 23:09
Calling Kotlin from Python
os.system("kotlinc src/main/kotlin/Launcher.kt -include-runtime -d launcher.jar")
process = Popen(['java', '-jar', 'launcher.jar', tmp_file_name], stdout=PIPE)
(stdout, stderr) = process.communicate()
@thomasnield
thomasnield / DirtyCollections.kt
Last active June 9, 2018 16:26
JavaFX DirtyObservableList, DirtyObservableSet, and DirtyObjectProperty
import javafx.beans.property.SimpleBooleanProperty
import javafx.beans.value.ObservableValue
import javafx.collections.FXCollections
import javafx.collections.ObservableList
import javafx.collections.ObservableSet
import javafx.collections.SetChangeListener
import tornadofx.*
class DirtyObservableList<T>(originalList: List<T> = listOf()):
@thomasnield
thomasnield / binary_tree_traversing.kt
Last active June 9, 2018 19:58
Traversing a Binary Tree (Using a heuristic)
// PROBLEM VISUALIZATION (https://i.imgur.com/cwg5LZ9.png)
val treeRowCount = 5
enum class Direction {
ROOT,
LEFT,
RIGHT;
}
@thomasnield
thomasnield / segment_intersection.kt
Created June 24, 2018 12:32
Segment Intersection
// https://bryceboe.com/2006/10/23/line-segment-intersection-algorithm/
fun main(args: Array<String>) {
val x1 = Point(0.0, 0.0)
val y1 = Point(4.0, 4.0)
val x2 = Point(0.0, 4.0)
val y2 = Point(4.0, 0.0)
@thomasnield
thomasnield / prophet_example.r
Created June 26, 2018 20:14
Facebook Prophet Example
library(prophet)
library(dplyr)
df <- read.csv('http://bit.ly/2po0xPJ') %>% mutate(y=log(y))
#floor and cap
lower = quantile(df$y, .05)
upper = quantile(df$y, .95)
df <- df %>% mutate(floor = lower, cap = upper)
@thomasnield
thomasnield / DequeTransition.kt
Last active July 8, 2018 17:46
DequeTransition
import javafx.animation.Timeline
import java.util.concurrent.ConcurrentLinkedDeque
class DequeTransition {
private var _queue = ConcurrentLinkedDeque<Timeline>()
operator fun plusAssign(timeline: Timeline) {
@thomasnield
thomasnield / SingleAssignWithDefault.kt
Created July 27, 2018 19:32
Kotlin Property Delegate - Single Assign with Default
/**
* Property delegate that has a default value, but can only be re-assigned once
* This is helpful for deductive flagging tasks
*/
fun <T> singleAssignWithDefault(defaultValue: T) = SingleAssignWithDefault(defaultValue)
class SingleAssignWithDefault<T>(val defaultValue: T) {
private object UNINITIALIZED_VALUE
@thomasnield
thomasnield / rolling_batches_occurrences.kt
Last active July 28, 2018 21:35
Kotlin Collection Operator- Rolling Batches and Rolling Occurences
fun <T> List<T>.rollingBatches(batchSize: Int) = (0..size).asSequence().map { i ->
subList(i, (i + batchSize).let { if (it > size) size else it })
}.filter { it.size == batchSize }
enum class RecurrenceMode { PARTIAL_ONLY, FULL_ONLY, ALL }
fun <T> List<T>.rollingRecurrences(slotsNeeded: Int, gap: Int, recurrences: Int, mode: RecurrenceMode = RecurrenceMode.FULL_ONLY) =
(0..size).asSequence().map { i ->
(1..recurrences).asSequence().map { (it - 1) * gap }
.filter { it + i < size}
@thomasnield
thomasnield / MontyHall.kts
Last active September 23, 2018 20:31
Monte Carlo Simulation of Monty Hall
import java.util.concurrent.ThreadLocalRandom
/**
* Monty Hall Problem Simulation
*/
fun randomDoor() = ThreadLocalRandom.current().nextInt(0,3)
// how many trials do you want to run?
val trialCount = 10000