aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArun Isaac2023-11-17 23:24:00 +0000
committerArun Isaac2023-11-17 23:24:00 +0000
commit6842628dc5345b47ca7ce99ea5eba119cd5741e6 (patch)
tree4d9d9a4d60c2e04c7ea12b40a0dcc7c29fd00b69
parentbac6c575e7c03a1946ea46379a5bc98f009408fd (diff)
downloadccwl-6842628dc5345b47ca7ce99ea5eba119cd5741e6.tar.gz
ccwl-6842628dc5345b47ca7ce99ea5eba119cd5741e6.tar.lz
ccwl-6842628dc5345b47ca7ce99ea5eba119cd5741e6.zip
utils: Define pairify behaviour on odd number of elements.
* ccwl/utils.scm (pairify): Ignore extra elements when list has an odd number of elements. * tests/utils.scm ("pairify must ignore extra elements when list has an odd number of elements"): New test.
-rw-r--r--ccwl/utils.scm9
-rw-r--r--tests/utils.scm6
2 files changed, 12 insertions, 3 deletions
diff --git a/ccwl/utils.scm b/ccwl/utils.scm
index 508e884..d9d1ee3 100644
--- a/ccwl/utils.scm
+++ b/ccwl/utils.scm
@@ -1,5 +1,5 @@
;;; ccwl --- Concise Common Workflow Language
-;;; Copyright © 2021, 2022 Arun Isaac <arunisaac@systemreboot.net>
+;;; Copyright © 2021, 2022, 2023 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; This file is part of ccwl.
;;;
@@ -48,12 +48,17 @@
(display (make-string (* 2 level) #\space) port))
(define (pairify lst)
- "Return a list of pairs of successive elements of LST. For example,
+ "Return a list of pairs of successive elements of LST. Ignore extra
+elements when LST has an odd number of elements. For example,
(pairify (list 1 2 3 4 5 6))
+=> ((1 . 2) (3 . 4) (5 . 6))
+
+(pairify (list 1 2 3 4 5 6 7))
=> ((1 . 2) (3 . 4) (5 . 6))"
(match lst
(() '())
+ ((single-element) '())
((first second tail ...)
(cons (cons first second)
(pairify tail)))))
diff --git a/tests/utils.scm b/tests/utils.scm
index b630b10..66220cc 100644
--- a/tests/utils.scm
+++ b/tests/utils.scm
@@ -1,5 +1,5 @@
;;; ccwl --- Concise Common Workflow Language
-;;; Copyright © 2021, 2022 Arun Isaac <arunisaac@systemreboot.net>
+;;; Copyright © 2021, 2022, 2023 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; This file is part of ccwl.
;;;
@@ -216,4 +216,8 @@
0 0)))
(list sum sum-of-squares)))
+(test-equal "pairify must ignore extra elements when list has an odd number of elements"
+ '((1 . 2) (3 . 4) (5 . 6))
+ (pairify (list 1 2 3 4 5 6 7)))
+
(test-end "utils")