Skip to content

Instantly share code, notes, and snippets.

@qerub
qerub / sum-time-durations.clj
Created January 18, 2014 00:29
Summing time durations (given as a string) with Clojure and Joda-Time
;; lein try joda-time org.clojure/algo.generic
;; Let's first shave the parsing yak:
(import (org.joda.time.format PeriodFormatterBuilder))
(def h:m-formatter
(-> (PeriodFormatterBuilder.)
(.appendHours)
(.appendSeparator ":")
@qerub
qerub / HttpsFilter.java
Last active November 6, 2022 00:12
Servlet filter for forcing HTTPS when behind a SSL termination proxy that sends X-Forwarded-Proto
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import static java.lang.String.format;
public class HttpsFilter implements Filter {
@qerub
qerub / null-guard.sjs
Created April 18, 2014 23:36 — forked from aaronpowell/null-guard.sjs
The ?. operator from C# for JavaScript via Sweet.js
// This version allows LHS to be any expression
// (and makes sure it's only evaluated once by storing the result)
let (?.) = macro {
rule infix { $lhs:expr | $rhs:ident } => {
(function ($tmp) {
return $tmp === null ? null : $tmp.$rhs;
})($lhs)
}
}
@qerub
qerub / lifx-sunrise-simulator.rb
Last active September 9, 2020 11:44
LIFX Sunrise Simulator
require "lifx" # http://www.rubydoc.info/gems/lifx
def calculate_color(i) # 0 <= i <= 1
h = [40 * 2 * i, 40].min # will be 40 (yellow) at i=1/2 and stay there
s = 1.0 - [(i - 0.5) * 2, 0].max # will be 1 until i=1/2 and then go down to 0
b = i
LIFX::Color.hsbk(h, s, b, LIFX::Color::KELVIN_MIN)
end
duration = 10 * 60 # seconds
@qerub
qerub / index.md
Last active November 6, 2016 19:17
Useful Docker commands

How to remove dangling/untagged images

docker images -q -f dangling=true | xargs docker rmi

How to remove stopped/exited containers

docker ps -q -f status=exited | xargs docker rm
@qerub
qerub / cwd.sh
Last active August 29, 2015 14:02
Bash script to exec program in given working directory
#!/bin/bash
# Usage: cwd.sh /app /usr/bin/python example.py
cd $1 && exec "${@:2}"
@qerub
qerub / gist:96104c9844aa399b679e
Created July 28, 2014 13:24
Integrating Thrift's JSON serializer with Jackson
class ThriftSerializer extends JsonSerializer<TBase> {
@Override
public void serialize(TBase value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
try {
TProtocolFactory f = new TSimpleJSONProtocol.Factory();
String s = new TSerializer(f).toString(value);
jgen.writeRawValue(s);
}
catch (TException e) {
throw new IOException(e);
@qerub
qerub / fancy-defn.clj
Last active August 29, 2015 14:07
[Clojure] fancy-defn: Proof of concept of schema.core/defn augmented with automatically generated clojure.core.typed annotations via circle.schema-typer
(ns fancy-defn
(:require [schema.core :as s]
[clojure.core.typed :as t]
[circle.schema-typer :as st]))
;; Schemas created with s/defn end up using this.
(defmethod st/convert schema.core.One [schema]
(assert (= false (:optional? schema))) ;; No support for optional arguments yet.
(st/convert (:schema schema)))
@qerub
qerub / Optionals.java
Last active August 29, 2015 14:08
Java 8: The Missing Parts, Chapter 1: Optionals.java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
public class Optionals {
@qerub
qerub / lifx-http-api.clj
Last active April 3, 2016 11:06
[Clojure] Interacting with LIFX's HTTP API using schemas
;; https://github.com/plumatic/schema
;; https://github.com/dakrone/clj-http
;; http://api.developer.lifx.com/docs/set-state
(ns lifx-http-api
(:require [schema.core :as s]
[clj-http.client :as http]))
(def ^:dynamic *base-url* "https://api.lifx.com/v1/lights")
(def ^:dynamic *auth-token* "<get yours at https://cloud.lifx.com/settings>")