├── .gitignore ├── test └── contextual │ ├── test_runner.cljs │ └── core_test.cljc ├── project.clj ├── README.md ├── LICENSE └── src └── contextual └── core.cljc /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | -------------------------------------------------------------------------------- /test/contextual/test_runner.cljs: -------------------------------------------------------------------------------- 1 | (ns contextual.test-runner 2 | (:require [cljs.test :refer-macros [run-tests]] 3 | [contextual.core-test])) 4 | 5 | (enable-console-print!) 6 | 7 | (try 8 | (run-tests 9 | 'contextual.core-test) 10 | (finally 11 | (.exit js/phantom))) 12 | 13 | 14 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject com.cognitect/contextual "0.1.0" 2 | :description "Associative values in Clojure and ClojureScript which 3 | know their context" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :dependencies [[org.clojure/clojure "1.7.0-beta1"] 7 | [org.clojure/clojurescript "0.0-3196"]] 8 | :profiles {:dev {:dependencies [[org.clojure/test.check "0.6.3-SNAPSHOT"]] 9 | :plugins [[lein-cljsbuild "1.0.5"] 10 | [com.cemerick/clojurescript.test "0.3.3"]] 11 | :aliases {"test-all" ["do" "test," "cljsbuild" "test"]} 12 | :cljsbuild {:test-commands {"all" ["phantomjs" "target/test-runner.js"]} 13 | :builds [{:source-paths ["src" "test"] 14 | :compiler {:output-to "target/test-runner.js" 15 | :optimizations :whitespace 16 | :pretty-print true}}]}}} 17 | ) 18 | 19 | -------------------------------------------------------------------------------- /test/contextual/core_test.cljc: -------------------------------------------------------------------------------- 1 | (ns contextual.core-test 2 | (:require #?@(:cljs [[cljs.test :as t :refer-macros [deftest is]] 3 | [cljs.test.check :as tc] 4 | [cljs.test.check.generators :as gen] 5 | [cljs.test.check.properties :as prop :include-macros true] 6 | [cljs.test.check.cljs-test :refer-macros [defspec]]] 7 | :clj [[clojure.test :as t :refer [deftest is]] 8 | [clojure.test.check :as tc] 9 | [clojure.test.check.generators :as gen] 10 | [clojure.test.check.properties :as prop] 11 | [clojure.test.check.clojure-test :refer [defspec]]]) 12 | [contextual.core :as c])) 13 | 14 | (deftest contextual-map-basics 15 | (let [c (c/contextualize {:foo :bar} [:a :b])] 16 | (is (= [:a :b] (c/context c))) 17 | (is (= {:foo :bar} (c/decontextualize c))) 18 | (is (= c {:foo :bar})) 19 | (is (= {:foo :bar} c)) 20 | (is (= (hash {:foo :bar}) (hash c))))) 21 | 22 | (deftest contextual-map-traversal 23 | (let [c (c/contextualize {:a {:b {:c 0}}})] 24 | (is (= [] (c/context c))) 25 | (is (= [:a] (c/context (:a c)))) 26 | (is (= [:a :b] (c/context (-> c :a :b)))))) 27 | 28 | (deftest contextual-map-lookup 29 | (let [c (c/contextualize {:a {:b {:c 0}}}) 30 | check (fn [v] 31 | (is (= [:a] (c/context v))) 32 | (is (= {:b {:c 0}} (c/decontextualize v))))] 33 | (check (:a c)) 34 | (check (get c :a)) 35 | (check (c :a)) 36 | (check (apply c [:a])) 37 | (check (get-in c [:a])) 38 | (check (second (first (seq c)))))) 39 | 40 | (deftest emptiness-is-filling-me 41 | (is (empty? (c/contextualize {}))) 42 | (is (empty? (c/contextualize [])))) 43 | 44 | (deftest contextual-vec-basics 45 | (let [c (c/contextualize [:foo :bar] [:a :b])] 46 | (is (= [:a :b] (c/context c))) 47 | (is (= [:foo :bar] (c/decontextualize c))) 48 | (is (= c [:foo :bar])) 49 | (is (= [:foo :bar] c)) 50 | (is (= (hash [:foo :bar]) (hash c))))) 51 | 52 | (deftest contextual-vec-traversal 53 | (let [c (c/contextualize [[:a :b] [:c :d] [:e :f]])] 54 | (is (= [] (c/context c))) 55 | (is (= [:a :b] (first c))) 56 | (is (= [0] (c/context (first c)))))) 57 | 58 | (deftest contextual-vec-lookup 59 | (let [c (c/contextualize [[:a] [:b] [:c] [:d]])] 60 | (is (= [[0] [1] [2] [3]] (map c/context c))) 61 | (is (= [2] (c/context (c 2)))) 62 | (is (= [2] (c/context (nth c 2)))) 63 | (is (= [2] (c/context (get c 2)))) 64 | (is (= [2] (c/context (first (filter #(= [:c] %) c))))))) 65 | 66 | (deftest mixed-traversal 67 | (let [c (c/contextualize {:a [nil {:b [nil nil {:c [4]}]}]})] 68 | (is (= [:a 1 :b 2 :c] (c/context (get-in c [:a 1 :b 2 :c])))))) 69 | 70 | (defn check-children 71 | [parent path k v] 72 | (if (or (map? v) (vector? v)) 73 | (and (satisfies? c/Contextual v) 74 | (satisfies? c/Contextual (get parent k)) 75 | (satisfies? c/Contextual (parent k)) 76 | (= path (c/context v)) 77 | (cond 78 | (map? v) (every? (fn [[child-k child-v]] 79 | (check-children v (conj path child-k) child-k child-v)) v) 80 | (vector? v) (every? identity 81 | (map-indexed (fn [idx child] 82 | (check-children v (conj path idx) idx child)) 83 | v)))) 84 | true)) 85 | 86 | (defspec context-is-passed-correctly 30 87 | (prop/for-all [m (gen/map gen/any gen/any)] 88 | (let [c (c/contextualize m)] 89 | (every? (fn [[k v]] 90 | (check-children c [k] k v)) 91 | c)))) 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # contextual 2 | 3 | A small library for Clojure and ClojureScript providing associative 4 | data structures that store metadata about their context in some larger 5 | data structure. 6 | 7 | ## Rationale 8 | 9 | A common idiom in functional programming is to walk a tree-like data 10 | structure of nested maps or vectors, processing each "node" of the 11 | tree with a function.. 12 | 13 | Sometimes, it is necessary to know "where" a given node is located 14 | within the data structure as a whole. For example, this is often the 15 | case when building a user interface, so user interaction events can be 16 | applied back to the same logical location in the tree. 17 | 18 | Normally, this requires passing around additional book-keeping 19 | arguments to each function. Each function must be passed not just the 20 | node itself, but its "path" or context within the data structure as a 21 | whole. 22 | 23 | This library provides contextual data structures for maps and vectors 24 | that track their *own* context, from some arbitrary root. They implement 25 | the full set of persistent map and vector interfaces and are full 26 | substitutes for Clojure(Script)'s map and vector datatypes. 27 | 28 | At any point, an application may query a contextual object for its 29 | path relative to the root. Retrieving a "child" map or vector from a 30 | contextual data structure will yield another contextual object, with a 31 | path reflecting its key or index within its "parent". This is entirely 32 | seamless and requires no special functions or arguments. Intermediate 33 | functions can be agnostic to the fact that they are operating on 34 | contextual objects vs normal data structures. 35 | 36 | Creating a contextual object or retrieving its path are constant-time 37 | operations. 38 | 39 | For the purposes of equality semantics, contextual objects are always 40 | equivalent to their non-contextual counterparts. The path of a 41 | contextual object is not included in equality comparisons - it is 42 | effectively metadata (although it is not implemented using Clojure's 43 | metadata mechanism). 44 | 45 | ## Usage 46 | 47 | To create a contextual object, use `contextual.core/contextualize`, 48 | passing an associative data structure to wrap and (optionally) a 49 | path. If no path is given, an empty path with be used, implying that 50 | the given object is the root. 51 | 52 | ```clojure 53 | (def root (c/contextualize {:a [{:b {:c 1}} {:x 3}]})) 54 | ``` 55 | 56 | To retrieve the path of a contextual object, use 57 | `contextual.core/context`, which returns the path relative to the 58 | root. 59 | 60 | ```clojure 61 | (c/context root) ;; => [] 62 | ``` 63 | 64 | Use any of Clojure's built in methods for retrieving nested values: 65 | 66 | ```clojure 67 | (def node (:b (first (root :a)))) 68 | 69 | (= node {:c 1}) ;; => true 70 | 71 | (c/context node) ;; => [:a 0 :b] 72 | ``` 73 | 74 | Getting items via the sequence APIs returns contextual objects, too. 75 | 76 | ```clojure 77 | (map c/context (:a root)) ;;=> ([:a 0] [:a 1]) 78 | (map c/context (vals root)) ;;=> ([:a]) 79 | ``` 80 | 81 | To unwrap the contextual object and get a normal data structure back, 82 | use `contextual.core/decontextualize`. 83 | 84 | ```clojure 85 | (c/decontextualize node) ;; => {:c 1} 86 | ``` 87 | 88 | ## Implementation 89 | 90 | The library is implemented in terms of two protocols: 91 | 92 | `Contextual` indicates a contextual object and supports the `context` 93 | and `decontextualize` protocol methods. 94 | 95 | Two implementations are provided, `DelegateMap` and 96 | `DelegateVec`. These wrap maps and vectors (respectively), maintaining 97 | the object's path and delegating operations to the wrapped map or 98 | vector implementation. 99 | 100 | The `Contextualizable` protocol provides the `contextualize` method, 101 | which converts the implemented type into an instance of 102 | `Contextual`. By default it is extended to all of Clojure(Script)'s 103 | default persistent map and vector types, clients may extend it to 104 | additional types if necessary. 105 | 106 | ## License 107 | 108 | Copyright © 2015 Cognitect, Inc 109 | 110 | Distributed under the Eclipse Public License either version 1.0 or (at 111 | your option) any later version. 112 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor tocontrol, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /src/contextual/core.cljc: -------------------------------------------------------------------------------- 1 | (ns contextual.core) 2 | 3 | (defprotocol Contextual 4 | "An associative data structure which tracks a path of keys from some 5 | root that was used to obtain this value. Any 'child' values 6 | retrieved from a Contextual object, via any of its APIs, will also 7 | be Contextual (provided they also are associative), with the same 8 | root and the child's key added as a path element." 9 | (context [_] "Return the full path from the root") 10 | (decontextualize [_] "Return the underlying, non-contextual 11 | version of the object")) 12 | 13 | (defprotocol Contextualizable 14 | "A value that can be contextualized." 15 | (contextualize [_] [_ path] "Return a contextualized version 16 | of the value. Uses the given path is provided, otherwise uses an 17 | empty path (i.e, the given object is the root).")) 18 | 19 | (declare ->DelegateMap ->DelegateVec) 20 | 21 | (extend-protocol Contextualizable 22 | #?@(:clj [clojure.lang.IPersistentVector 23 | (contextualize ([v] (->DelegateVec v [])) 24 | ([v path] (->DelegateVec v path))) 25 | clojure.lang.IPersistentMap 26 | (contextualize ([m] (->DelegateMap m [])) 27 | ([m path] (->DelegateMap m path)))] 28 | :cljs [cljs.core.PersistentVector 29 | (contextualize ([v] (->DelegateVec v [])) 30 | ([v path] (->DelegateVec v path))) 31 | cljs.core.Subvec 32 | (contextualize ([v] (->DelegateVec v [])) 33 | ([v path] (->DelegateVec v path))) 34 | cljs.core.PersistentArrayMap 35 | (contextualize ([m] (->DelegateMap m [])) 36 | ([m path] (->DelegateMap m path))) 37 | cljs.core.PersistentHashMap 38 | (contextualize ([m] (->DelegateMap m [])) 39 | ([m path] (->DelegateMap m path))) 40 | cljs.core.PersistentTreeMap 41 | (contextualize ([m] (->DelegateMap m [])) 42 | ([m path] (->DelegateMap m path)))])) 43 | 44 | (defn contextualizable? 45 | "Return true if the value can be converted to a contextual one. 46 | 47 | We would prefer to use (satisfies? Contextualizable), however, the 48 | performance of satisfies? is not satisfactory." 49 | [value] 50 | (or (map? value) (vector? value))) 51 | 52 | (defn- contextual-value 53 | [path key value] 54 | (if (contextualizable? value) 55 | (contextualize value (conj path key)) 56 | value)) 57 | 58 | (defn- contextual-entry 59 | [path [key value :as entry]] 60 | (if (contextualizable? value) 61 | #?(:clj (clojure.lang.MapEntry. key (contextualize value (conj path key))) 62 | :cljs [key (contextualize value (conj path key))]) 63 | entry)) 64 | 65 | #?(:clj 66 | (deftype DelegateMap [delegate path] 67 | Contextual 68 | (context [_] path) 69 | (decontextualize [_] delegate) 70 | 71 | clojure.lang.Associative 72 | (containsKey [_ key] (.containsKey delegate key)) 73 | (assoc [_ key val] (->DelegateMap (.assoc delegate key val) path)) 74 | (entryAt [_ key] (contextual-entry path (.entryAt delegate key))) 75 | 76 | clojure.lang.Counted 77 | (count [_] (.count delegate)) 78 | 79 | clojure.lang.IFn 80 | (applyTo [_ [key & _ :as arglist]] 81 | (contextual-value path key (.applyTo delegate arglist))) 82 | (invoke [_ key] 83 | (contextual-value path key (.invoke delegate key))) 84 | (invoke [_ key not-found] 85 | (contextual-value path key (.invoke delegate key not-found))) 86 | 87 | clojure.lang.IHashEq 88 | (hasheq [_] (.hasheq delegate)) 89 | 90 | clojure.lang.ILookup 91 | (valAt [_ key] 92 | (contextual-value path key (.valAt delegate key))) 93 | (valAt [_ key notfound] 94 | (contextual-value path key (.valAt delegate key notfound))) 95 | 96 | clojure.lang.IMeta 97 | (meta [_] (.meta delegate)) 98 | 99 | clojure.lang.IObj 100 | (withMeta [_ meta] (->DelegateMap (.withMeta delegate meta) path)) 101 | 102 | clojure.lang.MapEquivalence 103 | 104 | clojure.lang.IPersistentCollection 105 | (cons [_ o] (->DelegateMap (.cons delegate o) path)) 106 | (empty [_] (->DelegateMap (.empty delegate) path)) 107 | (equiv [_ o] (.equiv delegate o)) 108 | 109 | clojure.lang.IPersistentMap 110 | (assocEx [_ k v] (->DelegateMap (.assocEx delegate k v) path)) 111 | 112 | clojure.lang.Seqable 113 | (seq [_] 114 | (when-let [s (.seq delegate)] 115 | (map (partial contextual-entry path) s))) 116 | 117 | java.io.Serializable 118 | 119 | java.lang.Iterable 120 | (iterator [this] (.iterator (seq this))) 121 | 122 | java.lang.Runnable 123 | (run [this] (.invoke this)) 124 | 125 | java.util.Map 126 | (containsValue [_ value] (.containsValue delegate value)) 127 | (entrySet [this] (set (seq this))) 128 | (equals [_ o] (.equals delegate o)) 129 | (get [this k] (get this k)) 130 | (hashCode [_] (.hashCode delegate)) 131 | (isEmpty [_] (.isEmpty delegate)) 132 | (keySet [_] (.keySet delegate)) 133 | (size [_] (.size delegate)) 134 | (values [_] (map val (seq (.values delegate)))) 135 | 136 | java.util.concurrent.Callable 137 | (call [this] (.invoke this)) 138 | 139 | clojure.core.protocols/IKVReduce 140 | (kv-reduce [_ f init] (clojure.core.protocols/kv-reduce delegate f init)))) 141 | 142 | #?(:cljs 143 | (deftype DelegateMap [delegate path] 144 | Contextual 145 | (context [_] path) 146 | (decontextualize [_] delegate) 147 | 148 | Object 149 | (toString [_] (.toString delegate)) 150 | (equiv [this other] (-equiv this other)) 151 | 152 | ICloneable 153 | (-clone [_] (->DelegateMap delegate path)) 154 | 155 | IWithMeta 156 | (-with-meta [_ meta] (->DelegateMap (-with-meta delegate meta) path)) 157 | 158 | IMeta 159 | (-meta [_] (-meta delegate)) 160 | 161 | ICollection 162 | (-conj [_ entry] (->DelegateMap (-conj delegate entry) path)) 163 | 164 | ICounted 165 | (-count [_] (-count delegate)) 166 | 167 | IEmptyableCollection 168 | (-empty [_] (->DelegateMap (-empty delegate) path)) 169 | 170 | IEquiv 171 | (-equiv [_ other] (-equiv delegate other)) 172 | 173 | IHash 174 | (-hash [_] (-hash delegate)) 175 | 176 | ISeqable 177 | (-seq [_] 178 | (when-let [s (-seq delegate)] 179 | (map (partial contextual-entry path) s))) 180 | 181 | ILookup 182 | (-lookup [_ k] (contextual-value path k (-lookup delegate k))) 183 | (-lookup [_ k not-found] (contextual-value path k (-lookup delegate k not-found))) 184 | 185 | IAssociative 186 | (-assoc [_ k v] (->DelegateMap (-assoc delegate k v) path)) 187 | (-contains-key? [_ k] (-contains-key? delegate k)) 188 | 189 | IMap 190 | (-dissoc [_ k] (->DelegateMap (-dissoc delegate k) path)) 191 | 192 | IKVReduce 193 | (-kv-reduce [_ f init] (-kv-reduce delegate f init)) 194 | 195 | IFn 196 | (-invoke [_ k] (contextual-value path k (delegate k))) 197 | (-invoke [_ k not-found] (contextual-value path k (delegate k not-found))))) 198 | 199 | #?(:clj 200 | 201 | (deftype DelegateVec [delegate path] 202 | Contextual 203 | (context [_] path) 204 | (decontextualize [_] delegate) 205 | 206 | clojure.lang.Associative 207 | (containsKey [_ key] (.containsKey delegate key)) 208 | (assoc [_ key val] (->DelegateVec (.assoc delegate key val) path)) 209 | (entryAt [_ key] (contextual-entry path (.entryAt delegate key))) 210 | 211 | clojure.lang.Counted 212 | (count [_] (.count delegate)) 213 | 214 | clojure.lang.IFn 215 | (applyTo [_ [key & _ :as arglist]] 216 | (contextual-value path key (.applyTo delegate arglist))) 217 | (invoke [_ key] 218 | (contextual-value path key (.invoke delegate key))) 219 | (invoke [_ key not-found] 220 | (contextual-value path key (.invoke delegate key not-found))) 221 | 222 | clojure.lang.IHashEq 223 | (hasheq [_] (.hasheq delegate)) 224 | 225 | clojure.lang.ILookup 226 | (valAt [_ key] 227 | (contextual-value path key (.valAt delegate key))) 228 | (valAt [_ key notfound] 229 | (contextual-value path key (.valAt delegate key notfound))) 230 | 231 | clojure.lang.IMeta 232 | (meta [_] (.meta delegate)) 233 | 234 | clojure.lang.IObj 235 | (withMeta [_ meta] (->DelegateVec (.withMeta delegate meta) path)) 236 | 237 | clojure.lang.IPersistentCollection 238 | (cons [_ o] (->DelegateVec (.cons delegate o) path)) 239 | (empty [_] (->DelegateVec (.empty delegate) path)) 240 | (equiv [_ o] (.equiv delegate o)) 241 | 242 | clojure.lang.Seqable 243 | (seq [_] 244 | (when-let [s (.seq delegate)] 245 | (map-indexed (fn [i v] 246 | (contextual-value path i v)) s))) 247 | 248 | java.io.Serializable 249 | 250 | java.lang.Iterable 251 | (iterator [this] (.iterator (seq this))) 252 | 253 | java.lang.Runnable 254 | (run [this] (.invoke this)) 255 | 256 | java.util.concurrent.Callable 257 | (call [this] (.invoke this)) 258 | 259 | clojure.lang.IReduceInit 260 | (reduce [_ f start] (.reduce delegate f start)) 261 | 262 | clojure.lang.IReduce 263 | (reduce [_ f] (.reduce delegate f)) 264 | 265 | clojure.lang.IPersistentStack 266 | (peek [_] (let [idx (dec (count delegate))] 267 | (contextual-value path idx (.peek delegate)))) 268 | (pop [_] (->DelegateVec (.pop delegate) path)) 269 | 270 | clojure.lang.IPersistentVector 271 | (length [_] (.length delegate)) 272 | (assocN [_ idx val] (->DelegateVec (.assocN delegate idx val) path)) 273 | 274 | clojure.lang.Indexed 275 | (nth [_ i] (contextual-value path i (.nth delegate i))) 276 | (nth [_ i not-found] (contextual-value path i 277 | (.nth delegate i not-found))) 278 | 279 | clojure.lang.Sequential 280 | 281 | java.lang.Comparable 282 | (compareTo [_ other] (.compareTo delegate other)) 283 | 284 | java.util.Collection 285 | (contains [_ obj] (.contains delegate obj)) 286 | (containsAll [_ c] (.containsAll delegate c)) 287 | (equals [_ o] (.equals delegate o)) 288 | (hashCode [_] (.hashCode delegate)) 289 | (isEmpty [_] (.isEmpty delegate)) 290 | (size [_] (.size delegate)) 291 | (toArray [this] (clojure.lang.RT/seqToArray (seq this))) 292 | (toArray [this a] (clojure.lang.RT/seqToPassedArray (seq this) a)) 293 | 294 | java.util.List 295 | (get [_ idx] (contextual-value path idx (.get delegate idx))) 296 | (indexOf [_ o] (.indexOf delegate o)) 297 | (lastIndexOf [_ o] (.lastIndexOf delegate o)) 298 | (listIterator [this] 299 | ;; This is quite inefficent, but much easier to 300 | ;; implement. Should be enough of an edge case that it doesn't 301 | ;; matter. 302 | (.listIterator (vec (seq this)))) 303 | (listIterator [this i] 304 | (.listIterator (vec (seq this)) i)) 305 | (subList [_ from to] 306 | (->DelegateVec (.subList delegate from to) path)) 307 | 308 | java.util.RandomAccess 309 | )) 310 | 311 | #? (:cljs 312 | (deftype DelegateVec [delegate path] 313 | Contextual 314 | (context [_] path) 315 | (decontextualize [_] delegate) 316 | 317 | Object 318 | (toString [_] (.toString delegate)) 319 | (equiv [this other] (-equiv this other)) 320 | 321 | ICloneable 322 | (-clone [_] (->DelegateVec delegate path)) 323 | 324 | IWithMeta 325 | (-with-meta [_ meta] (DelegateVec. (-with-meta delegate meta) path)) 326 | 327 | IMeta 328 | (-meta [_] (-meta delegate)) 329 | 330 | IStack 331 | (-peek [_] (let [idx (dec (count delegate))] 332 | (contextual-value path idx (-peek delegate)))) 333 | (-pop [_] (->DelegateVec (-pop delegate) path)) 334 | 335 | ICollection 336 | (-conj [_ val] (->DelegateMap (-conj delegate val) path)) 337 | 338 | IEmptyableCollection 339 | (-empty [coll] (->DelegateMap (-empty delegate) path)) 340 | 341 | ISequential 342 | IEquiv 343 | (-equiv [_ other] (-equiv delegate other)) 344 | 345 | IHash 346 | (-hash [_] (-hash delegate)) 347 | 348 | ISeqable 349 | (-seq [_] 350 | (when-let [s (-seq delegate)] 351 | (map-indexed (fn [i val] 352 | (contextual-value path i val)) s))) 353 | 354 | ICounted 355 | (-count [_] (-count delegate)) 356 | 357 | IIndexed 358 | (-nth [_ n] (contextual-value path n (-nth delegate n))) 359 | (-nth [_ n not-found] 360 | (contextual-value path n (-nth delegate n not-found))) 361 | 362 | ILookup 363 | (-lookup [_ k] 364 | (contextual-value path k (-lookup delegate k))) 365 | (-lookup [_ k not-found] 366 | (contextual-value path k (-lookup delegate k not-found))) 367 | 368 | IMapEntry 369 | (-key [_] (-key delegate)) 370 | (-val [_] (contextual-value path 1 (-val delegate))) 371 | 372 | IAssociative 373 | (-assoc [_ k v] (->DelegateVec (-assoc delegate k v) path)) 374 | (-contains-key? [_ k] (-contains-key? delegate k)) 375 | 376 | IVector 377 | (-assoc-n [_ n val] 378 | (->DelegateVec (-assoc-n delegate n val) path)) 379 | 380 | IReduce 381 | (-reduce [_ f] 382 | (-reduce delegate f)) 383 | (-reduce [_ f init] 384 | (-reduce delegate f init)) 385 | 386 | IKVReduce 387 | (-kv-reduce [_ f init] 388 | (-kv-reduce delegate f init)) 389 | 390 | IFn 391 | (-invoke [_ k] (contextual-value path k (delegate k))) 392 | (-invoke [_ k not-found] (contextual-value path k (delegate k not-found))) 393 | 394 | IIterable 395 | (-iterator [this] 396 | (seq-iter (seq this))))) 397 | 398 | --------------------------------------------------------------------------------