Skip to content

Instantly share code, notes, and snippets.

@gabrieljones
gabrieljones / fibonacci.kt
Created January 18, 2024 15:05
multiple ways to compute the fiboonacci sequence
package gabrieljones
import com.google.common.math.BigIntegerMath
import java.math.BigDecimal
import java.math.BigInteger
import java.math.MathContext
import java.math.RoundingMode
import kotlin.coroutines.cancellation.CancellationException
fun main() {
@gabrieljones
gabrieljones / runCatchingNonFatal.kt
Last active January 18, 2024 15:00
runCatching that does not catch Fatal throwables and does not catch CancellationException
inline fun <R> runCatchingNonFatal(block: () -> R): Result<R> {
return try {
Result.success(block())
}
catch (t: Throwable) {
when (t) {
// VirtualMachineError includes OutOfMemoryError and other fatal errors
is VirtualMachineError, is ThreadDeath, is InterruptedException, is LinkageError, is CancellationException -> throw t
else -> Result.failure(t)
package com.gabrieljones
import java.util.concurrent.{Executors, ScheduledFuture}
import scala.sys.ShutdownHookThread
object AllLogicInShutdownHook {
def main(args: Array[String]): Unit = {
// schedule executor
println("Adding Hook...")
package com.gabrieljones
import scala.collection.mutable.ListBuffer
object NestedFinally {
def main(args: Array[String]): Unit = {
val es: ListBuffer[Exception] = ListBuffer.empty
try {
try {
throw new Exception("1")
@gabrieljones
gabrieljones / listcerts.sh
Created January 23, 2023 18:56
list the certs of cacerts in a distroless java based container image with the keytool inside the image
docker run --rm --entrypoint="/usr/lib/jvm/java-17-openjdk-amd64/bin/keytool" distroless/java17:nonroot -list -cacerts -storepass changeit
@gabrieljones
gabrieljones / Three Dimensions of a Dependency Version.md
Last active October 13, 2022 14:48
Three Dimensions of a Dependency Version

There are at least three natural dimensions to the modern maven dependency version. Dependency resolution version specifiers should incorporate all three dimensions. As of v7.5.1 Gradle has no mechanism to specify all three dimmensions simultaneously. You can do SemVer Range or latest.release vs latest.integration but not all three at the same time. https://docs.gradle.org/current/userguide/single_versions.html

Proposal: Allow use of all three dimensions simultaneously.

  1. SemVer Range eg
  • 2.15.0
package dev.gabrieljones;
import io.vavr.control.Either;
import io.vavr.control.Option;
import io.vavr.control.Try;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.{Timer, TimerTask}
import java.util.concurrent.ForkJoinPool
import java.util.concurrent.atomic.{AtomicInteger, AtomicLong, AtomicReference}
import scala.concurrent.duration.{Duration, DurationInt, DurationLong}
import scala.concurrent.{Await, ExecutionContext, ExecutionContextExecutor, Future, Promise, blocking}
import scala.util.chaining.scalaUtilChainingOps
object ForkJoinPoolSandbox extends App {
val timer = new Timer()
// ==UserScript==
// @name QR This SVG
// @namespace https://gabrieljones.dev/
// @include *
// @version 0.1
// @author Gabriel Jones
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant GM_registerMenuCommand
// @grant GM_download
// ==/UserScript==
@gabrieljones
gabrieljones / DistributedCounterDemo.ino
Created December 22, 2020 18:26 — forked from bigjosh/DistributedCounterDemo.ino
Counts the number of connected nodes in a distributed and robust way. More info at https://forum.move38.com/t/yadca-yet-another-distributed-counting-algorithm/468
// SimpleCounter demo
// A simple distributed counter example
//
// On startup, blinks are dim BLUE which shows they are in IDLE mode waiting for a master
//
// Button press a blink to make it master of the cluster. The master will show the current count
// using the 0-342 display format decribed below under showNumber()...
//
// While a blink is actively part of a counting cluster, it will show dim GREEN on the face
// that points to its parent. All parent faces eventually lead back to the master.