Skip to content

Instantly share code, notes, and snippets.

View timruffles's full-sized avatar

Tim Ruffles timruffles

View GitHub Profile
@timruffles
timruffles / go-quiz.go
Last active August 19, 2019 07:43
What does this program output, and why? Reason it out gophers! Answers in a spoiler block - https://github.com/dear-github/dear-github/issues/166#issuecomment-236342209
package main
import "fmt"
func main() {
type person struct {
nickname string
}
ppl := []person{
@timruffles
timruffles / ban_random_int.sh
Created July 11, 2019 13:33
A bad way to generate a random init in a range using only bash built-ins. Useful in a pinch when you aren't sure what external programs are available.
# Gets an int between min max inclusive very inefficiently, but
# only using bash built-ins. More inefficient the smaller the gap
#
# usage: n=$( bad_random_int 1000 2000 )
bad_random_int() {
local min=$1
local max=$2
local n=0
while [[ "$n" -lt "$min" ]] || [[ "$n" -gt "$max" ]]; do
@timruffles
timruffles / deps.js
Created April 1, 2014 15:06
deps.js
function Container() {
this.call = exports.call.bind(null,this);
this._deps = {};
}
Container.prototype = {
get: function(dep) {
return this._deps[dep];
},
put: function(name,thing) {
if(typeof name === "object") {
@timruffles
timruffles / demo.go
Last active March 7, 2019 17:19
Go - cost of change for methods in application code. App code has many types that work together, requiring mocking.
// Let's define a type with a method that needs a fair few arguments
type Thing struct {}
func (c *Thing) Verb(a A,b B,c C,d D) {}
// We have things that depend on Thing and we want to test them, so to mock it we'll need to
// define an interface and generate a mock
type Thinger interface {
Verb(a A,b B,c C,d D)
}
@timruffles
timruffles / add.sh
Last active February 25, 2019 15:52
Go dep - adding a fork via `source`. Useful when you want to use a private fork of a public repo for instance, without updating all package paths.
SOURCE="github.com/you/thing"
FORK="github.com/other/thing"
PACKAGE="some-package"
CONSTRAINT="aabbcc"
dep ensure -add ${SOURCE}/${PACKAGE}:${FORK}.git@${CONSTRAINT}
@timruffles
timruffles / hash_converter.rb
Created May 24, 2012 09:47
rails hash formatter - hash/json to/from camel case and underscores
module HashConverter
class << self
def to_underscore hash
convert hash, :underscore
end
def to_camel_case hash
convert hash, :camelize, :lower
end
def convert obj, *method
case obj
@timruffles
timruffles / js-values-sketch.c
Created July 13, 2018 07:57
Representing JsValues for JS -> C compiler
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdint.h>
#include <string.h>
/**
* So, plan is to store the values for the JS compiler
* in a union, nicking Lua's idea. That way immediate
* values like numbers live inside the JSValue wrapper,
@timruffles
timruffles / deploy.sh
Last active June 22, 2018 19:01
pure bash provisioning - node + postgres on ubuntu 15.10. Whole /deployment directory has been scp'd to machine, containing all config files etc
#!/bin/bash
# run on source machine to build and then copy over
set -eo pipefail
main() {
if [[ -z $SKIP_BUILD ]]; then
grunt build
fi
@timruffles
timruffles / functors.ts
Created June 7, 2016 09:30
functor in TypeScript - this is probably in violation of many functor laws :/
interface Functor<T> {
map(mapper: (x: T) => T): Functor<T>;
ret(x: T): Functor<T>;
}
class Maybe<T> implements Functor<T> {
constructor(private value: T) {}
private nowt() {
@timruffles
timruffles / enums.ts
Last active June 15, 2017 09:34
string enums (ish, with some caveats) before ts 2.4
// cast the strings as any
enum StringEnum {
a = 'a' as any,
b = 'b' as any,
}
const a = StringEnum.a;
const x = 'a';
const y = 1;