Skip to content

Instantly share code, notes, and snippets.

(defn fold [vals, seed, next, return, error]
(if (< 0 (count vals))
#(next
(first vals)
seed
(fn [result]
(fold (subvec vals 1) result next return error))
error)
#(return seed)))
@jido
jido / gist:588764
Created September 20, 2010 22:25
The "feed" macro inserts what follows in the blank. Very handy. Author: chouser
(use 'clojure.walk)
(defmacro _>_ [& exprs]
(reduce
(fn [inner step]
(clojure.walk/postwalk-replace {'___ inner} step))
(reverse exprs)))
(defn cat [lst, val, cont] #(cont (conj lst val)))
;; feed macro
(use 'clojure.walk)
(defmacro _>_ [& exprs]
(reduce
(fn [inner step]
(clojure.walk/postwalk-replace {'___ inner} step))
(reverse exprs)))
;; Protocol for dodo prototypes
(defprotocol DodoProtocol
# Dodo code, imperative style
# ---
String s = ""
loop for (int n = 1; n < 100; ++.n)
{
test
{
n % 4 = 0:
continue.
;; Dispatch message to object
(defmacro call* [obj, getter & args]
`(let [object# ~obj]
(((keyword '~getter) @(type object#)) object# ~@args)))
(defrecord Dodo [instance])
(def DodoClass (new Dodo nil))
# Imported Clojure functions
clojure('=', 2) -> eq
clojure('dec', 1) -> dec
clojure('conj', 2) -> conj
clojure('vector', 0) -> vector
clojure('count', 1) -> count
clojure('merge', 2) -> combine
clojure('first', 1) -> first
clojure('next', 1) -> rest
clojure('concat', 2) -> concat
(defn dodo*fork [coroutine, return]
(let [outcome (promise)]
(println "starting coroutine")
(future
(coroutine ; run in parallel
(fn [& args] (deliver outcome {:return args}))
(fn [e] (deliver outcome {:error e}))))
(println "returning blocking function")
(return
(fn wait [return, error]
; I have this in my code
(defn fun [f, continuation] (-> f continuation)) ; pass f to continuation
; I want to change it to this
(defn fun [argc, f, continuation]
(continuation
(fn [& args]
@jido
jido / gist:1057053
Created June 30, 2011 19:50
Using closure and continuation-passing style in C
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char* description;
} event;
typedef void (*fun)();
@jido
jido / gist:1126681
Created August 5, 2011 00:43
How JS scoping messes with capture
var a = {};
a.foo = function() {
return "foo";
}
a.bar = function() {
return "bar";
}