GNU bug report logs - #54172
[PATCH] vectors: add (vector-last) support

Previous Next

Package: guile;

Reported by: Aleix Conchillo Flaqué <aconchillo <at> gmail.com>

Date: Sat, 26 Feb 2022 18:15:01 UTC

Severity: normal

Tags: patch

To reply to this bug, email your comments to 54172 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-guile <at> gnu.org:
bug#54172; Package guile. (Sat, 26 Feb 2022 18:15:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Aleix Conchillo Flaqué <aconchillo <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-guile <at> gnu.org. (Sat, 26 Feb 2022 18:15:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Aleix Conchillo Flaqué <aconchillo <at> gmail.com>
To: bug-guile <at> gnu.org
Cc: Aleix Conchillo Flaqué <aconchillo <at> gmail.com>
Subject: [PATCH] vectors: add (vector-last) support
Date: Sat, 26 Feb 2022 10:14:30 -0800
* libguile/vectors.c: add (vector-last) support.

* libguile/vectors.h: define scm_vector_last and scm_c_vector_last.

* doc/ref/api-data.texi (Vector Accessors): add documentation for
(vector-last).
---
 doc/ref/api-data.texi         | 10 ++++++++++
 libguile/vectors.c            | 30 +++++++++++++++++++++++++++++-
 libguile/vectors.h            |  6 ++++--
 test-suite/tests/srfi-43.test | 12 +++++++++++-
 4 files changed, 54 insertions(+), 4 deletions(-)

diff --git a/doc/ref/api-data.texi b/doc/ref/api-data.texi
index 8658b9785..cf7253b31 100644
--- a/doc/ref/api-data.texi
+++ b/doc/ref/api-data.texi
@@ -6277,6 +6277,16 @@ Return the contents of position @var{k} (a @code{size_t}) of
 @var{vec}.
 @end deftypefn
 
+@rnindex vector-last
+@deffn {Scheme Procedure} vector-last vec
+@deffnx {C Function} scm_vector_last (vec)
+Return the contents of the last element of @var{vec}.
+@end deffn
+
+@deftypefn {C Function} SCM scm_c_vector_last (SCM vec)
+Return the contents of the last element of @var{vec}.
+@end deftypefn
+
 A vector created by one of the dynamic vector constructor procedures
 (@pxref{Vector Creation}) can be modified using the following
 procedures.
diff --git a/libguile/vectors.c b/libguile/vectors.c
index 18c7dc54d..e5aa297df 100644
--- a/libguile/vectors.c
+++ b/libguile/vectors.c
@@ -188,7 +188,35 @@ scm_c_vector_ref (SCM v, size_t k)
 }
 #undef FUNC_NAME
 
-SCM_DEFINE (scm_vector_set_x, "vector-set!", 3, 0, 0, 
+SCM_DEFINE (scm_vector_last, "vector-last", 1, 0, 0,
+	    (SCM vector),
+            "@samp{Vector-ref} returns the contents of the last element of\n"
+            "@var{vector}.\n\n"
+            "@lisp\n"
+            "(vector-ref '#(3 1 27 5)) @result{} 5\n"
+            "@end lisp")
+#define FUNC_NAME s_scm_vector_last
+{
+  return scm_c_vector_last (vector);
+}
+#undef FUNC_NAME
+
+SCM
+scm_c_vector_last (SCM v)
+#define FUNC_NAME s_scm_vector_last
+{
+  SCM_VALIDATE_VECTOR (1, v);
+
+  size_t len = SCM_I_VECTOR_LENGTH (v);
+
+  if (len == 0)
+    scm_out_of_range (NULL, 0);
+
+  return scm_c_vector_ref (v, len - 1);
+}
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_vector_set_x, "vector-set!", 3, 0, 0,
 	    (SCM vector, SCM k, SCM obj),
             "@var{k} must be a valid index of @var{vector}.\n"
             "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n"
diff --git a/libguile/vectors.h b/libguile/vectors.h
index 005999a2b..b729e6105 100644
--- a/libguile/vectors.h
+++ b/libguile/vectors.h
@@ -1,7 +1,7 @@
 #ifndef SCM_VECTORS_H
 #define SCM_VECTORS_H
 
-/* Copyright 1995-1996,1998,2000-2002,2004-2006,2008-2009,2011,2014,2018
+/* Copyright 1995-1996,1998,2000-2002,2004-2006,2008-2009,2011,2014,2018,2020
      Free Software Foundation, Inc.
 
    This file is part of Guile.
@@ -32,13 +32,14 @@ SCM_API SCM scm_vector_p (SCM x);
 SCM_API SCM scm_vector_length (SCM v);
 SCM_API SCM scm_vector (SCM l);
 SCM_API SCM scm_vector_ref (SCM v, SCM k);
+SCM_API SCM scm_vector_last (SCM v);
 SCM_API SCM scm_vector_set_x (SCM v, SCM k, SCM obj);
 SCM_API SCM scm_make_vector (SCM k, SCM fill);
 SCM_API SCM scm_vector_to_list (SCM v);
 SCM_API SCM scm_vector_fill_x (SCM v, SCM fill_x);
 SCM_API SCM scm_vector_move_left_x (SCM vec1, SCM start1, SCM end1,
 				    SCM vec2, SCM start2);
-SCM_API SCM scm_vector_move_right_x (SCM vec1, SCM start1, SCM end1, 
+SCM_API SCM scm_vector_move_right_x (SCM vec1, SCM start1, SCM end1,
 				     SCM vec2, SCM start2);
 SCM_API SCM scm_vector_copy (SCM vec);
 SCM_API SCM scm_vector_copy_partial (SCM vec, SCM start, SCM end);
@@ -48,6 +49,7 @@ SCM_API int scm_is_vector (SCM obj);
 SCM_API SCM scm_c_make_vector (size_t len, SCM fill);
 SCM_API size_t scm_c_vector_length (SCM vec);
 SCM_API SCM scm_c_vector_ref (SCM vec, size_t k);
+SCM_API SCM scm_c_vector_last (SCM vec);
 SCM_API void scm_c_vector_set_x (SCM vec, size_t k, SCM obj);
 SCM_API const SCM *scm_vector_elements (SCM array,
 					scm_t_array_handle *h,
diff --git a/test-suite/tests/srfi-43.test b/test-suite/tests/srfi-43.test
index 554843e75..4d0093974 100644
--- a/test-suite/tests/srfi-43.test
+++ b/test-suite/tests/srfi-43.test
@@ -1,6 +1,6 @@
 ;;;; srfi-43.test --- test suite for SRFI-43 Vector library -*- scheme -*-
 ;;;;
-;;;; Copyright (C) 2014 Free Software Foundation, Inc.
+;;;; Copyright (C) 2014, 2020 Free Software Foundation, Inc.
 ;;;;
 ;;;; This library is free software; you can redistribute it and/or
 ;;;; modify it under the terms of the GNU Lesser General Public
@@ -390,6 +390,16 @@
   (pass-if-error "non-vector" (vector-ref '(a b c) 0))
   (pass-if-error "inexact index" (vector-ref '#(a b c) 1.0)))
 
+;;
+;; vector-last
+;;
+
+(with-test-prefix "vector-last"
+  (pass-if-equal "single element" 'a (vector-last '#(a)))
+  (pass-if-equal "multiple elements" 'c (vector-last '#(a b c)))
+  (pass-if-error "empty vector" (vector-last '#()))
+  (pass-if-error "non-vector" (vector-last '(a b c))))
+
 ;;
 ;; vector-length
 ;;
-- 
2.35.1





This bug report was last modified 2 years and 305 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.