Skip to content

Instantly share code, notes, and snippets.

View ischumacher's full-sized avatar

Ian Schumacher ischumacher

  • Vancouver
View GitHub Profile
@ischumacher
ischumacher / JThumbnailer.kt
Created August 26, 2018 16:15
Quick and dirty code to create video thumbnails / screens / fillmstrip. Require FFMPEG to be on 'path' so can be executed.
import java.awt.Color
import java.awt.RenderingHints
import java.awt.image.BufferedImage
import java.io.File
import java.nio.file.Paths
import javax.imageio.ImageIO
fun proc(vararg cmd: String): Process {
val pb = ProcessBuilder(cmd.asList())
pb.redirectError(ProcessBuilder.Redirect.INHERIT)
@ischumacher
ischumacher / Z85.kt
Last active July 21, 2018 14:21
Kotlin translation of C version of Z85 encoding. Z85 encoding is similar to Base64 encoding except that it uses Base85 and encodes 4 bytes into 5 bytes. https://rfc.zeromq.org/spec:32/Z85/
// Maps base 256 to base 85
val encoder = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#"
.toCharArray()
// Maps base 85 to base 256
// We chop off lower 32 and higher 128 ranges
val decoder = byteArrayOf(
0x00, 0x44, 0x00, 0x54, 0x53, 0x52, 0x48, 0x00,
0x4B, 0x4C, 0x46, 0x41, 0x00, 0x3F, 0x3E, 0x45,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x40, 0x00, 0x49, 0x42, 0x4A, 0x47,
@ischumacher
ischumacher / json.kt
Last active February 20, 2018 16:43
JSON implementation in Kotlin in ~200 lines of code.
package ian.json
fun exampleJSONUsage() {
var json = JSON.parseJSON("{a:5, b:'test', c: {d: 'xxxxxx', e: ['Hello','There','test','one']}}")
// pretty printed JSON
println(json)
// Should be same as previous
println(JSON.parseJSON(json.toString()))
// get a child JSON object
println(json.getJSONObject("c"))
@ischumacher
ischumacher / RJoin
Created September 23, 2013 00:29
Mathematica: Relational join of 2 tables on a unique id
(* Simulate tables *)
a = Table[{x, RandomReal[]}, {x, 1, 100, 2}];
b = Table[{x, RandomReal[]}, {x, 1, 100, 3}];
RJoin[a_, b_] := Block[{f},
Do[
f[x[[1]]] = x[[2]],
{x, a}
];
Cases[b /. {x_, y_} -> {x, y, f[x]}, {_, _, _Real}]
@ischumacher
ischumacher / collisionprob.ma
Last active December 23, 2015 16:59
Mathematica: Birthday paradox. Collision probability function.
(*
Total possible number of items - n
Population size for collision probability - x
For 32 bit hash, collision prob = 0.5 when number of hashes is 77,162
For 64 bit hash, collision prob = 0.5 when number of hashes is 5 billion.
Solve[CollisionProb[x, n] == 0.5, {x, n}]
n -> 0.721348 x^2
@ischumacher
ischumacher / oil.ma
Last active December 23, 2015 16:58
Mathematica: Plot the price of oil
DateListPlot[
Drop[Import["http://research.stlouisfed.org/fred2/data/OILPRICE.txt",
"Table", "HeaderLines" -> 12,
"DateStringFormat" -> {"Year", "Month", "Day"}], 5],
Joined -> True, PlotRange -> All]