Skip to content

Instantly share code, notes, and snippets.

View stephennancekivell's full-sized avatar

Stephen Nancekivell stephennancekivell

View GitHub Profile
@stephennancekivell
stephennancekivell / PreparedStatementBuilder
Created November 28, 2016 11:24
scala DSL for dynamically building cached cassandra prepared statements in scala
package com.mgl.digital.dataservices.utils
import com.datastax.driver.core._
class PreparedStatementBuilder(session: Session) {
private var statementCache: Map[String, PreparedStatement] = Map()
def insertInto(table: String): Insert =
{sealed trait Tree
case object EmptyTree extends Tree
case class Leaf(label: String, popularity: Double) extends Tree
case class Node(popularity: Double, left: Tree, right: Tree) extends Tree
implicit val treeOrdering = new Ordering[Tree] {
@stephennancekivell
stephennancekivell / FutureOps.scala
Last active August 1, 2017 09:52
Scala Future gatherUnordered
/**
* Like Future.sequence but but limits the parallelism to only process so many futures at once.
* Useful if you have lots of future operations but dont want to overload something with too many at once.
*/
def gatherUnordered[A, B](parallelism: Int,
xs: Seq[A])(fn: A => Future[B])(implicit ex: ExecutionContext): Future[Seq[B]] = {
def go(todo: Seq[A], inprogress: Seq[Future[B]], acc: Seq[B]): Future[Seq[B]] = {
if (inprogress.size < parallelism && todo.nonEmpty) {
val numToAdd = parallelism - inprogress.size
val (toStart, stillTodo) = todo.splitAt(numToAdd)
@stephennancekivell
stephennancekivell / gather.scala
Created August 1, 2017 09:53
Scala Future gatherUnordered Usage
import $ivy.`net.databinder.dispatch::dispatch-core:0.13.1`
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import dispatch._, Defaults._
val svc = url("http://httpbin.org/ip")
def get() = Http.default(svc OK as.String)
def getAndTime(idx: Int) = {
@stephennancekivell
stephennancekivell / traverseCancellable.js
Created December 1, 2017 06:05
javascript traverse cancellable
/*
* traverse cancellable takes an Array of promise's running them in parallel returning a single promise of their result and cancel function.
*
* @inputThunks Array[() => Promise[A]] an array of promises not yet started, thunks () => Promise
* @returns Object {
* promise: Promise[Array[A]]
* cancel: Function() to cancel
* }
*/
export function traverseCancellable(inputThunks) {
@stephennancekivell
stephennancekivell / gatherUnordered.js
Created December 5, 2017 02:32
GatherUnordered.js
/*
* gather unordered takes an Array of promise's running them in parallel returning a single promise of their result and cancel function.
*
* If a error is encountered the first error will be returned, remaining unordered thunks will not be executed.
*
* @inputThunks Array[() => Promise[A]] an array of promises not yet started, thunks () => Promise
* @parallism number of promises to run at once
* @returns Object {
* promise: Promise[Array[A]]
* cancel: Function() to cancel
# results from https://github.com/stephennancekivell/circe-argonaut-compile-times
# 100 case classes with up to 15 values
play-json 2.12
23
19
play-json.topic 2.12
17
20
circe 2.12
@stephennancekivell
stephennancekivell / diffjavamap.scala
Created June 7, 2018 23:37
scala diff recursive java Map[String,Any]
val skip = Set.empty[String]
import java.util.{Map => JMap, List => JList}
def diff(a: JMap[String, Any], b: JMap[String, Any], prefix: String = ""): Unit = {
(a.keySet().asScala.toSet ++ b.keySet().asScala.toSet).filterNot(skip.contains).foreach { key =>
val prefixKey = s"$prefix.$key"
(Option(a.get(key)), Option(b.get(key))) match {
case (Some(av), Some(bv)) if av.isInstanceOf[JMap[String, Any]] && bv.isInstanceOf[JMap[String, Any]] =>
diff(av.asInstanceOf[JMap[String, Any]], bv.asInstanceOf[JMap[String, Any]], prefixKey)
@stephennancekivell
stephennancekivell / gist:39eeabe0ef085cbcb7a06816628c7994
Created February 18, 2020 04:57
terraform docker 2.7.0 rebuild
terraform apply
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# docker_container.nginx will be created
+ resource "docker_container" "nginx" {
@stephennancekivell
stephennancekivell / gist:fb5a3113daa6d47a1350cc25721f386f
Created February 18, 2020 04:58
terraform docker 2.7.0 rebuild debug
terraform apply
2020/02/18 15:58:02 [INFO] Terraform version: 0.12.13
2020/02/18 15:58:02 [INFO] Go runtime version: go1.12.9
2020/02/18 15:58:02 [INFO] CLI args: []string{"/usr/local/Cellar/tfenv/1.0.2/versions/0.12.13/terraform", "apply"}
2020/02/18 15:58:02 [DEBUG] Attempting to open CLI config file: /Users/stephen/.terraformrc
2020/02/18 15:58:02 [DEBUG] File doesn't exist, but doesn't need to. Ignoring.
2020/02/18 15:58:02 [INFO] CLI command args: []string{"apply"}
2020/02/18 15:58:02 [DEBUG] checking for provider in "."
2020/02/18 15:58:02 [DEBUG] checking for provider in "/usr/local/Cellar/tfenv/1.0.2/versions/0.12.13"
2020/02/18 15:58:02 [DEBUG] checking for provider in ".terraform/plugins/darwin_amd64"