diff --git a/CHANGES.md b/CHANGES.md index 0f54829..3aafe76 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,7 @@ +## 0.5.6 +* Add multi-path implementation +* change FIRST/LAST to select nothing on an empty sequence + ## 0.5.5 * Change filterer to accept a selector (that acts like selected? to determine whether or not to select value) diff --git a/src/clj/com/rpl/specter.clj b/src/clj/com/rpl/specter.clj index ee7f494..ea1ed40 100644 --- a/src/clj/com/rpl/specter.clj +++ b/src/clj/com/rpl/specter.clj @@ -113,9 +113,9 @@ (def VAL (->ValCollect)) -(def LAST (->LastStructurePath)) +(def LAST (->PosStructurePath last set-last)) -(def FIRST (->FirstStructurePath)) +(def FIRST (->PosStructurePath first set-first)) (defn srange-dynamic [start-fn end-fn] (->SRangePath start-fn end-fn)) diff --git a/src/clj/com/rpl/specter/impl.clj b/src/clj/com/rpl/specter/impl.clj index a171ed1..4ccd7bd 100644 --- a/src/clj/com/rpl/specter/impl.clj +++ b/src/clj/com/rpl/specter/impl.clj @@ -374,23 +374,17 @@ (collect-val [this structure] structure)) -(deftype LastStructurePath []) +(deftype PosStructurePath [getter setter]) (extend-protocol StructurePath - LastStructurePath + PosStructurePath (select* [this structure next-fn] - (next-fn (last structure))) + (if-not (empty? structure) + (next-fn ((.getter this) structure)))) (transform* [this structure next-fn] - (set-last structure (next-fn (last structure))))) - -(deftype FirstStructurePath []) - -(extend-protocol StructurePath - FirstStructurePath - (select* [this structure next-fn] - (next-fn (first structure))) - (transform* [this structure next-fn] - (set-first structure (next-fn (first structure))))) + (if (empty? structure) + structure + ((.setter this) structure (next-fn ((.getter this) structure)))))) (deftype WalkerStructurePath [afn])