Skip to content

Instantly share code, notes, and snippets.

View wspringer's full-sized avatar

wilfred@eastpole.nl wspringer

View GitHub Profile
const numberRegexp = /^[0-9]+$/;
export type Validator<T> = (t: T) => boolean
export function validZip(s: string) {
return s.length === 5 && numberRegexp.test(s);
}
validZip("09809"); // true
let numberRegexp = /^[0-9]+$/;
export interface Validator<T> {
isAcceptable(t: T): boolean;
}
export class ZipCodeValidator implements Validator<String> {
isAcceptable(s: string) {
return s.length === 5 && numberRegexp.test(s);
}
@wspringer
wspringer / 0_reuse_code.js
Created March 14, 2016 09:12
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@wspringer
wspringer / grabs.sh
Last active March 14, 2016 09:14
List of all dependencies, ready to be processed by something else, like https://github.com/sindresorhus/package-json
npm ls | gawk 'match($0, "[| +--`]*([^@]*).*", ary) { print ary[1] }' | sort | uniq
@wspringer
wspringer / ng-scroll-support.coffee
Created October 28, 2015 08:07
Angular directive for tracking scroll events
angular.module 'ngScrollSupport', []
.directive 'ngScrollSupport', ->
restrict: 'A'
link: (scope, element, attrs) ->
atTop = true
atBottom = false
check = ->
scrollTop = element.prop 'scrollTop'
scrollHeight = element.prop 'scrollHeight'
clientHeight = element.prop 'clientHeight'
@wspringer
wspringer / bower.coffee
Created January 23, 2015 16:28
Bower support for Metalsmith
bower = (files, metalsmith, done) ->
include = (root, included) ->
for file in included
contents = readFileSync(file)
files["#{root}/#{basename(file)}"] =
contents: contents
include('css', lib.self().ext('css').files)
include('js', lib.self().ext('js').files)
include('fonts', lib.self().ext(['eot','otf','ttf','woff']).files)
done()
import Cocoa
enum Try<T>: Printable, DebugPrintable {
// Captures a successful outcome
case Success(@autoclosure() -> T)
// Captures a failure, including the root cause, which for now can be anything
case Failure(Any)
@wspringer
wspringer / Tuple2Traversable.scala
Created October 10, 2012 21:00
Need something that wraps a traversable collection of tuples. Does this approach make sense at all?
/**
* A Traversable-alike abstraction of a traversable collection of tuples, without actually creating the tuples.
* I'd hope this would prevent me from polluting the heap with actual Tuple2 instances I never actually need.
*/
trait Tuple2Traversable[+A, +B] {
def foreach[T](fn: (A, B) => T)
def map[T](fn: (A, B) => T): Traversable[T] = new Traversable[T] {
def foreach[U](f: (T) => U) {
@wspringer
wspringer / jaro.scala
Created July 2, 2012 10:56
Jaro distance in Scala
import math._
object Jaro {
def distance(first: String, second: String) = {
val maxLength = max(first.length, second.length)
val maxDistance = (maxLength / 2) - 1
var matches = 0
var halfTranspositions = 0
for (i <- 0 until first.length) {
@wspringer
wspringer / dashboard.haml
Created March 25, 2012 20:29
Super compact JMX dashboard, based on Jolokia, ICanHaz and HAML
!!!
%html
%head
%title JMX Console
%script(src="js/ICanHaz.min.js" language="javascript")
%script(src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" language="javascript")
%script(src="js/json2.js" language="javascript")
%script(src="js/jolokia-min.js" language="javascript")
%script(src="js/jolokia-simple-min.js" language="javascript")
%link(href="css/style.css" rel="stylesheet" type="text/css")