Skip to content

Instantly share code, notes, and snippets.

View lloydmeta's full-sized avatar
🏠
Working from home

Lloyd lloydmeta

🏠
Working from home
View GitHub Profile
@lloydmeta
lloydmeta / PrettyDuration.scala
Created June 15, 2021 03:11
Scala Duration (incl FiniteDuration) pretty print in a way that can be parsed by `Duration.apply`
object PrettyDuration {
import scala.concurrent.duration._
def show(duration: Duration): String = {
if (Duration.Inf == duration) {
"Inf"
} else if (Duration.MinusInf == duration) {
"MinusInf"
} else {
duration.toCoarsest.toString
}
@lloydmeta
lloydmeta / AnyValAwareMatchers.scala
Last active April 27, 2021 07:42
Scala `AnyVal`-aware helper method around Mockito's `any`
import scala.reflect.macros.whitebox
import scala.language.experimental.macros
trait AnyValAwareMatchers {
/**
* An `any` matcher implementation macro that is aware of [[AnyVal]] wrapper classes.
*
* When using it with custom [[AnyVal]] classes, make sure to pass the concrete type.
@lloydmeta
lloydmeta / Transform.scala
Last active August 24, 2018 16:46
[Scala] Transforms one case class into another as long as the fields are a subset of the other via LabelledGeneric; Scastie link https://scastie.scala-lang.org/lloydmeta/ra0dvrV5Q2uxruQKoR020Q/2
@ import $ivy.{`com.chuusai::shapeless:2.3.3`}
import shapeless._, ops.hlist.Align, ops.hlist.SelectAll, SelectAll._
class Transform[T] {
// The fun stuff. Given an S, returns a T, if S has the right (subset of) fields
def apply[S, SR <: HList, TR <: HList](s: S)(
implicit
genS: LabelledGeneric.Aux[S, SR],
@lloydmeta
lloydmeta / .gitconfig
Last active January 27, 2018 03:37
git refresh-from upstream
[alias]
retest-commit = commit --allow-empty -m \"Retest - empty commit\"
current-remote = !git config branch.`git name-rev --name-only HEAD`.remote
current-branch = symbolic-ref --short HEAD
refresh-from = !git pull $1 $(git current-branch) && git push && :
refresh-master = !git fetch -fu $1 master:master && git push $(git current-remote) master && :
@lloydmeta
lloydmeta / ulam_spiral.rs
Created December 25, 2017 08:49
Given the index of a point in an Ulam Spiral, return the cartesian Coordinates in O(1)
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Coords {
pub x: i64,
pub y: i64,
}
/// Given an Ulam spiral index, returns the Cartesian coordiantes
/// for it.
///
/// # Examples

Keybase proof

I hereby claim:

  • I am lloydmeta on github.
  • I am lloydmeta (https://keybase.io/lloydmeta) on keybase.
  • I have a public key ASDV-Q5mo7JtznoCLhHrmY3Dug4Qe9ISTKQDM4bIxOuj-wo

To claim this, I am signing this object:

@lloydmeta
lloydmeta / loop.rs
Last active September 10, 2017 02:54 — forked from xevix/loop.rs
pub fn combine_all_option<T>(xs: &Vec<T>) -> Option<T>
where
T: Semigroup + Clone,
{
xs.iter().skip(1).fold(
xs.first().map(|v| v.clone()),
|acc, next| acc.map(|v| v.combine(next)),
)
}
@lloydmeta
lloydmeta / NewTypeJsonFormats.scala
Created August 16, 2017 03:10
Play JSON formatter generater for NewType wrapper types
import play.api.libs.json._
object NewTypeJsonFormats {
/**
* Returns a Json Formatter for newtype wrappers
*
* Adapted from https://groups.google.com/d/msg/play-framework/zDUxEpEOZ6U/-7BpwI8iBCoJ
* Usage: NewTypeJsonFormats(UserId.apply)(UserId.unapply)
@lloydmeta
lloydmeta / localstack.sh
Created August 15, 2017 08:18
Local AWS Stack via localstack that supports non-python lambda functions
#!/usr/bin/env bash
docker run -it -e LAMBDA_EXECUTOR="docker" -p 8080:8080 -p 443:443 -p 4567-4582:4567-4582 -p 4590-4593:4590-4593 -v "/private/var/folders/localstack:/tmp/localstack" -v "/var/run/docker.sock:/var/run/docker.sock" -e DOCKER_HOST="unix:///var/run/docker.sock" -e HOST_TMP_FOLDER="/private/var/folders/localstack" "localstack/localstack"
@lloydmeta
lloydmeta / BaseChange.hs
Last active June 9, 2017 15:43
Dec to BaseN and back
module BaseChange where
import Data.Foldable (foldl')
-- Turns a base 10 number into a list representation in another base
-- based on the zero and digit representation you pass in
-- :: Integral -> zero -> [digits] -> [representation]
decToBaseN :: Integral a => a -> b -> [b] -> [b]
decToBaseN i zero digits = if base == 0 then [] else go i []
where