Skip to content

Instantly share code, notes, and snippets.

Avatar

Timothy Baldridge halgari

View GitHub Profile
View gist:4337160
#include "Python.h"
static PyObject *simple_example(PyObject *self, PyObject *args)
{
PyObject *x;
x = PyTuple_GetItem(args, 0);
PyObject *zero = PyInt_FromLong(0);
if (PyObject_RichCompareBool(x, zero, Py_EQ))
{
Py_DECREF(args);
View gist:4340392
(defn wrap-foo [foo]
(reify
IFn
(invoke [this arg]
(.myMethod foo arg))
IDeref
(deref [this]
foo)))
View gist:5172567
(def adder
(fn [x]
(fn [y]
(+ x y))))
(def add5 (adder 5))
(add5 3)
=> 8
View gist:5970465
(gen-plan
[a (assert-entity {:foo 42})
b (if pred?
(assert-entity {:a a :bar b})
(no-op))]
b)
View gist:5984313
(def philostate
(let [c (chan)]
(go (loop [state {:socrates :thinking
:plato :thinking
:hickey :thinking
:sussman :thinking
:stroustrup :thinking}]
(let [f (<! c)
new-state (f state)]
View gist:5987327
(defn make-read-only [c]
(reify clojure.core.async.impl.protocols/ReadPort
(take! [port handler]
(clojure.core.async.impl.protocols/take! c handler))))
(println (<!! (make-read-only (go 42))))
View gist:6023391
(let [error-chan (chan)]
(go
(try
...
(catch Exception ex (>! error-chan ex)))))
View gist:6073419
(ns hoare.problem
(:require [clojure.core.async :refer :all]))
(defn fan-in
([ins] (fan-in (chan) ins))
([c ins]
(go (while true
(let [[x] (alts! ins)]
(>! c x))))
c))
View gist:6122107
(defn close-at-end [x file]
(if x
(cons (first x) (lazy-seq (close-at-end (next x) file)))
(do (.close file)
nil)))
View Optionally throttled chan
(defn throttle-chan [input]
(let [throttle-chan (chan)
output-chan (chan)]
(go
(loop [throttle-msec nil]
(let [t-chan (when (= :disabled throttle-msec)
(timeout throttle-msec))
[val c] (alt! [input throttle-chan])]
(if (= c throttle-chan)
(recur val)