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 / 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 / enum.py
Last active May 13, 2023 10:06
A lightweight, very convenient function to dynamically create good enums in Python
# Makes an enum class with instances (class attributes) having uppercase
# names while the string representations can be in any case. For example,
# Color = enum_class('Color', 'red', 'amber', 'green') returns class Color
# with members Color.RED, Color.AMBER, and Color.GREEN. The __str__ instance
# 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 enum_class(classname, *values):
cls = type(classname, (), {})
@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.

@rtoal
rtoal / water_10_7_4.py
Created April 26, 2016 03:28
The famous 10-7-4 water jug problem
# 10-7-4 Water Jug Problem
# Get 2 units of water in the 7-container or the 4-container
# Starting state is (0,7,4)
# Operations are F10, F7, F4, T107, T104, T710, T74, T410, T47
# Goal states are (x,2,z) and (x,y,2)
operations = {
'F10' : lambda x, y, z: [10, y, z],
@rtoal
rtoal / timecomplexity.py
Last active January 31, 2016 19:17
Run times for various complexity functions, just for fun
# Runtimes for various complexity functions, sort of.
#
# A hacked-together little Python3 script printing runtimes for various
# complexity functions to standard output.
#
# The script isn't very general or customizable; you have to edit the
# source code to explore different functions or use different values
# of n.
import math
@rtoal
rtoal / callby.py
Created December 28, 2015 05:16
Determines whether Python is call-by-value, call-by-reference, or call-by-sharing
arg = {'x': 0}
def f(param):
same_ids = id(arg) == id(param)
different_ids = not same_ids
param = {'y': 4}
arg_changed = 'y' in arg
print('Call by value: {}'.format(different_ids))
print('Call by sharing: {}'.format(same_ids and not arg_changed))
print('Call by reference: {}'.format(same_ids and arg_changed))
@rtoal
rtoal / vector.lua
Created March 14, 2015 19:31
Example of an interesting class pattern in Lua, with operator overloading
Vector = (function (class, meta, prototype)
class.new = function (i, j)
return setmetatable({i = i, j = j}, meta)
end
prototype.magnitude = function (self)
return math.sqrt(self.i * self.i + self.j * self.j)
end
meta.__index = prototype
meta.__add = function (self, v)
return class.new(self.i + v.i, self.j + v.j)
@rtoal
rtoal / powerpowermod.jl
Created March 8, 2015 21:27
Modular double exponentiation
function powerpowermod(a, b, c, p)
if !isprime(p)
error("Go away. I not gonna even try this.")
end
return powermod(a, powermod(b, c, p-1), p)
end
@rtoal
rtoal / permutations.lua
Created February 22, 2015 20:08
Heap's permutation algorithm as a Lua 5.3 commandline script
if #arg ~= 1 then
io.stderr:write('Exactly one argument required\n')
os.exit(1)
end
function generatePermutations(n, a)
if n == 0 then
print(utf8.char(table.unpack(a)))
else
for i = 1, n do