Skip to content

Instantly share code, notes, and snippets.

View metric-space's full-sized avatar

Luke Meyers metric-space

View GitHub Profile
@metric-space
metric-space / quicksort.clj
Created August 9, 2015 03:51
clojure version of the quicksort in lyah
;; definitely slow
(defn quicksort [unsorted]
(if (empty? unsorted)
unsorted
(let [pivot (first unsorted)
left (filter #(< % pivot) (next unsorted))
right (filter #(> % pivot) (next unsorted))
]
(concat (quicksort left) [pivot] (quicksort right))
@metric-space
metric-space / graphStuff.js
Created September 11, 2015 02:17
BFS and DFS (imperative style) with trees
// the intention was to make a gist, hence everything is put
// into one file.
var expect = require('chai').expect;
function treeNode(key) {
this.key = key;
this.left = null;
this.right = null;
@metric-space
metric-space / filenameGenOfSorts.hs
Created October 11, 2015 17:37
unique file name generator ..
import Data.List hiding (find )
import Data.Char
import System.FilePath.Find -- Sullivan's filemanip library
-- Responsible for splitting each file name into a list of substrings
myFunction :: [Char] -> String -> [String]
myFunction slig [] = []
myFunction splig something@(x:xs) = if (elem x splig ) then ( myFunction splig xs )
@metric-space
metric-space / rbtreeTest.js
Created November 7, 2015 18:07
filling and searching objects, arrays and red-black trees with date strings
var _ = require('lodash');
var moment = require('moment');
var RBTree = require('bintrees').RBTree;
var time = require('performance-now');
var TEST_NUMBER = 100000
function randomRangeGenerator(min,max){
var a = Math.floor(min + Math.random()*(max-min));
@metric-space
metric-space / roundRobinTournament.hs
Last active March 22, 2017 06:31
Round Robin tournament Scheduler
-- round robin based scheduler based on Chris Okasaki's stack Overflow answer
-- the algorithm is as follows assuming you have 6 teams
-- arange them as (clockwise)
--
-- | 0, 1, 2 | now keep 0 fixed and rotate as such | 0, 5, 1 |
-- | 5, 4, 3 | | 4, 3, 2 |
-- leading to games (0,4), (5,3) and (1,2)
-- rotate again with 0 fixed and you get | 0, 4, 5 |
-- | 3, 2, 1 |
@metric-space
metric-space / sha1sum.go
Created April 24, 2016 04:22
go version of sha1sum
package main
import (
"crypto/sha1"
"fmt"
"io/ioutil"
"log"
"os"
)
@metric-space
metric-space / graph->dot.lisp
Created May 16, 2016 00:45
SBCL version of code in "Land of Lisp" (Conrad Barski) page (Graph code, page 120)
(defun dot->png (fname thunk)
(with-open-file (*standard-output*
fname
:direction :output
:if-exists :supersede)
(funcall thunk))
(sb-ext:run-program "/usr/local/bin/dot" (list "-T" "png" "-O " fname) :error t)
)
@metric-space
metric-space / first_example_with_rxjs.html
Created July 28, 2016 21:11
simple counter with reset using rxjs
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.6/rx.all.js"></script>
<title>Rxjs start</title>
</head>
<body>
<button id="egg">Mash</button>
<button id="reset">Reset</button>
<div id="display">0</>
@metric-space
metric-space / random_question.clj
Created August 31, 2016 06:39
generate sum of odd number squares that are less than 10000
;; http://learnyouahaskell.com/higher-order-functions
;; answer -> 1666500
(let [coll (iterate (partial + 2) 1)
sq (fn [x] (* x x))]
(reduce (fn [a,x] (if (> (sq x) 10000) (reduced a) (+ a (sq x))))
coll))
@metric-space
metric-space / sieve_of_eratosthenes.clj
Last active September 1, 2016 03:51
sieve of eratosthenes in clojure
(defn generate-primes-till [x]
;; sieve of eratosthenes
(let [coll (take-while #(< % x) (iterate (partial + 2) 3))
sq (fn [x] (* x x))
not-a-multiple (fn [x a] (or (= a x) (> (mod a x) 0)))
steps (take-while #( < (sq %) x) coll)]
(cons 2 (reduce (fn [a,x] (filter #(not-a-multiple x %) a))
coll steps))))
(println (generate-primes-till 1000 ))