Commits

Robert Smith committed 2ba2892

Add some error checking on permutation size in generation routines.

Comments (0)

Files changed (1)

permutation-generation.lisp

 
 (defun make-perm-generator (n)
   "Create a generator that generates permutations of size N."
+  (assert (plusp n)
+          (n)
+          "Must provide a positive size for permutation generation. Given ~D."
+          n)
   (let ((perm t))
     (lambda ()
       ;; Check if PERM is NIL (if the generator was exhausted).
 RESULT."
   (let ((perm (gensym "PERM-"))
         (len (gensym "LEN-")))
-    `(loop :with ,len := ,n
-           :for ,perm := (iota-vector (1+,len)) :then (next-perm ,perm ,len)
-           :while ,perm
-           :do (let ((,x (%make-perm :spec (map 'vector 'abs ,perm))))
-                 ,@body)
-           :finally (return ,result))))
+    `(let ((,len ,n))
+       (assert (plusp ,len)
+               (,len)
+               "Must provide a positive size for permutation generation. Given ~D."
+               ,len)
+       (loop :for ,perm := (iota-vector (1+,len)) :then (next-perm ,perm ,len)
+             :while ,perm
+             :do (let ((,x (%make-perm :spec (map 'vector 'abs ,perm))))
+                   ,@body)
+             :finally (return ,result)))))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.