summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2018-01-02 02:57:55 -0800
committerKaz Kylheku <kaz@kylheku.com>2018-01-02 02:57:55 -0800
commit94f730114aeb371a65f5d43650bf1d0045bff9e2 (patch)
treed764d057717987a0a1c4e1149b1ac317f6694412 /lib.c
parent2a43a5b68507d24e33783996c6a5fa99dd8233a2 (diff)
downloadtxr-94f730114aeb371a65f5d43650bf1d0045bff9e2.tar.gz
txr-94f730114aeb371a65f5d43650bf1d0045bff9e2.tar.bz2
txr-94f730114aeb371a65f5d43650bf1d0045bff9e2.zip
last: rewrite using seq_info.
* lib.c (last): Use seq_info classification rather than relying on listp.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c27
1 files changed, 17 insertions, 10 deletions
diff --git a/lib.c b/lib.c
index fe8efbb6..6ca611b9 100644
--- a/lib.c
+++ b/lib.c
@@ -677,16 +677,23 @@ val lastcons(val list)
val last(val seq, val n)
{
- if (null_or_missing_p(n)) {
- if (listp(seq))
- return lastcons(seq);
- return sub(seq, negone, t);
- } else {
- if (listp(seq))
- return nthlast(n, seq);
- return if3(plusp(n),
- sub(seq, neg(n), t),
- sub(seq, t, t));
+ seq_info_t si = seq_info(seq);
+
+ switch (si.kind) {
+ case SEQ_NIL:
+ return nil;
+ case SEQ_LISTLIKE:
+ return if3(null_or_missing_p(n),
+ lastcons(seq),
+ nthlast(n, seq));
+ case SEQ_VECLIKE:
+ return if3(null_or_missing_p(n),
+ sub(seq, negone, t),
+ if3(plusp(n),
+ sub(seq, neg(n), t),
+ sub(seq, t, t)));
+ default:
+ uw_throwf(error_s, lit("last: ~s isn't a sequence"), seq, nao);
}
}