* [PATCH] vectors: add (vector-last) support
@ 2020-12-21 9:44 Aleix Conchillo Flaqué
2021-02-12 19:56 ` Aleix Conchillo Flaqué
0 siblings, 1 reply; 4+ messages in thread
From: Aleix Conchillo Flaqué @ 2020-12-21 9:44 UTC (permalink / raw)
To: guile-devel
[-- Attachment #1: Type: text/plain, Size: 233 bytes --]
Hi,
Attached is a patch to add support for a new function (vector-last)
that will retrieve the last element of a vector.
This avoids having to do (vector-ref v (- (vector-length v) 1)).
Let me know what you think.
Thanks,
Aleix
[-- Attachment #2: 0001-vectors-add-vector-last-support.patch --]
[-- Type: application/octet-stream, Size: 5142 bytes --]
From bbb2212775a1cd4b521d7674d60f2410f797fb78 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= <aconchillo@gmail.com>
Date: Mon, 21 Dec 2020 01:33:11 -0800
Subject: [PATCH] vectors: add (vector-last) support
* 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 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.29.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] vectors: add (vector-last) support
2020-12-21 9:44 Aleix Conchillo Flaqué
@ 2021-02-12 19:56 ` Aleix Conchillo Flaqué
0 siblings, 0 replies; 4+ messages in thread
From: Aleix Conchillo Flaqué @ 2021-02-12 19:56 UTC (permalink / raw)
To: guile-devel
[-- Attachment #1: Type: text/plain, Size: 386 bytes --]
Any interest on this one?
On Mon, Dec 21, 2020 at 1:44 AM Aleix Conchillo Flaqué <aconchillo@gmail.com>
wrote:
> Hi,
>
> Attached is a patch to add support for a new function (vector-last)
> that will retrieve the last element of a vector.
>
> This avoids having to do (vector-ref v (- (vector-length v) 1)).
>
> Let me know what you think.
>
> Thanks,
>
> Aleix
>
[-- Attachment #2: Type: text/html, Size: 767 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH] vectors: add (vector-last) support
@ 2021-02-12 20:03 Aleix Conchillo Flaqué
2021-05-25 19:04 ` Aleix Conchillo Flaqué
0 siblings, 1 reply; 4+ messages in thread
From: Aleix Conchillo Flaqué @ 2021-02-12 20:03 UTC (permalink / raw)
To: guile-devel; +Cc: Aleix Conchillo Flaqué
* 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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] vectors: add (vector-last) support
2021-02-12 20:03 [PATCH] vectors: add (vector-last) support Aleix Conchillo Flaqué
@ 2021-05-25 19:04 ` Aleix Conchillo Flaqué
0 siblings, 0 replies; 4+ messages in thread
From: Aleix Conchillo Flaqué @ 2021-05-25 19:04 UTC (permalink / raw)
To: guile-devel
[-- Attachment #1: Type: text/plain, Size: 5601 bytes --]
ping!
On Fri, Feb 12, 2021, 12:03 PM Aleix Conchillo Flaqué <aconchillo@gmail.com>
wrote:
> * 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
>
>
[-- Attachment #2: Type: text/html, Size: 6623 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-05-25 19:04 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-12 20:03 [PATCH] vectors: add (vector-last) support Aleix Conchillo Flaqué
2021-05-25 19:04 ` 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é
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).