unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: "Aleix Conchillo Flaqué" <aconchillo@gmail.com>
To: guile-devel@gnu.org
Cc: "Aleix Conchillo Flaqué" <aconchillo@gmail.com>
Subject: [PATCH] vectors: add (vector-last) support
Date: Fri, 12 Feb 2021 12:03:48 -0800	[thread overview]
Message-ID: <20210212200348.34042-1-aconchillo@gmail.com> (raw)

* 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).

Signed-off-by: Aleix Conchillo Flaqué <aconchillo@gmail.com>
---
 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 2ad13f5a5..0878c1173 100644
--- a/doc/ref/api-data.texi
+++ b/doc/ref/api-data.texi
@@ -6354,6 +6354,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 0f1e6085e..f079f1f53 100644
--- a/libguile/vectors.c
+++ b/libguile/vectors.c
@@ -193,7 +193,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 41e2c8909..08fe19f49 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);
 
@@ -47,6 +48,7 @@ SCM_API int scm_is_simple_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 vec,
 					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.30.0




             reply	other threads:[~2021-02-12 20:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-12 20:03 Aleix Conchillo Flaqué [this message]
2021-05-25 19:04 ` [PATCH] vectors: add (vector-last) support Aleix Conchillo Flaqué
  -- strict thread matches above, loose matches on Subject: below --
2020-12-21  9:44 Aleix Conchillo Flaqué
2021-02-12 19:56 ` Aleix Conchillo Flaqué

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210212200348.34042-1-aconchillo@gmail.com \
    --to=aconchillo@gmail.com \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).