This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;; 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; using a pattern matching macro | |
(defn test [x] | |
(match x | |
(? (comp not number?)) :not-a-number | |
(? #(< % 42)) (recur (inc x)) | |
42 :booya! | |
_ :too-high!)) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns state.clj) | |
(comment | |
(def f | |
(statefully | |
x <- (*read*) | |
(*write* 42) | |
y <- (*read*) | |
(*update* str " was frobnicated!") |