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
final case class IPV6Full private (value: String) extends AnyVal
object IPV6Full {
val DataSegments: Int = 8
def fromString(s: String): Either[Throwable, IPV6Full] = {
def buildExpansion0s(cnt: Int): String = {
Iterator.continually('0').take(cnt).mkString(":")
}
s match {
@lloydmeta
lloydmeta / deploy-swarm.sh
Created May 2, 2017 16:15
Small bash script to make it easier to deploy/update a Docker Swarm cluster.
#!/usr/bin/env bash
set -e;
# Ths script
me="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")";
# Args
MANAGER_IP=$1
STACK_NAME=$2;
@lloydmeta
lloydmeta / DieselDemo.scala
Last active April 11, 2017 15:19
Demonstrating composing multiple Tagless Final DSLs
import cats._, implicits._
import diesel.diesel
object DieselDemo extends App {
@diesel
trait Maths[F[_]] {
def int(i: Int): F[Int]
def add(l: F[Int], r: F[Int]): F[Int]
}
@lloydmeta
lloydmeta / ListOps.hs
Last active February 20, 2017 15:21
Split and Join in Haskell
split :: Eq a => a -> [a] -> [[a]]
split s = go []
where
go acc [] = reverse acc
go acc rs =
let (next, remaining) = span (/= s) rs
in go (next : acc) (drop 1 remaining)
join :: t -> [[t]] -> [t]
join j [] = []
@lloydmeta
lloydmeta / FreekMonadIfElse.scala
Last active December 30, 2016 18:48
How to do mixed DSL branching with Free Monads via Freek. Note the Lifting DSL and for comprehension usage.
import freek._
object KVStore {
sealed trait DSL[A]
final case class Put[A](key: String, v: A) extends DSL[Unit]
final case class Get[A](key: String) extends DSL[Option[A]]
}
object WebIO {
trait Ev[A] {
}
object Ev {
implicit def witnessSelfTypeEqualsSelf[A <: { type SelfType }](implicit ev: A#SelfType =:= A) = new Ev[A] {}
}
class A {
type SelfType = A
trait Ev[A] {
}
object Ev {
implicit def evProvider[A <: { type SelfType }](implicit ev: A#SelfType =:= A) = new Ev[A] {}
}
class A {
@lloydmeta
lloydmeta / FoldRight.scala
Created September 20, 2016 17:32
FoldRight implemented using CPS in a purely functional way
object FoldRight {
def apply[A, B](list: Seq[A])(init: B)(f: (A, B) => B): B = {
@scala.annotation.tailrec
def step(current: Seq[A], thunks: List[B => B]): B = current match {
case Nil => init
case Seq(last) => thunks.foldLeft(f(last, init)) { (acc, next) => next(acc) }
case Seq(x, xs @ _*) => step(xs, { acc: B => f(x, acc) } +: thunks)
}
@lloydmeta
lloydmeta / featuresVector.m
Last active March 26, 2016 08:02
Takes a vector of indices and a featureVectorSize and produces a feature vector
function x = featuresVector(indices, featureVectorSize)
%FEATURESVECTOR takes in a indices vector, a featureVectorSize and produces a feature vector
%where x(i) is 1 if i exists in the indices and 0 if it does not
% x = FEATURESVECTOR(indices featureVectorSize) takes in a indices vector and a featureVectorSize and
% produces a feature vector from the indices.
indexed = eye(n)(word_indices, :);
[rows, cols] = size(indexed);
if rows == 1
x = indexed' > 0;
elseif rows == 0
@lloydmeta
lloydmeta / FoldRightTrampoline.scala
Last active March 11, 2016 15:44
FoldRight implemented via Trampolining.
import scala.annotation.tailrec
sealed trait Bounce[A]
case class Done[A](result: A) extends Bounce[A]
case class Cont[A](thunk: () => Bounce[A]) extends Bounce[A]
object Trampoline {
/**
* Bounce away