Skip to content

Instantly share code, notes, and snippets.

View aboekhoff's full-sized avatar

Andrew Boekhoff aboekhoff

  • DemandingBear
  • San Francisco
View GitHub Profile
(defn e-bind [f]
(fn [v]
(let [[e *v] (try [true (f v)] (catch Exception e [false (.toString e)]))]
(if e *v (handler e *v f)))))
;; presumably this would do something constructive, and
;; probably expect more preceding state from preceding computations
;; in a domonad form
(defn handler [s x f]
(ns playground)
(deftype TypedFn
[signature function] :as this
clojure.lang.IFn
(applyTo [args] (apply function args))
(call [] (function))
(invoke [arg] (function arg)))
(defmacro fnt
(ns debuggery
(:use clojure.walk
clojure.stacktrace
clojure.contrib.macro-utils
clojure.contrib.pprint))
(comment
(with-debuggery
(defn bad-plus [n] (+ n nil))
(defn use-bad-plus [] (bad-plus 42)))
(ns state.clj)
(comment
(def f
(statefully
x <- (*read*)
(*write* 42)
y <- (*read*)
(*update* str " was frobnicated!")
module Kumochan where
import Monad
import Control.Monad.State
data Env = Env {nodeId::Int, symId::Int} deriving (Show)
root = Env (-1) (-1)
nextNodeId :: Env -> (Int, Env)
;; using a pattern matching macro
(defn test [x]
(match x
(? (comp not number?)) :not-a-number
(? #(< % 42)) (recur (inc x))
42 :booya!
_ :too-high!))
;; silly example tail-elimination from cps style to javascript
(EV '(let* (f (fn (k x)
(k (op* * x x)))
g (fn (k _f x)
(_f k x))
h (fn (k _f)
(k (fn (_k x)
(_f _k x))))
(Array (h (fn (k*) (k* (fn (x) x) 42)) f))
;; pointless ml-style syntax
;; (now just need a parser combinator to add incanter's infix math and were in business)
(defmacro mlet [& xs]
(let [[[n & args] l2] (split-with #(not= '= %) xs)
[l2 l3] (split-with #(not= 'in %) l2)]
`(let [~n (fn ~n [~@args] ~(rest l2))] ~(rest l3))))
(comment
(mlet f x = * x x in
(ns combinatrix.sequence
(:require [combinatrix.core :as combinatrix]))
(combinatrix/defmonad
:name :sequence
:do in-sequence
:bind (fn [mv f] (mapcat (fn [x] (f x)) mv))
:return list
:zero ()
:plus concat)
(ns combinatrix.sequence
(:use combinatrix.core))
(defmonad
:name :sequence
:do in-sequence
:bind (fn [mv f] (mapcat (fn [x] (f x)) mv))
:return list
:zero ()
:plus concat