Skip to content

Instantly share code, notes, and snippets.

View stephennancekivell's full-sized avatar

Stephen Nancekivell stephennancekivell

View GitHub Profile
@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 / 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)
{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 / 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 =
@stephennancekivell
stephennancekivell / gist:e778101317cc55b6720a
Created February 22, 2015 22:22
WS executeRecovering
import play.api.libs.ws._
def get(url: String): Future[WSResponse] = {
val split = url.split("/")
val hostname = split(0)
val path = "/"+split(1)
def getHosts = Try(InetAddress.getAllByName(hostname).map(_.getHostAddress).toSeq)
def executeRecovering(triedHosts: Seq[String] = Nil): Future[WSResponse] = {
@stephennancekivell
stephennancekivell / jmap.all.sh
Created March 11, 2014 08:53
Get jvm memory stats for all java processes.
#!/bin/bash
ps aux | grep java
for PID in `pidof java`
do
echo
echo "jmap -heap $PID"
jmap -heap $PID
done
#!/bin/bash
#
# Notify of Homebrew updates via Notification Center on Mac OS X
#
# Author: Chris Streeter http://www.chrisstreeter.com
# Requires: terminal-notifier. Install with:
# brew install terminal-notifier
TERM_APP='/Applications/Terminal.app'
BREW_EXEC='/usr/local/bin/brew'
package com.stephenn.roguelike.util
import javax.imageio.ImageIO
import java.io.File
import java.awt.image.BufferedImage
object SpriteSplitter {
def main(args: Array[String]) {
val sprites = split(loadImage("assets/nethack-3.4.3-32x32.png"), 1280/32, 960/32)
@stephennancekivell
stephennancekivell / underscore-test.js
Last active October 5, 2016 09:15
A unit testable version of debounce from underscore.js I think underscore needs a flush feature so we can easily test our applications that use it. A underscore.flush method would allow you to test the different cases without having to make your unit tests slow. This could work similar to angular.js's $httpBackend.flush().
describe('underscore', function(){
it('shouldnt execute immediately', function(){
var hasHappened = false;
var fn = underscore.debounce(function(){
hasHappened = true;
}, 100);
expect(hasHappened).toBe(false);
#!/bin/bash
MSG=`sudo apt-get --yes dist-upgrade`
MSG_HEADING='sudo apt-get --yes dist-upgrade'
GmailSend.py -u bla@gmail.com -p bla -t bla@gmail.com -s "server update" -b "$MSG_HEADING \n\n $MSG"