Skip to content

Instantly share code, notes, and snippets.

View theon's full-sized avatar

Ian Forsey theon

View GitHub Profile
import scala.io.Source
val reverseLetters = ( ('a' to 'z') zip ('a' to 'z').reverse ).toMap.withDefault(identity)
val words = Source.fromFile("/usr/share/dict/words").getLines.toVector
def isWordMatch(word: String) = {
val reverseLetterWord = word.toLowerCase.map(reverseLetters)
reverseLetterWord.reverse == word
}
@theon
theon / .bash_profile_prompt.sh
Last active October 19, 2015 09:25
My bash prompt
RESET="\[\017\]"
NORMAL="\[\033[0m\]"
YELLOW="\[\033[33;1m\]"
function colour256() {
echo "\[\033[38\;5\;$1m\]";
}
# Alternative to \W. \W works in $PS1, but bash can not interpret it
function pwdbn() {
var http = require('http'),
httpProxy = require('http-proxy');
//
// A node http-proxy application to sit in front of a cube server (https://github.com/square/cube)
// and do some primitive authentication to require a password (specified in the 'cube-password' header
// in order call the write apis, but still allowing anyone to call the read apis.
//
httpProxy.createServer(function (req, res, proxy) {
if(req.method !== 'OPTIONS' && req.method !== 'GET') {
@theon
theon / plant-watering-chart.html
Created January 13, 2013 19:28
Example of a cubism chart used to chart moisture levels from an arduino plant watering system that writes plant moisture levels to a cube server. See: http://theon.github.com/plant-watering-with-arduino.html
<html>
<head>
<script type="text/javascript" src="https://raw.github.com/mbostock/d3/master/d3.min.js"></script>
<script type="text/javascript" src="https://raw.github.com/square/cubism/master/cubism.v1.min.js"></script>
<style>
.time-series {
font-family: "Helvetica Neue", Helvetica, sans-serif;
margin: 30px auto;
width: 800px;
position: relative;
#!/bin/sh
test -f ~/.sbtconfig && . ~/.sbtconfig
SBT_LAUNCH=/usr/local/Cellar/sbt/0.12.1/libexec/sbt-launch.jar
# Take leading integer as debug port and not sbt args
DEBUG_PORT=$1
SBT_ARGS=`echo "$@" | grep -oE "[^0-9].*"`
exec java -Xmx512M -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${DEBUG_PORT} ${SBT_OPTS} -jar $SBT_LAUNCH $SBT_ARGS
import akka.actor.ActorRef
import akka.util.Timeout
import concurrent.{Promise, Future, ExecutionContext}
import spray.http.{HttpMethods, HttpResponse, HttpRequest}
import spray.client.pipelining._
import spray.http.StatusCodes.Redirection
import concurrent.Promise._
import spray.http.HttpMethods._
import scala.concurrent.duration._
val sets = List(
name.map(n => Field("name", n)),
share.map(s => Field("shared", s))
).flatten
val query = sets.zipWithIndex.foldLeft(Q.u + "UPDATE wishlist SET "){
case (q, (f, 0)) => q + f.col + " = " +? f.value
case (q, (f, _)) => q + ", " + f.col + "=" +? f.value
}
//Experiment trying to mimic the java syntax
// isTrue ? thing: otherThing
//NOTE: This is just for proving a point about scala's great expressivness.
// Don't use this for anything real. In real code use if(isTrue) thing else otherThing
class QuestionWithSuccess[T](cond: Boolean, success: T) { def |(fail: T) = if(cond) success else fail }
class Question(cond: Boolean) { def ?[T](t: T) = new QuestionWithSuccess(cond, t) }
implicit def boolToQuestion(b: Boolean) = new Question(b)
true ? "ham" | "cheese"
f recoverWith {
case _: TimeoutException ⇒
Future.failed(new TimeoutException("Alerts API Timeout"))
}
f recoverWith {
case _: TimeoutException ⇒
log.error("Alerts API took too long")
Future.failed(new TimeoutException("Alerts API Timeout"))
}