Skip to content

Instantly share code, notes, and snippets.

View hrb90's full-sized avatar

Harrison Brown hrb90

View GitHub Profile
@hrb90
hrb90 / arc_segment_intersection.js
Last active January 6, 2018 16:25
arc/segment intersection in javascript
// returns an array of real solutions to the quadratic ax^2 + bx + c = 0
const quadratic = (a, b, c) => {
if (a === 0) return [ -c/b ];
const discriminant = b*b - 4*a*c;
if (discriminant < 0) {
return [];
} else {
const y = - b / (2 * a);
const x = Math.sqrt(discriminant) / (2 * a);
return [y + x, y - x];
@hrb90
hrb90 / banach-tarski.purs
Last active January 3, 2018 22:37
What's an anagram of Banach-Tarski? Banach-Tarski Banach-Tarski.
module BanachTarski where
import Prelude
import Data.Foldable (and, foldr)
import Data.Group.Free (FreeGroup(..), Signed(..), free)
import Data.List ((:))
import Data.Tuple (Tuple, snd)
import Math (Radians, acos, cos, sin)
module Main where
import Prelude hiding (between)
import Control.Monad.Eff (Eff)
import Data.Array (foldr, many, reverse, some, uncons)
import Data.Bifunctor (lmap)
import Data.Either (Either)
import Data.Functor (voidRight)
import Data.Maybe (Maybe(..))
@hrb90
hrb90 / ProfunctorLens.js
Last active November 15, 2017 21:20 — forked from tel/ProfunctorLens.js
Pure Profunctor Lenses in ES6
/**
* Lens types.
* ===========
*
* a * b = {fst: a, snd: b}
* a + b = {index: Boolean, value: a | b}
*
* Iso s t a b = forall (~>) . Profunctor (~>) => (a ~> b) -> (s ~> t)
* Lens s t a b = forall (~>) . Strong (~>) => (a ~> b) -> (s ~> t)
* Prism s t a b = forall (~>) . Choice (~>) => (a ~> b) -> (s ~> t)
@hrb90
hrb90 / purescript-confusion.purs
Last active November 7, 2017 18:12
purescript-by-example chapter 9 lsystem problem 1
module Example.LSystem where
import Prelude
import Control.Monad.Eff (Eff)
import Data.Array (foldM)
import Data.Maybe (Maybe(..))
import Graphics.Canvas (CANVAS, closePath, fillPath, getCanvasElementById, getContext2D, lineTo, moveTo, setFillStyle, setStrokeStyle, strokePath)
import Math as Math
import Partial.Unsafe (unsafePartial)
@hrb90
hrb90 / binary_tree_invert.hs
Last active September 1, 2017 16:23
i is a haskell programmer
{-# LANGUAGE DeriveFunctor #-}
data MaybePair a = Pair (Maybe a) (Maybe a)
deriving (Eq, Show, Functor)
data BinaryTree a =
Node a (MaybePair (BinaryTree a))
deriving (Eq, Show)
invert :: BinaryTree a -> BinaryTree a
@hrb90
hrb90 / monadic-fizzbuzz.js
Last active November 28, 2017 01:31
FizzBuzz with monads!
/***
You've probably seen FizzBuzz before. If you need a reminder, the problem is
this: Print out a list of numbers from 1 to 100, except that if a number's
divisible by 3, replace it with "Fizz", and if it's divisible by 5, replace it
with "Buzz". If it's divisible by 3 and 5, replace it with "FizzBuzz".
This is pretty straightforward to do in JavaScript with a for loop and some
if-then blocks, but it quickly gets more complicated. What if we want to add
"Quux" at the end of every number divisible by 7? And also add "Prime" at the
@hrb90
hrb90 / hart-mas-colell.py
Created July 26, 2017 18:49
Python implementation of algorithm for correlated equilibria described in Hart and Mas-Colell (2000)
# Written by Daniel Filan (2017) based on the algorithm described in Hart and Mas-Colell (2000)
# This code is in the public domain.
# This script implements the Hart-Mas-Colell algorithm for finding correlated
# equilibria in many-player many-action games via the players following a simple
# adaptive procedure.
import numpy as np
import random