parameterize srange and srange-dynamic, ParamsNeededPaths can now be called as a function to convert to CompiledPath

This commit is contained in:
Nathan Marz 2015-09-10 22:26:32 -04:00
parent ef40adbe3b
commit 983bf84495
2 changed files with 54 additions and 31 deletions

View file

@ -108,7 +108,7 @@
[selector transform-fn structure & {:keys [merge-fn] :or {merge-fn concat}}] [selector transform-fn structure & {:keys [merge-fn] :or {merge-fn concat}}]
(compiled-replace-in (i/comp-paths* selector) transform-fn structure :merge-fn merge-fn)) (compiled-replace-in (i/comp-paths* selector) transform-fn structure :merge-fn merge-fn))
(def bind-params i/bind-params) (def bind-params* i/bind-params*)
;; paramspath* [bindings num-params-sym [impl1 impl2]] ;; paramspath* [bindings num-params-sym [impl1 impl2]]
@ -161,11 +161,19 @@
(def FIRST (i/->PosStructurePath first i/set-first)) (def FIRST (i/->PosStructurePath first i/set-first))
;;TODO: should be parameterized (defparamspath srange-dynamic [start-fn end-fn]
(defn srange-dynamic [start-fn end-fn] (i/->SRangePath start-fn end-fn)) (select* [this structure next-fn]
(i/srange-select structure (start-fn structure) (end-fn structure) next-fn))
(transform* [this structure next-fn]
(i/srange-transform structure (start-fn structure) (end-fn structure) next-fn)
))
;;TODO: should be parameterized (defparamspath srange [start end]
(defn srange [start end] (srange-dynamic (fn [_] start) (fn [_] end))) (select* [this structure next-fn]
(i/srange-select structure start end next-fn))
(transform* [this structure next-fn]
(i/srange-transform structure start end next-fn)
))
(def BEGINNING (srange 0 0)) (def BEGINNING (srange 0 0))

View file

@ -9,6 +9,10 @@
[clojure.string :as s]) [clojure.string :as s])
) )
(defn spy [e]
(println e)
e)
(defn gensyms [amt] (defn gensyms [amt]
(vec (repeatedly amt gensym))) (vec (repeatedly amt gensym)))
@ -69,12 +73,31 @@
(defn no-params-compiled-path [transform-fns] (defn no-params-compiled-path [transform-fns]
(->CompiledPath transform-fns nil 0)) (->CompiledPath transform-fns nil 0))
;;TODO: this must implement IFn so it can be transformed to CompiledPath
;; (just calls bind-params) (declare bind-params*)
(defrecord ParamsNeededPath [transform-fns num-needed-params])
(defmacro define-ParamsNeededPath []
(let [a (with-meta (gensym "array") {:tag 'objects})
impls (for [i (range 21)
:let [args (vec (gensyms i))
setters (for [j (range i)] `(aset ~a ~j ~(get args j)))]]
`(~'invoke [this# ~@args]
(let [~a (object-array ~i)]
~@setters
(bind-params* this# ~a 0)
)))]
`(defrecord ~'ParamsNeededPath [~'transform-fns ~'num-needed-params]
clojure.lang.IFn
~@impls
(~'applyTo [this# args#]
(let [a# (object-array args#)]
(bind-params* this# a# 0))))))
(defn bind-params [^ParamsNeededPath params-needed-path params idx] (define-ParamsNeededPath)
(defn bind-params* [^ParamsNeededPath params-needed-path params idx]
(->CompiledPath (->CompiledPath
(.-transform-fns params-needed-path) (.-transform-fns params-needed-path)
params params
@ -375,7 +398,7 @@
(fn [params# params-idx#] (fn [params# params-idx#]
p# ) p# )
(fn [params# params-idx#] (fn [params# params-idx#]
(bind-params p# params# (+ params-idx# o#)) (bind-params* p# params# (+ params-idx# o#))
))) )))
offsets# offsets#
paths#) paths#)
@ -383,7 +406,7 @@
ret# ~(paramspath* post-bindings num-params-sym impls) ret# ~(paramspath* post-bindings num-params-sym impls)
] ]
(if (= 0 ~num-params-sym) (if (= 0 ~num-params-sym)
(bind-params ret# nil 0) (bind-params* ret# nil 0)
ret# ret#
)))) ))))
@ -576,27 +599,19 @@
(collect-val [^SelectCollector this structure] (collect-val [^SelectCollector this structure]
((.-sel-fn this) (.-selector this) structure))) ((.-sel-fn this) (.-selector this) structure)))
(deftype SRangePath [start-fn end-fn]) (defn srange-select [structure start end next-fn]
(next-fn (-> structure vec (subvec start end))))
(extend-protocol p/StructurePath (defn srange-transform [structure start end next-fn]
SRangePath (let [structurev (vec structure)
(select* [^SRangePath this structure next-fn] newpart (next-fn (-> structurev (subvec start end)))
(let [start ((.-start-fn this) structure) res (concat (subvec structurev 0 start)
end ((.-end-fn this) structure)] newpart
(next-fn (-> structure vec (subvec start end))) (subvec structurev end (count structure)))]
)) (if (vector? structure)
(transform* [^SRangePath this structure next-fn] (vec res)
(let [start ((.-start-fn this) structure) res
end ((.-end-fn this) structure) )))
structurev (vec structure)
newpart (next-fn (-> structurev (subvec start end)))
res (concat (subvec structurev 0 start)
newpart
(subvec structurev end (count structure)))]
(if (vector? structure)
(vec res)
res
))))
(deftype ViewPath [view-fn]) (deftype ViewPath [view-fn])