Skip to content

Instantly share code, notes, and snippets.

View robot-dreams's full-sized avatar

Elliott Jin robot-dreams

  • San Francisco, CA
View GitHub Profile
@robot-dreams
robot-dreams / keybase.md
Created August 13, 2016 16:34
Keybase proof

Keybase proof

I hereby claim:

  • I am robot-dreams on github.
  • I am eyj (https://keybase.io/eyj) on keybase.
  • I have a public key whose fingerprint is 3D1B 8DAF EC71 A8F6 797A 8206 8C7C 79E9 7D30 1AF0

To claim this, I am signing this object:

@robot-dreams
robot-dreams / Main.elm
Created December 11, 2016 22:52
Starter file for elm projects
import Html exposing (Html, program, text)
main = program
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}
@robot-dreams
robot-dreams / next_permutation.go
Last active January 15, 2017 05:47
Next lexicographic permutation (iterative)
func nextPermutation(a []int) {
// Find the greatest index i such that a[i] < a[i + 1]
i := len(a) - 2
for i >= 0 && a[i] >= a[i+1] {
i--
}
if i >= 0 {
// Find the greatest index j > i such that a[i] < a[j]; such an index
// exists because a[i] < a[i + 1]
j := len(a) - 1
@robot-dreams
robot-dreams / hamming.hs
Last active January 15, 2017 05:46
Hamming's problem (enumerate all positive integers divisible by no primes other than 2, 3, or 5)
hamming :: [Integer]
hamming =
1 : merge3
(map (*2) hamming)
(map (*3) hamming)
(map (*5) hamming)
where
merge (x:xs) (y:ys) =
case compare x y of
LT -> x : merge xs (y:ys)
@robot-dreams
robot-dreams / queens.hs
Last active December 29, 2016 19:49
Generate safe placements of n queens on a chessboard with n rows and n columns
import Control.Monad (foldM)
-- All safe placements of n queens, on a board with n rows and n columns; a
-- placement is safe if no two queens are attacking each other
queens :: Int -> [[(Int, Int)]]
queens n =
foldM (extend n) [] [1..n]
-- All possible ways to extend, on a board with n rows, an existing safe
-- placement of queens by adding an additional queen in column j
@robot-dreams
robot-dreams / fib.hs
Last active January 17, 2017 00:43
Fibonacci in logarithmic time
fib :: Int -> Integer
fib n =
let fibTriplet k
| k <= 0 = (0, 0, 1)
| k == 1 = (0, 1, 1)
| even k = let (a, b, c) = fibTriplet (k `div` 2)
in (a^2 + b^2, b * (a + c), b^2 + c^2)
| odd k = let (a, b, c) = fibTriplet (k - 1)
in (b, c, b + c)
snd3 (_, y, _) = y
@robot-dreams
robot-dreams / wbl_heap.sml
Created January 16, 2017 22:31
Weight-biased leftist heap (exercise 3.3 of Purely Functional Data Structures)
functor WBLHeap(Element : ORDERED) : HEAP =
struct
structure Elem = Element
datatype Heap = E
| T of int * Elem.T * Heap * Heap
val empty = E
fun isEmpty E = true
@robot-dreams
robot-dreams / morse.txt
Last active February 16, 2018 10:34
Morse Code Mnemonics (from Mind Performance Hacks)
unstressed syllables -> dot (short)
stressed syllables -> dash (long)
a-BOUT .-
BOIS-ter-ous-ly -...
CARE-less CHIL-dren -.-.
DAN-ger-ous -..
eh? .
fe-ne-STRA-tion ..-.
GOOD GRA-vy --.
@robot-dreams
robot-dreams / or_chan.go
Last active February 16, 2018 10:31
Useful pattern from "Concurrency in Go" for merging "done" channels
// Returns a channel that will be closed whenever ANY of the input channels are
// closed (or written to).
func or(channels ...<-chan struct{}) <-chan struct{} {
switch len(channels) {
case 0:
return nil
case 1:
return channels[0]
}
orDone := make(chan struct{})
@robot-dreams
robot-dreams / barnes_hut.go
Last active December 6, 2018 19:50
Fast approximation of n-body simulation via Barnes-Hut algorithm, O(n log n)
package main
import (
"math"
)
// We represent a region of space, containing some number of particles, as an
// OctTree. Leaves of the tree contain at most 1 particle (but are also allowed
// to represent empty regions).
//