Skip to content

Instantly share code, notes, and snippets.

View aboekhoff's full-sized avatar

Andrew Boekhoff aboekhoff

  • DemandingBear
  • San Francisco
View GitHub Profile
(ns parenja.reader
(:refer-clojure :rename {list clj-list
symbol clj-symbol
keyword clj-keyword
vector clj-vector}
:exclude [unquote unquote-splicing read])
(:use somnium.yap
combinatrix.parser
combinatrix.parser.text
[combinatrix.util :only [definitions]]))
;;;; this cps style code
(anf-name x (fn [a]
(anf y (fn [b]
(anf z (fn [c]
(k [:if a b c])))
;;;; becomes
(run-cps
(defmonad
:name :parser
:do parsing
:bind (fn [mv f]
(fn [s]
(when-let [[v s*] (mv s)]
((f v) s*))))
:return (fn [v] (fn [s] [v s]))
:zero (fn [s] nil)
:plus (fn [a b] (fn [s] (or (a s) (b s))))
(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
(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)
;; 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
;; 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))
;; using a pattern matching macro
(defn test [x]
(match x
(? (comp not number?)) :not-a-number
(? #(< % 42)) (recur (inc x))
42 :booya!
_ :too-high!))
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)
(ns state.clj)
(comment
(def f
(statefully
x <- (*read*)
(*write* 42)
y <- (*read*)
(*update* str " was frobnicated!")