Skip to content
Permalink
Browse files

Bind over more than just constant expressions.

  • Loading branch information
technomancy committed Sep 12, 2011
1 parent 73b9b97 commit 989521d82c1fd7672490d18d6e0b9dd8d3336d70
Showing with 14 additions and 14 deletions.
  1. +2 −1 .gitignore
  2. +2 −4 project.clj
  3. +5 −5 src/serializable/fn.clj
  4. +5 −4 test/serializable/fn_test.clj
@@ -1,4 +1,5 @@
pom.xml
*jar
lib
classes
classes
/.lein-failures
@@ -1,7 +1,5 @@
(defproject serializable-fn "1.1.0"
(defproject serializable-fn "1.1.1"
:url "http://github.com/technomancy/serializable-fn"
:author "Seajure"
:description "Make functions that have pretty representations."
:dependencies [[org.clojure/clojure "1.2.0-master-SNAPSHOT"]
[org.clojure/clojure-contrib "1.2.0-SNAPSHOT"]]
:dev-dependencies [[leiningen/lein-swank "1.2.0-SNAPSHOT"]])
:dependencies [[org.clojure/clojure "1.2.1"]])
@@ -2,10 +2,10 @@
"Serializable functions! Check it out."
(:refer-clojure :exclude [fn]))

(defn- save-env [env form]
(if env
`(let ~(vec (flatten (for [[name local] env]
[name (.v (.init local))])))
(defn- save-env [bindings form]
(if bindings
`(let ~(vec (apply concat (for [b bindings]
[(.sym b) (.eval (.init b))])))
(~@form))
form))

@@ -14,7 +14,7 @@
fn [& sigs]
`(with-meta (clojure.core/fn ~@sigs)
{:type ::serializable-fn
::source (quote ~(save-env &env &form))}))
::source (quote ~(save-env (vals &env) &form))}))

(defmethod print-method ::serializable-fn [o ^Writer w]
(print-method (::source (meta o)) w))
@@ -1,7 +1,7 @@
(ns serializable.fn-test
(:refer-clojure :exclude [fn])
(:use [serializable.fn] :reload-all)
(:use [clojure.test]))
(:use [serializable.fn]
[clojure.test]))

(def dinc-list '(fn [x] (inc (inc x))))

@@ -29,5 +29,6 @@
(is (= 2 ((eval (read-string (pr-str dinc))) 0))))

(deftest serializable-roundtrip-with-lexical-context
(let [x 0]
(is (= 2 ((eval (read-string (pr-str (fn [] (dinc x))))))))))
(let [x 0, y (+ 95 4)]
(is (= [2 100]
((eval (read-string (pr-str (fn [] [(dinc x) (inc y)])))))))))

0 comments on commit 989521d

Please sign in to comment.
You can’t perform that action at this time.