-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#78] Implement with-monkeypatches, use to "fix" core.logic
- Loading branch information
Showing
4 changed files
with
77 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
(ns kibit.monkeypatch | ||
"Various helpers providing a with-monkeypatches form which wraps" | ||
(:require [clojure.core.logic :as c.c.l]) | ||
(:import [clojure.lang | ||
Var | ||
IPersistentSet] | ||
[clojure.core.logic.protocols | ||
ITreeTerm])) | ||
|
||
(defn ^:pivate tree-term? [x] | ||
(and (or (coll? x) | ||
(instance? ITreeTerm x)) | ||
(not (instance? IPersistentSet x)))) | ||
|
||
(def kibit-redefs | ||
{#'c.c.l/tree-term? tree-term?}) | ||
|
||
(defmacro with-monkeypatches | ||
"Builds a try/finally which captures Var bindings (and ^:macro tags) comming in, creates new Var | ||
bindings within the try and in the finally restores the original bindings. This allows users to | ||
establish stack-local patched contexts." | ||
{:style/indent [1]} | ||
[redefs & forms] | ||
(let [redefs (eval redefs) | ||
original-bindings (into {} | ||
(for [k (keys redefs)] | ||
[k (gensym)]))] | ||
`(let [~@(for [[k v] original-bindings | ||
f [v `(deref ~k)]] | ||
f)] | ||
(try ~@(for [[k v] redefs] | ||
`(do (.bindRoot ~k ~v) | ||
~(if (.isMacro ^Var k) | ||
`(.setMacro ~k)))) | ||
~@forms | ||
(finally | ||
~@(for [[k v] redefs] | ||
`(do (.bindRoot ~k ~(get original-bindings k)) | ||
~(if (.isMacro ^Var k) | ||
`(.setMacro ~k))))))))) |
This file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(ns resources.sets) | ||
|
||
(defn killit [coll] | ||
(not-any? #{"string1" "string2"} (map ffirst coll))) |