Skip to content

Instantly share code, notes, and snippets.

View maetl's full-sized avatar

Mark Rickerby maetl

View GitHub Profile
@maetl
maetl / build.rb
Created July 17, 2019 00:57
Minimalist documentation site generator
require "kramdown"
require "mustache"
require "fileutils"
WEB_DIR = "./public"
DOCUMENT_TPL = ".site/templates/document.html"
INDEX_TPL = ".site/templates/index.html"
HOME_TPL = ".site/templates/homepage.html"
Mustache.template_path = ".site/templates/partials"
@maetl
maetl / pickWeightedValue.js
Created August 7, 2018 02:49
Pick a weighted value from an object in JS
const rules = {
'90%': 0.9,
'10%': 0.1
}
function pickWeightedValue(rules) {
const productions = Object.keys(rules)
const weights = Object.values(rules)
let maxIndex = 0
@maetl
maetl / karla.html
Created February 10, 2018 13:40
Test of a variation of Karla that supports Māori macrons properly.
<html>
<head>
<link rel="stylesheet" media="screen" href="https://fontlibrary.org/face/karmilla" type="text/css"/><style>
h1, h2, p {
font-family: 'Karmilla', sans-serif;
}
</style>
</head>
<body>
<h1>te ao māori</h1>
@maetl
maetl / subgraph.rb
Created June 10, 2017 13:22
Subgraph isomorphism matching for small in-memory Ruby graphs.
$LOAD_PATH << "./lib"
require 'mementus'
# Prototype of subgraph isomorphism search using Ullmann’s original 1976
# algorithm [1] as the foundation for a depth-first scan that moves forward
# through each possible outgoing edge, then attempts to confirm the match by
# doing a reverse edge lookup, comparing the query to the target graph.
#
# This is known to be a NP-complete problem but despite being exponential in
# theory, in practice it’s possible to achieve reasonable results by using
@maetl
maetl / pipe.js
Created March 25, 2017 12:09
Lazy pipeline API using wrapped iterators and generators to pump values through the chain
function Pipe(sequence) {
const wrappedIterator = sequence[Symbol.iterator]();
this.next = wrappedIterator.next.bind(wrappedIterator);
}
Pipe.step = function(generator) {
return new Pipe({
[Symbol.iterator]: generator
});
}

Peters told the Lords they should Brexit, to again look to the Reserve Bank to tell us how they're going to do themselves out of control and when is this incompetent Clark Government going to be done. No chance of that with New Zealand First, obviously there's still a lot of figures were spilling off the Tertiary Minister's printing press, the custodian of the market. No chance of that with New Zealand First, obviously there's still a lot of work to be about the crisis that they've tried unsuccessfully to get the incumbents have thrown up their hands in horror at the response their billion dollar binge on roads, power poles and sewers has been fierce with no political punches being pulled, even between MPs on the topic on Black Friday last month. Peters did stop for a breather though and was asked about his Brexit buddy Nigel Farage's resignation as the upper price it could fetch at auction.

require 'fiber'
class Step
def initialize(source, yielder=nil)
@context = Fiber.new do
source.each do |element|
if yielder
yielder.call(element)
else
Fiber.yield(element)
require 'eventmachine'
class Pipeline < EM::Completion
def initialize(*steps)
@steps = steps
@steps.each_cons(2) do |pair|
pair.first.callback do |input|
EM.next_tick do
pair.last.call(input)
@maetl
maetl / eight.rb
Created November 14, 2015 04:12
Magic numbers
(1..9).reduce('') do |m,i|
s = i.to_s
m << s
r = "#{m} * 8 + #{i.to_s}"
puts r + " = " + eval(r).to_s
m
end
@maetl
maetl / filter.rb
Last active August 29, 2015 14:18
Several more or less hacky ways to generate filter parameters as described in the JSON API recommendation (http://jsonapi.org/recommendations/). It looks as if using “filter[name]=value” directly is incompatible with the URI Template specification documented in RFC6570, but “filter%5Bname%5D=value” works. Unsure if that’s expected or not. The Fi…
require 'addressable/template'
class Filter::RawParams
def initialize(params)
@params = params
end
def encode_value(value)
Addressable::URI.encode_component(value, Addressable::URI::CharacterClasses::UNRESERVED)
end