Skip to content

Instantly share code, notes, and snippets.

View stanch's full-sized avatar

Nick stanch

  • Product Manager at @snowplow
  • Lisbon, Portugal
View GitHub Profile
@stanch
stanch / AkkaHttp.scala
Last active January 21, 2016 18:40
Demo fragments for my talk “What can we learn from composable DSLs?”
object Utils {
val disableCache = respondWithHeaders(
`Cache-Control`(`no-cache`, `max-age`(0), `must-revalidate`, `no-store`),
`Expires`(DateTime.MinValue)
)
}
object BooksRoutes {
val routes = path("books" / Segment) { id =>
complete(id)
@stanch
stanch / SealedTest.scala
Created January 24, 2016 14:39
Extending Quicklens (https://github.com/adamw/quicklens) to support sealed traits
case class Grand(parent: Parent)
sealed trait Parent { def x: Int }
case class Child1(x: Int) extends Parent
case class Child2(x: Int) extends Parent
val grand = Grand(Child1(3))
it should "modify a field in a sealed trait" in {
modify(grand)(_.parent.x).using(_ + 1) should be (Grand(Child1(4)))
}
val l2d = Map('i' -> 1, 'v' -> 5, 'x' -> 10, 'l' -> 50, 'c' -> 100)
def compute(ds: Seq[Int]) = ds.foldRight((0, 0)) {
case (d, (acc, last)) => if (d < last) (acc - d, last) else (acc + d, d)
}
def roman(numeral: String) = compute(numeral map l2d)._1
@stanch
stanch / Example.scala
Created February 23, 2016 00:56
case class persistence visualized
case class Street(name: String, house: Int)
case class Address(street: Street, city: String)
case class Person(address: Address, age: Int)
val person1 = Person(Address(Street("Functional Rd.", 1), "London"), 35)
val person2 = person.modify(_.address.street.house).using(_ + 3)
/*
┌───────────┐ ┌───────────┐
│Person (35)│ │Person (35)│
@stanch
stanch / jwk-to-pem.scala
Last active July 4, 2024 17:27
A quick script to convert JWK public keys to .pem files
load.ivy("com.nimbusds" % "nimbus-jose-jwt" % "4.21")
load.ivy("org.bouncycastle" % "bcprov-jdk15on" % "1.51")
@
import com.nimbusds.jose.jwk._
import org.bouncycastle.util.io.pem._
import java.io._
def main(input: String) = {
@stanch
stanch / TreeSetAnimation.scala
Last active September 25, 2016 17:24
TreeSet animation using reftree (https://github.com/stanch/reftree)
import reftree.{Diagram, Utils}
import scala.collection.immutable.TreeSet
val adding = Utils.iterate(TreeSet(1), 15)(s ⇒ s + (s.size + 1))
val removing = Utils.iterate(adding.last, 15)(s ⇒ s - s.size)
Diagram().renderAnimation("treeset", tweakOptions = _.copy(
delay = 200, onionSkin = 0, diffAccent = true,
verticalSpacing = 1.1, highlightColor = "coral1", density = 75
))(adding ++ removing)
@stanch
stanch / FingerTreeAnimation.scala
Last active September 25, 2016 17:26
FingerTreeAnimation using reftree (https://github.com/stanch/reftree)
import de.sciss.fingertree.{FingerTree, Measure}
import reftree.{Diagram, Utils}
import reftree.contrib.FingerTreeInstances._
implicit val measure = Measure.Indexed
val trees = Utils.iterate(FingerTree(1), 22)(t ⇒ t :+ (t.measure + 1))
Diagram().renderAnimation("finger", tweakOptions = _.copy(
delay = 200, loop = false, onionSkin = 0, diffAccent = true,
// git clone https://github.com/stanch/reftree && cd reftree
// sbt demo
def add(n: Int)(q: Queue[Int]) = Utils.iterate(q, n + 1)(q => q :+ (q.max + 1)).tail
def remove(n: Int)(q: Queue[Int]) = Utils.iterate(q, n + 1)(q => q.tail).tail
def addRemove(n: Int)(q: Queue[Int]) = Utils.flatIterate(q)(add(n), rm(n))
val queues = Utils.flatIterate(Queue(1, 2, 3), 3)(addRemove(2))
diagram.renderAnimation(
@stanch
stanch / MapImplicit.scala
Last active December 6, 2016 20:25
When you need to map over an implicit in the current scope
case class MapImplicit[A](f: A => A)(implicit current: A) {
def in[B](code: A => B) = code(f(current))
}
// start with an implicit value
implicit val foo: Int = 3
// prints “3”
println(implicitly[Int])
@stanch
stanch / transitive.jq
Last active June 13, 2017 23:37
Finding transitive closures of resource graphs (e.g. Cloud Formation)
# Check whether the input array intersects the specified array of items
def intersects(items):
reduce .[] as $item (false; . or (items | index($item)));
# Find all references to other resources
def references(ref):
[.. | objects | ref | values];
# Remove entries whose keys do not match the predicate
def filter_keys(pred):