Skip to content

Instantly share code, notes, and snippets.

@nbenns
nbenns / gist:1b007847940ef9c9ce6278da056c315f
Last active November 8, 2023 19:13
Scala 3 macro to return lambda
type Params[N <: Num] <: Tuple = N match {
case 0 => EmptyTuple
case _ => Double *: Params[N - 1]
}
type Num = Singleton & Int
opaque type Multivector[DIM <: Num] = Array[Double]
transparent inline def vector[N <: Num](using v: ValueOf[N], ev: (N > 0) =:= true): Params[N] => Multivector[N] =
${ vectorImpl[N]('{valueOf[N]}) }
@nbenns
nbenns / k8s-alternative-mac
Created January 31, 2022 19:16
Install Podman and Kind on Mac to replace docker-desktop
brew install podman --head # you must use head due to issues with kind compatibility
# make sure docker desktop is deleted
sudo unlink /usr/local/bin/docker
sudo ln -s /usr/local/bin/podman /usr/local/bin/docker
podman machine init
podman machine start
podman machine ssh
@nbenns
nbenns / kube.txt
Created August 8, 2020 13:28
Kubernetes
# Show pods on a node
kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=<node>
@nbenns
nbenns / PhantomPasswords.scala
Last active July 7, 2020 23:36
Making sure you don't screw up passwords via Phantom types
sealed trait SecurityState
trait PlainText extends SecurityState
trait Hashed extends SecurityState
trait Confirmed extends SecurityState
trait Verified extends SecurityState
sealed trait UpdateState
trait New extends UpdateState
trait Confirmation extends UpdateState
trait Current extends UpdateState
@nbenns
nbenns / queries.sql
Last active October 24, 2019 01:30
DB Queries
-- MySQL count same queries
SELECT substring_index(info, '/', 1), count(*) AS cnt
FROM INFORMATION_SCHEMA.PROCESSLIST
WHERE INFO IS NOT NULL
GROUP BY 1
HAVING cnt>1
ORDER BY cnt DESC;
-- MySQL show queries on DB
@nbenns
nbenns / errcompose.scala
Created January 2, 2019 23:15
Composing Errors with Coproduct
import shapeless.{ :+:, CNil, Coproduct }
import shapeless._
val error1 = Coproduct[IllegalArgumentException :+: CNil](new IllegalArgumentException)
val error2 = Coproduct[NumberFormatException :+: CNil](new NumberFormatException)
type MyError = IllegalArgumentException :+: NumberFormatException :+: CNil
object MyErrorHandler extends Poly1 {
implicit def caseConfigNotFoundException: Case.Aux[IllegalArgumentException, String] =
@nbenns
nbenns / fixgadt.scala
Created December 20, 2018 16:59
Fix a GADT Based List
import scala.language.higherKinds
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}
object Functor {
def apply[F[_]](implicit fun: Functor[F]): Functor[F] = fun
implicit class FunctorOps[F[_]: Functor, A](fa: F[A]) {
@nbenns
nbenns / Option.java
Created October 20, 2018 20:49
Transition to Option
import java.util.function.Function;
public abstract class Option<T> {
private static class Some<T> extends Option<T> {
private T val;
Some(final T val) {
this.val = val;
}
@nbenns
nbenns / timestuff.scala
Last active October 28, 2019 18:37
Using ZonedDateTime properly
def fromISOString(d: String): ZonedDateTime =
ZonedDateTime.parse(d, DateTimeFormatter.ISO_OFFSET_DATE_TIME).withZoneSameInstant(ZoneOffset.UTC)
def toISOString(d: ZonedDateTime) = d.withZoneSameInstant(ZoneOffset.UTC).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)
// parsing a "date only"
LocalDate.parse(str).atStartOfDay(ZoneOffset.UTC)
@nbenns
nbenns / SizedMatrix.scala
Last active June 10, 2018 17:09
Play with adding size of col and row as type parameters to a matrix
import shapeless.ops.hlist.Length
import shapeless.{::, HList, HNil, LUBConstraint, Nat, Poly1, Succ}
import shapeless.Nat._
abstract class Matrix[Col <: Nat, Row <: Nat, A] {
type RowLst <: HList
type ConLst <: HList
val data: ConLst
}