Skip to content

Instantly share code, notes, and snippets.

View rtoal's full-sized avatar
💭
Copying and pasting

Ray Toal rtoal

💭
Copying and pasting
View GitHub Profile
@rtoal
rtoal / sizes.cpp
Created October 2, 2017 03:24
A C++ program with a crazy macro to print ranges of numeric types
#include <cstdint>
#include <climits>
#include <cassert>
#include <iostream>
using namespace std;
#define show_type_range(t, min, max)\
cout << #t << " (" << sizeof(t) << " bytes): " << min << ".." << max << '\n';
@rtoal
rtoal / htmlCollectionIteratorPolyfill.js
Created December 14, 2016 16:52
Polyfill allowing iteration of Safari's HTMLCollection objects
// Polyfill because Safari's HTMLCollections are not iterable
if (typeof HTMLCollection.prototype[Symbol.iterator] !== 'function') {
HTMLCollection.prototype[Symbol.iterator] = function () {
let i = 0;
return {
next: () => ({done: i >= this.length, value: this.item(i++)})
}
};
}
@rtoal
rtoal / [-8] < 0.md
Created June 18, 2017 04:39
Is [-8] < 0?

Clojure

user=> (< [-8] 0)

ClassCastException clojure.lang.PersistentVector cannot be cast to java.lang.Number

Python 2 (BOOOOOOOOOOO)

&gt;&gt;&gt; [-8] &lt; 0
@rtoal
rtoal / offside.js
Created March 12, 2017 01:34
An illustration of preprocessing lines of text for later use of the offside rule
const program = `
def f(x):
y = 0
while x < y:
if p:
x = 3
else:
for x in range(10):
@rtoal
rtoal / translation-telephone.md
Last active January 11, 2017 08:10
A translation telephone example

English

She's feeling under the weather so will work from home today, with a laptop and cup of tea

Afrikaans

Sy gevoel onder die weer so sal werk van die huis af vandag, met 'n skootrekenaar en koppie tee

Hindi

वह मौसम के अंतर्गत महसूस किया तो एक लैपटॉप और एक कप चाय के साथ आज घर से काम करेंगे

Bulgarian

@rtoal
rtoal / mongo-getting-started.js
Last active January 10, 2017 06:58
Getting started with the mongo shell
show dbs
use hello
db.people.insert({name: "Abdul", age: 22, likes: ["skiing", "hiking"]})
db.people.insert({name: "Barbara", age: 20, country: "UK"})
db.people.insert({name: "Chi", age: 40, country: "CN",
job: {title: "welder", salary: {currency: "EUR", amount: 50000}}})
@rtoal
rtoal / Arithmetic.json
Created December 8, 2016 22:07 — forked from mroeder/Arithmetic.json
Arithmetic Grammar for Ohm
[
{
"text": "2 * (42 - 1) / 9",
"startRule": "Exp",
"shouldMatch": true
},
{
"text": "1+2*3",
"startRule": "Exp",
"shouldMatch": true
@rtoal
rtoal / enum.rb
Created December 4, 2016 16:41
A lightweight, very convenient function to dynamically create good enums in Ruby
# Makes an enum class whose instances are all constants with uppercase names,
# but whose case of string representations are customizable. For example,
# Color = make_enum_class('red', 'amber', 'green') returns the class Color
# with members Color::RED, Color::AMBER, and Color::GREEN. The to_s methods
# produce 'red', 'green', and 'blue', respectively. To get the instance
# from the string, use, for example, Color.from_string('blue'), which will
# return Color.BLUE.
def make_enum_class(*constants)
cap = ->(s){s.upcase.gsub(/ /, '_')}
@rtoal
rtoal / PageTest.java
Created November 14, 2016 21:24
A pretty cool builder pattern I discovered in Java
class Pager {
private int offset;
private int limit;
private Pager() {}
public static Pager withOffset(int offset) {return new Pager().offset(offset);}
public Pager offset(int offset) {this.offset = offset; return this;}
public int offset() {return this.offset;}
@rtoal
rtoal / api-decision.md
Last active September 24, 2016 19:22
A general question about bulk operations in RESTful APIs

An API Design Choice

Let's say we have an API endpoint

POST /customers

which creates a customer given a suitable JSON document and returns a 201 CREATED on success.