Skip to content

Instantly share code, notes, and snippets.

View grignaak's full-sized avatar

Michael Deardeuff grignaak

  • Seattle, WA, USA
View GitHub Profile
@grignaak
grignaak / hash.rs
Created August 23, 2016 20:38
A simple merkle tree
use std::fmt;
use ring::digest;
use ring::constant_time::verify_slices_are_equal;
use rustc_serialize::hex::{ ToHex, FromHex, FromHexError };
use rustc_serialize::base64;
use rustc_serialize::base64::{ ToBase64, FromBase64, FromBase64Error };
/// Errors that occur when parsing a hash value
pub enum ParseError {
@grignaak
grignaak / ability.rs
Last active August 23, 2016 21:22
Online ranking algorithm in rust
use std::cmp::Ordering;
pub struct Ability {
mean: f64,
variance: f64,
}
impl Ability {
pub fn zero() -> Self {
Self::with_stddev(0.0, 0.0)
@grignaak
grignaak / Vector2D.js
Last active December 13, 2015 18:58
Some JS Vector stuff
/*jshint curly:false */
/*global define:true */
define(function() {
"use strict";
function Vector2D(x,y) {
Object.defineProperties(this, {
x: { value: x, enumerable: true },
y: { value: y, enumerable: true },
_length: { writable: true }
@grignaak
grignaak / Ordered64Binary.java
Created July 10, 2012 23:13
Lexicographically ordered numbers
package net.deardeuff.ids.ordered;
import com.google.common.base.Preconditions;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
public final class Ordered64Binary {
private static final char TRAILING_CHAR = '.';
private static final Ordered64EncodeMap encoding =
new Ordered64EncodeMap("-_").changeDecodingTo(TRAILING_CHAR, (byte)0);
@grignaak
grignaak / Ability.java
Created April 28, 2012 03:37
Online ranking algorithm in java
package gaming.online.java;
public class Ability {
private final double mean;
private final double variance;
private Ability(final double mean, final double variance) {
this.mean = mean;
this.variance = variance;
}
@grignaak
grignaak / Ability.scala
Created April 28, 2012 00:04
Online ranking algorithm in scala
package gaming.online.scala
/**
* A representation of a player's ability. A normal distribution, really
*/
// private constructor
class Ability private (val mean: Double, val stddev: Double, val variance: Double) {
// Callers of this method cannot use parentheses
def skill:Double = mean - 3 * stddev
@grignaak
grignaak / ranking.clj
Created April 27, 2012 00:12
Online ranking algorithm in clojure
(ns gaming.online.ranking
"Synopsis:
(bradley-terry-full 5
(list (create-team 1 45 (list (create-player 101 (create-ability-with-stddev 25 8))))
(create-team 2 32 (list (create-player 102 (create-ability-with-stddev 25 8))))))
Updates players' ability compared with how they were expected to perform")
; defn- means a private function
(defn- sum [f xs]
@grignaak
grignaak / PubSubJs.js
Created February 1, 2012 06:17
Combining message sending in PubSubJs
/*
Copyright (c) 2010 Morgan Roderick http://roderick.dk
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
@grignaak
grignaak / ClassKey.java
Created November 22, 2011 05:40
An basic base implementation of the RoleObjectPattern
package net.deardeuff.roleobject;
import com.google.common.base.Preconditions;
/**
* A Key based on a Class
*/
public final class ClassKey<T> implements View.Key<T> {
private final Class<T> delegate;
@grignaak
grignaak / WaitFreeTest.java
Created October 4, 2011 08:51
Wait free vs SynchronizedDeque vs Partially Synchronized Queues
// The wait free algorithm is at the very bottom of the page.
package performance;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;