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 / rxjava2_flowable_guava_immutable_collections.kt
Created March 22, 2017 17:37
Kotlin Extensions for RxJava2 and Google Guava Immutable Collections
import com.google.common.collect.ImmutableList
import com.google.common.collect.ImmutableListMultimap
import com.google.common.collect.ImmutableMap
import com.google.common.collect.ImmutableSet
import io.reactivex.Flowable
import io.reactivex.Observable
fun <T> Observable<T>.toImmutableList() =
collect( { ImmutableList.builder<T>() }, { b,t -> b.add(t) })
@thomasnield
thomasnield / kotlin_sequence_for_guava_immutable_collections.kt
Created March 22, 2017 17:38
Kotlin Sequence Extensions for Google Guava Immutable Collections
import com.google.common.collect.ImmutableList
import com.google.common.collect.ImmutableListMultimap
import com.google.common.collect.ImmutableMap
import com.google.common.collect.ImmutableSet
fun <T> Sequence<T>.toImmutableList(): ImmutableList<T> {
val builder = ImmutableList.builder<T>()
forEach { builder.add(it) }
@thomasnield
thomasnield / rxjava_cartesian_product.kt
Created March 27, 2017 14:32
RxJava Cartestian Product
import io.reactivex.Observable
fun main(args: Array<String>) {
val sources = listOf(
Observable.just(1, 2, 3, 4, 5),
Observable.just(10, 20, 30, 40, 50),
Observable.just(100, 200, 300, 400, 500)
)
var cartesianProduct: Observable<List<Int>>? = null
@thomasnield
thomasnield / system76_galago_pro_specs.txt
Last active May 25, 2017 21:10
System76 Galago Pro Specs
thomas@thomas-galago ~ $ sudo lshw
[sudo] password for thomas:
thomas-galago
description: Notebook
product: Galago Pro (Not Applicable)
vendor: System76
version: galp2
serial: Not Applicable
width: 64 bits
capabilities: smbios-3.0 dmi-3.0 vsyscall32
@thomasnield
thomasnield / JdbcToDataClass.kt
Last active June 3, 2017 00:31
JDBC ResultSet to Data Class
// Bring in Google Guava as a dependency
// compile 'com.google.guava:guava:22.0'
import com.google.common.base.CaseFormat
import java.sql.ResultSet
fun ResultSet.toDataClass(): String {
@thomasnield
thomasnield / rxjava_replay_observable_invalidation.kt
Created June 23, 2017 19:55
RxJava - Replayed Observable Invalidation
import io.reactivex.Observable
import java.time.LocalTime
import java.util.concurrent.TimeUnit
fun main(args: Array<String>) {
val source = Observable.interval(5, TimeUnit.SECONDS)
.startWith(0)
.map {
@thomasnield
thomasnield / year_over_year_growth.sql
Created June 29, 2017 15:50
Year-over-year growth in SQL
-- =========================================
-- simple year-over-year
-- =========================================
SELECT
SUM(CASE WHEN year = 2008 THEN precipitation ELSE 0 END) AS cy_precipitation,
SUM(CASE WHEN year = 2009 THEN precipitation ELSE 0 END) AS py_precipitation,
SUM(CASE WHEN year = 2008 THEN precipitation ELSE 0.0 END) / SUM(CASE WHEN year = 2009 THEN precipitation ELSE 0.0 END) - 1.0 as change
FROM station_data
@thomasnield
thomasnield / sqlite_r_example.r
Last active July 7, 2017 15:59
SQLite and R Example
setwd('c:\\git')
library(DBI)
library(RSQLite)
db <- dbConnect(SQLite(), dbname='rexon_metals.db')
myQuery <- dbSendQuery(db, "SELECT * FROM CUSTOMER")
my_data <- dbFetch(myQuery, n = -1)
@thomasnield
thomasnield / matrices_demo.kt
Last active August 1, 2023 16:44
Matrices with Kotlin (using kotlin-statistics)
import java.time.LocalDate
import java.time.temporal.ChronoUnit
import org.apache.commons.math3.linear.Array2DRowRealMatrix
import org.apache.commons.math3.linear.RealMatrix
fun main(args: Array<String>) {
val matrix = patients.toMatrix(
{ it.age },
@thomasnield
thomasnield / kotlin_pom.xml
Created August 20, 2017 20:21
Kotlin 1.1.4 Maven POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.oreilly</groupId>
<artifactId>kotlinproject</artifactId>
<version>1.0</version>