Skip to content

Instantly share code, notes, and snippets.

@TJC
TJC / BenchTime.scala
Last active August 29, 2022 08:10
Benchmark ways of getting the time as seconds (Scala/Java)
import java.time.Clock
import java.time.Instant
object BenchTime {
val clock = Clock.systemUTC
def bench(fn: () => Unit, iterations: Int = 1000000) = {
val start = System.nanoTime
Range(0,iterations).foreach( _ => fn() )
val end = System.nanoTime
@TJC
TJC / mysql-formatter.scala
Last active March 18, 2022 04:53
Mysql 5.7 compatible Scala/Java DateTimeFormatter expression
import java.time._
import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder}
import java.time.temporal.ChronoField._
// This one doesn't work, because Mysql can return five-digits of precision, if the sixth digit was 0
val badFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS")
val mysqlFormatter = new DateTimeFormatterBuilder().appendValue(YEAR, 4)
.appendLiteral('-')
@TJC
TJC / soulaflare.ino
Created March 2, 2021 13:04
Soulaflare twinkling LEDs code
#include "Arduino.h"
#include <stdlib.h>
#include <FastLED.h>
// Was built against version 3.1
#define LEDPIN 0
#define LEDPIN2 1
#define LEDCOUNT 420
// Unused unless I go for non-mirroring:
#define LEDS_PER_STRIP 210
@TJC
TJC / encode_x264_screen
Last active January 27, 2021 07:19
Re-encode OSX screen recording mov files to a tenth of their size
#!/bin/bash
# Requires ffmpeg - brew install ffmpeg
if [[ -z "$1" ]]; then
echo "Usage: $0 Screen\\ Recording\\ at\\ 2021-01-21.mov"
exit 1
fi
basefilename=$(basename "$1" .mov)
outputname="$basefilename.mp4"
nice ffmpeg \
@TJC
TJC / encode_x264
Created August 20, 2020 07:44
Encode a Quicktime file to MPEG4-x264
#!/bin/bash
docker container run -u 1000 --rm -v "$PWD:/mnt" \
-v "$SRC:/input.mov:ro" \
alfg/ffmpeg \
nice ffmpeg -hide_banner \
-loglevel warning \
-i "/input.mov" \
-c:v libx264 -preset medium -profile:v high -level 4.2 -crf 21 \
"/mnt/output.mp4"
@TJC
TJC / ButterflyActor.sc
Created July 10, 2020 04:45
How to test Akka Actor has stopped, when using scheduler?
import java.util.concurrent.TimeUnit
import akka.actor.typed.Behavior
import akka.actor.typed.scaladsl.{Behaviors, TimerScheduler}
import scala.concurrent.duration._
class ButterflyActor(
timers: TimerScheduler[TriggerMessage],
) {
@TJC
TJC / Basic auth.md
Last active October 3, 2022 21:39
(Simplified) HTTP Basic auth vs MAC signed auth

Client:

username = "alice"
secretKey = "12345"
method = GET
url = "https://example.com/private"

Client sends what it essentially this HTTP request:
method = GET
URL = "https://example.com/private"
@TJC
TJC / RubyRunner.scala
Created June 20, 2019 04:43
Run a Ruby function from Scala and get the result.
import org.jruby.embed.ScriptingContainer
object RubyRunner {
def rubyScript = io.Source.fromFile("demo.rb").mkString
def main(args: Array[String]): Unit = {
val result = runScript(rubyScript, "hello world")
println(s"Ruby returned: $result")
}
@TJC
TJC / apparent_temp.scala
Last active February 8, 2019 06:15
apparent temperature equation
// rh = relative humidity, in range from 0.0 to 1.0
// t = temperature (celcius), typically in range from -10 to 50
// v = wind speed (m/s)
def vp(rh: Double, t: Double) = rh * 6.105 * Math.exp( (17.27 * t) / (237.7 + t))
def at(t: Double, rh: Double, v: Double) = t + (0.33 * vp(rh, t)) - (0.7 * v) - 4.00
#!/usr/bin/env ruby
# Tested on Ruby 2.5.1
require 'mini_racer'
MiniRacer::Platform.set_flags!
$iso = MiniRacer::Isolate.new
# Some mindless JS to keep the runtime busy for a moment
MY_JS = <<~JS
var foo = [];