summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2016-10-26 06:17:14 -0700
committerKaz Kylheku <kaz@kylheku.com>2016-10-26 06:17:14 -0700
commitbc0d27c80e2f7b534ba1efd3f60210b5855f65c1 (patch)
treea41320843ddd9827c7d6657898305488edd4f18e
parentdcd3ef3f78ee3f17d7c706ccaa1ff74c5dc7f104 (diff)
downloadtxr-bc0d27c80e2f7b534ba1efd3f60210b5855f65c1.tar.gz
txr-bc0d27c80e2f7b534ba1efd3f60210b5855f65c1.tar.bz2
txr-bc0d27c80e2f7b534ba1efd3f60210b5855f65c1.zip
sub function becomes accessor.
* share/txr/stdlib/place.tl (defplace sub): New place. * txr.1: Document sub as accessor.
-rw-r--r--share/txr/stdlib/place.tl39
-rw-r--r--txr.133
2 files changed, 69 insertions, 3 deletions
diff --git a/share/txr/stdlib/place.tl b/share/txr/stdlib/place.tl
index 320cf90e..9bad42fa 100644
--- a/share/txr/stdlib/place.tl
+++ b/share/txr/stdlib/place.tl
@@ -580,6 +580,45 @@
,',ind-sym (succ ,',ind-sym)))))
,body)))))
+(defplace (sub seq :whole args : (from 0) (to t)) body
+ (getter setter
+ (with-gensyms (seq-sym from-sym to-sym v-sym)
+ (with-update-expander (seq-getter seq-setter) seq nil
+ ^(alet ((,seq-sym (,seq-getter))
+ (,from-sym ,from)
+ (,to-sym ,to))
+ (macrolet ((,getter () ^(sub ,',seq-sym ,',from-sym ,',to-sym))
+ (,setter (val)
+ ^(alet ((,',v-sym ,val))
+ (,seq-setter (replace ,',seq-sym ,',v-sym
+ ,',from-sym ,',to-sym))
+ ,',v-sym)))
+ ,body)))))
+ (ssetter
+ (with-gensyms (seq-sym from-sym to-sym v-sym)
+ (with-update-expander (seq-getter seq-setter) seq nil
+ ^(macrolet ((,ssetter (val)
+ ^(alet ((,',seq-sym (,',seq-getter))
+ (,',from-sym ,',from)
+ (,',to-sym ,',to)
+ (,',v-sym ,val))
+ (,',seq-setter (replace ,',seq-sym ,',v-sym
+ ,',from-sym ,',to-sym))
+ ,',v-sym)))
+ ,body))))
+ (deleter
+ (with-gensyms (seq-sym from-sym to-sym)
+ (with-update-expander (seq-getter seq-setter) seq nil
+ ^(alet ((,seq-sym (,seq-getter))
+ (,from-sym ,from)
+ (,to-sym ,to))
+ (macrolet ((,deleter ()
+ ^(prog1
+ (sub ,',seq-sym ,',from-sym ,',to-sym)
+ (,',seq-setter (replace ,',seq-sym nil
+ ,',from-sym ,',to-sym)))))
+ ,body))))))
+
(defplace (gethash hash key : (default nil have-default-p)) body
(getter setter
(with-gensyms (entry-sym)
diff --git a/txr.1 b/txr.1
index bb739b47..a32453d9 100644
--- a/txr.1
+++ b/txr.1
@@ -22694,9 +22694,10 @@ a deeper copy than just duplicating the
value itself,
but it is not a deep copy.
-.coNP Function @ sub
+.coNP Accessor @ sub
.synb
.mets (sub < sequence >> [ from <> [ to ]])
+.mets (set (sub < sequence >> [ from <> [ to ]]) << new-val )
.syne
.desc
The
@@ -22721,10 +22722,13 @@ Thus
means
.codn "(sub a 0 t)" .
-The following equivalence holds between the
+The following semantic equivalence exists between a call to the
.code sub
function and
-the DWIM-bracket syntax:
+the DWIM-bracket syntax, except that
+.code sub
+is an ordinary function call form, which doesn't apply the
+Lisp-1 evaluation semantics to its arguments:
.cblk
;; from is not a list
@@ -22739,6 +22743,29 @@ on Range Indexing\(emexplains the semantics of the range specification.
If the sequence is a list, the output sequence may share substructure
with the input sequence.
+When a
+.code sub
+form is used as a syntactic place, that place denotes a slice of
+.metn seq ,
+which must be a syntactic place itself.
+Overwriting that slice is equivalent to using the
+.code replace
+function. The following equivalences give the semantics, except that
+.codn x ,
+.codn a ,
+.code b
+and
+.code v
+are evaluated only once, in left-to-right order:
+
+.cblk
+ (set (sub x a b) v) <--> (progn (replace x v a b)
+ v)
+
+ (del (sub x a b)) <--> (prog1 (sub x a b)
+ (replace x nil a b))
+.cble
+
.coNP Function @ replace
.synb
.mets (replace < sequence < replacement-sequence >> [ from <> [ to ]])