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
def tapRender[B: ToRefTree](value: B) = { render(value); value }
def zipperControl[A](zipper: Zipper[A])(implicit toRefTree: ToRefTree[Zipper[A]]): Unit = {
Iterator
.continually(Console.in.read())
.takeWhile(_ != 'q')
.filter(Set('w', 'a', 's', 'd'))
.foldLeft(tapRender(zipper)) {
case (z, 'w') ⇒ tapRender(z.tryMoveUp.orStay)
case (z, 'a') ⇒ tapRender(z.tryMoveLeft.orStay)
@stanch
stanch / morph.jq
Last active June 13, 2017 23:35
jq morphisms
## Definitions
# Functor map
def fmap(f):
if type == "object" then
map_values(f)
else
if type == "array" then
map(f)
else
@stanch
stanch / security-group-json-graph.sh
Last active April 4, 2017 14:24
A tool for plotting security rules found in a CloudFormation template
#!/bin/bash
#
# MIT License
#
# Copyright (c) 2017 Nick Stanchenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@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):
@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])
// 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 / 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,
@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 / jwk-to-pem.scala
Last active February 27, 2018 22:47
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 / 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)│