Skip to content

Instantly share code, notes, and snippets.

View willhbr's full-sized avatar

Will Richardson willhbr

View GitHub Profile
@willhbr
willhbr / allstrings.go
Last active August 29, 2015 14:16
Go exhaustive language calculator
package main
import (
"fmt"
"math"
)
func size(depth int, alpha *[]string) int {
return int(math.Pow(float64(len(*alpha)), float64(depth)))
}
package main
import (
"fmt"
"os"
"strings"
)
type State struct {
name string
@willhbr
willhbr / graham_scan.hs
Created July 4, 2015 13:47
Graham Scan Implementation in Haskell
import Data.List
import Data.List.Split
import Data.Ord
import System.Environment
theta :: (Num a, Eq a, Ord a, Fractional a) => (a,a) -> (a,a) -> a
theta (x1,y1) (x2,y2)
| dx == 0 && dy == 0 = 0
| dx < 0 = 2 - t
| dy < 0 = 4 + t
@willhbr
willhbr / pretty_json.rb
Created October 25, 2015 22:02
Format JSON with newlines.
def self.pretty_json(o, l=0)
o.class == Hash ? "{\n" + o.keys.map { |k| (" " * (l + 1) * INDENT) + "\"#{k.to_s}\": " + pretty_json(o[k], l + 1) }.join(",\n") + "\n" + (" " * l * INDENT) + "}" : o.class == Array ? "[\n" + o.map { |k| (" " * (l + 1) * INDENT) + pretty_json(k, l + 1) }.join(",\n") + "\n" + (" " * l * INDENT) + "]" : o.inspect
end
@willhbr
willhbr / matrix.py
Created January 31, 2016 05:15
Matrix Determinant Calculator
def calculate(mat, mul=1):
return (mul * mat[0][0] if len(mat) == 1 else sum((mul * calculate(list(map(lambda j: list(filter(lambda el: el != None, (mat[j][k] if k != i else None for k in range(len(mat))))), range(1, len(mat)))), (-1 if i % 2 == 0 else 1) * mat[0][i])) for i in range(len(mat))))
matrix = [
[1, -2, 3],
[0, -3, -4],
[0, 0, -3]
]
print(calculate(matrix))
@willhbr
willhbr / oh_god_why.swift
Last active May 7, 2016 05:28
I hope you're happy
/// Please don't do this
func iff(condition: Bool, block: () -> ()) -> Bool {
if(condition) {
block()
}
return condition
}
func iff<T>(optional: Optional<T>, block: (val: T) -> ()) -> Bool {
read("", :program, {
def(:program_length, send(program, :length))
def(:program_location, 0)
def(:input, (1, 2, 3, 4))
def(:input_location, 0)
def(:program_char, {
send(program, :substring, program_location, +(program_location, 1))
})
defmodule RegexCond do
defmacro match(matchable, do: matches) do
IO.inspect matches
de_sigged = matches |> Enum.map(fn
{:"->", context, [[lhs], code]} ->
lhs = de_sig matchable, lhs
{:"->", context, [[lhs], code]}
end)
quote do
@willhbr
willhbr / seng_stats.rb
Last active September 30, 2017 04:50
Number of words in reports vs number of words in code
#!/usr/bin/env ruby
DETAILED_MODE = !ARGV[2].nil?
begin
require 'yomu'
rescue LoadError
msg = '(Looks like this is you)'
lab = `hostname`.strip.start_with? 'cs'
puts <<-EOF
You need the 'yomu' gem to use this script.
### 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: