From 6a578338b6666844e6df34876c775974f6ec7dab Mon Sep 17 00:00:00 2001 From: Mark Oteiza Date: Tue, 25 Apr 2017 00:13:39 -0400 Subject: [PATCH 1/3] Add -iota (#207) --- dash.el | 17 +++++++++++++++++ dev/examples.el | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/dash.el b/dash.el index 078175c..afa5fad 100644 --- a/dash.el +++ b/dash.el @@ -2154,6 +2154,23 @@ N is the length of the returned list." (push (funcall fun (car r)) r)) (nreverse r)))) +(defun -iota (count &optional start step) + "Return a list containing COUNT numbers. +Starts from START and adds STEP each time. The default START is +zero, the default STEP is 1. +This function takes its name from the corresponding primitive in +the APL language." + (if (or (not (integerp count)) (< count 0)) + (signal 'wrong-type-argument count)) + (if (and step (zerop step)) (make-list count start) + (let ((res '()) + (x (or start 0)) + (dx (or step 1))) + (while (<= 0 (setq count (1- count))) + (push x res) + (setq x (+ x dx))) + (nreverse res)))) + (defun -fix (fn list) "Compute the (least) fixpoint of FN with initial input LIST. diff --git a/dev/examples.el b/dev/examples.el index 092e57e..3e0ca88 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -614,6 +614,10 @@ new list." (-interleave '(1 2 3) '("a" "b")) => '(1 "a" 2 "b") (-interleave '(1 2 3) '("a" "b" "c" "d")) => '(1 "a" 2 "b" 3 "c")) + (defexamples -iota + (-iota 6) => '(0 1 2 3 4 5) + (-iota 4 2.5 -2) => '(2.5 0.5 -1.5 -3.5)) + (defexamples -zip-with (-zip-with '+ '(1 2 3) '(4 5 6)) => '(5 7 9) (-zip-with 'cons '(1 2 3) '(4 5 6)) => '((1 . 4) (2 . 5) (3 . 6)) From 5f7f2d6c5f723704a6485bbc3ca6f9baf219b868 Mon Sep 17 00:00:00 2001 From: Mark Oteiza Date: Wed, 26 Apr 2017 01:12:51 -0400 Subject: [PATCH 2/3] Add example for an iota error condition --- dev/examples.el | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dev/examples.el b/dev/examples.el index 3e0ca88..a50cb91 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -616,7 +616,8 @@ new list." (defexamples -iota (-iota 6) => '(0 1 2 3 4 5) - (-iota 4 2.5 -2) => '(2.5 0.5 -1.5 -3.5)) + (-iota 4 2.5 -2) => '(2.5 0.5 -1.5 -3.5) + (-iota -1) !!> wrong-type-argument) (defexamples -zip-with (-zip-with '+ '(1 2 3) '(4 5 6)) => '(5 7 9) From d30867607c80012dac297b70984c9fae5c11e0b6 Mon Sep 17 00:00:00 2001 From: Mark Oteiza Date: Fri, 8 Jan 2021 14:01:16 -0500 Subject: [PATCH 3/3] Fix signal argument and case where start is null If either of the optional arguments are empty, just set them to the defaults. --- dash.el | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/dash.el b/dash.el index afa5fad..375cc7d 100644 --- a/dash.el +++ b/dash.el @@ -2160,16 +2160,16 @@ Starts from START and adds STEP each time. The default START is zero, the default STEP is 1. This function takes its name from the corresponding primitive in the APL language." - (if (or (not (integerp count)) (< count 0)) - (signal 'wrong-type-argument count)) - (if (and step (zerop step)) (make-list count start) - (let ((res '()) - (x (or start 0)) - (dx (or step 1))) + (when (not (natnump count)) + (signal 'wrong-type-argument (list #'natnump count))) + (or start (setq start 0)) + (or step (setq step 1)) + (if (zerop step) (make-list count start) + (let (result) (while (<= 0 (setq count (1- count))) - (push x res) - (setq x (+ x dx))) - (nreverse res)))) + (push start result) + (setq start (+ start step))) + (nreverse result)))) (defun -fix (fn list) "Compute the (least) fixpoint of FN with initial input LIST.