Skip to content

Instantly share code, notes, and snippets.

View timsgardner's full-sized avatar

Tims Gardner timsgardner

  • Arcadia Technologies
  • Brooklyn, NY
View GitHub Profile
@timsgardner
timsgardner / day04_week_01_lab.js
Created April 28, 2016 18:36
lab for functions
// CLASS 4 WEEK 1 LAB
// ============================================================
// Define a function maxOfTwoNumbers that takes two numbers as
// arguments and returns the largest of them. starter code:
var maxOfTwoNumbers = function(a, b){
// return the greater number, a or b
};
@timsgardner
timsgardner / prototypeTable.js
Created April 20, 2016 15:01
prototype table
var m = require('mori');
function uniquify(coll){
var seen = m.set();
var retval = m.reduce(
function(bldg, x){
if(m.hasKey(seen, x)){
return bldg;
}else{
seen = m.conj(seen, x);
@timsgardner
timsgardner / vector_insert.clj
Created April 12, 2016 17:29
vector insert!
(defn vector-insert [v1 v2 i]
(let [c1 (count v1)
c2 (count v2)
c3 (+ c1 c2)]
(persistent!
(loop [j i, v3 (transient v1)]
(cond
(< j (+ i c2))
(recur
(inc j)
(defrecord ObliqueSnoozler [])
(ObliqueSnoozler.)
;; System.TypeInitializationException: An exception was thrown by the type initializer for user.ObliqueSnoozler ---> System.NullReferenceException: Object reference not set to an instance of an object
;; at clojure.lang.Compiler.TryLoadInitType (System.String relativePath) [0x00000] in <filename unknown>:0
;; at clojure.lang.RT.load (System.String relativePath, Boolean failIfNotFound) [0x00000] in <filename unknown>:0
;; at clojure.lang.RT.load (System.String relativePath) [0x00000] in <filename unknown>:0
;; at clojure/core$load$fn__5867__5871.invoke () [0x00000] in <filename unknown>:0
;; at clojure/core$load__5874.doInvoke (System.Object ) [0x00000] in <filename unknown>:0
(def v (volatile! nil))
;; a function that I don't think will get disappeared by the compiler:
(defn bla [x]
(vreset! v x))
(def stuff (vec (range 5e5)))
(defmacro do-reduce [[x coll] & body]
`(do
@timsgardner
timsgardner / minimal-file-watch.clj
Created March 9, 2016 06:28
minimal file watch
(require '[clojure.core.async :as a])
(import
'[java.nio.file FileSystems WatchService Files Paths Path LinkOption NotDirectoryException WatchKey
StandardWatchEventKinds])
(defmacro async-pause [time]
`(a/<! (a/timeout ~time)))
;; wow java
(defn watch-directory [dir f opts] ;; only works for directories, because of fascism
@timsgardner
timsgardner / flumbum.clj
Last active March 6, 2016 22:48
agent problem
(ns flumbum
(:use clojure.pprint clojure.repl))
;; ==================================================
;; logging erotica
(def logs (atom {} :validator #(every? var? (keys %))))
(defmacro deflog [name & body]
`(let [f# (fn [] ~@body)
@timsgardner
timsgardner / irobot.clj
Last active March 5, 2016 21:39
robot thing
(defprotocol IRobot ; ha
(live? [_])
(kill [_])
(state [_]))
(defn robot
([state f]
(robot state (constantly true) f))
([state continue? f]
(let [kill (a/chan (a/sliding-buffer 1))
@timsgardner
timsgardner / traversal.clj
Last active February 26, 2016 20:31
traversal with transducers
;; feed in a traverse function; here's where you define the meaning of
;; the tree and so on, similar to the three zipper functions (that is, branch?,
;; children, make-node)
(defn trav-1 [x xfrm]
(if (coll? x)
(into (empty x) xfrm x)
x))
;; terse:
(defn prewalk [x f traverse]
@timsgardner
timsgardner / exemplary.html
Created February 26, 2016 02:01
superprofessional
<!doctype html>
<html><body><div><style></style></div><div style="font-family: sans-serif;
width: 100%;
margin-top: 1em;
margin-bottom: 1em;
"><div style="text-align: center;
max-width: 1000px;
width: 100%;
margin-right: auto;