Skip to content

Instantly share code, notes, and snippets.

View willhbr's full-sized avatar

Will Richardson willhbr

View GitHub Profile
@willhbr
willhbr / GrahamScan.swift
Created January 31, 2016 04:55
Graham Scan in Swift
struct Point: CustomStringConvertible {
let x: Int
let y: Int
func theta(anchor: Point) -> Float {
let dx = Float(x - anchor.x)
let dy = Float(y - anchor.y)
let t = dy / (abs(dx) + abs(dy))
if dx == 0 && dy == 0 {
return 0
struct SingleFiberAccess(T)
@object : T
{% if flag? :debug %}
@fiber = Fiber.current
{% end %}
def initialize(@object)
end
macro method_missing(call)
@willhbr
willhbr / Units.kt
Created May 23, 2017 04:47
Type-safe unit conversion with composite units
/**
* Based on Ben Trengrove's example: https://gist.github.com/bentrengrove/9759a3fbb564d62e1e63f417c58a3895
*/
package units
import java.util.concurrent.TimeUnit
abstract class Unit(val suffix: String, val ratio: Double) {
internal fun convertToBaseUnit(amount: Double) = amount * ratio
internal fun convertFromBaseUnit(amount: Double) = amount / ratio
var theme = {
color: [
'#292c36',
'#dc3c3c',
'#95c76f',
'#d28e5d',
'#8485ce',
'#b73999',
'#00aba5',
'#a9acb6',
module MultiIndexable
macro index(type, *args, set_args = [] of Nil)
@%lock = Mutex.new
{% for arg in args %}
@values_by_{{ arg.var }} = Hash({{ arg.type }}, {{ type }}).new
def [](*, {{ arg.var }} key : {{ arg.type }}) : {{ type }}
@values_by_{{ arg.var }}[key]
end
def []?(*, {{ arg.var }} key : {{ arg.type }}) : {{ type }}?
@values_by_{{ arg.var }}[key]?
require "http/server"
module MaybHTTP
VERSION = "0.1.0"
class ConcattedIO < IO
def initialize(@first : IO::Memory, @second : IO)
end
def read(bytes)
if @first.closed?
size = @second.read(bytes)
else
@willhbr
willhbr / spindle.cr
Created January 4, 2020 13:02
Crystal Fiber Spindles
class Spindle [12/193]
@fiber_count = 0
@chan = Channel(Nil).new
@subspindles : Array(Spindle)? = nil
@on_error : Proc(Exception, Nil)? = nil
def initialize
end
def self.new(existing : Spindle?)
@willhbr
willhbr / matrix.py
Last active April 1, 2019 12:34
Python Matrix Determinant Calculator
#! py
def solve(matrix, mul):
width = len(matrix)
if width == 1:
return mul * matrix[0][0]
else:
sign = -1
total = 0
for i in range(width):
m = []
@willhbr
willhbr / then.sh
Created January 8, 2019 11:14
Run something after something else
#!/bin/bash
__then() {
fg
echo "> $*"
"$@"
}
alias 'then:'=__then
### Keybase proof
I hereby claim:
* I am willhbr on github.
* I am willhbr (https://keybase.io/willhbr) on keybase.
* I have a public key ASBPxIfCrNsJrL_PZpocUrsB3CWHeAeWZV7Bm_dHMutf8wo
To claim this, I am signing this object: