Fix lazy evaluation of second argument to append-pipes for consistency with make-pipe; fixes mappend-pipe laziness
Wed Feb 2 09:28:10 PST 2011 David Lichteblau <david@lichteblau.com>
* Fix lazy evaluation of second argument to append-pipes for consistency with make-pipe; fixes mappend-pipe laziness
diff -rN -u old-plexippus-xpath/pipes.lisp new-plexippus-xpath/pipes.lisp
--- old-plexippus-xpath/pipes.lisp 2014-08-23 06:23:56.000000000 -0700
+++ new-plexippus-xpath/pipes.lisp 2014-08-23 06:23:56.000000000 -0700
@@ -128,12 +128,19 @@
(make-pipe result (map-pipe-filtering fn tail filter-test))
(map-pipe-filtering fn tail filter-test)))))
-(defun append-pipes (pipex pipey)
- "return a pipe that appends two pipes"
+(defmacro append-pipes (pipex pipey)
+ "return a pipe that appends two pipes.
+
+ The evaluation style of this macro has been chosen to be consistent
+ with MAKE-PIPE: The first argument is evaluated eagerly; the second
+ argument lazily."
+ `(%append-pipes ,pipex #'(lambda () ,pipey)))
+
+(defun %append-pipes (pipex pipey-fun)
(if (pipe-empty-p pipex)
- pipey
+ (funcall pipey-fun)
(make-pipe (pipe-head pipex)
- (append-pipes (pipe-tail pipex) pipey))))
+ (append-pipes (pipe-tail pipex) (funcall pipey-fun)))))
(defun mappend-pipe (fn pipe)
"lazily map fn over pipe, appending results"
diff -rN -u old-plexippus-xpath/xpath-test.lisp new-plexippus-xpath/xpath-test.lisp
--- old-plexippus-xpath/xpath-test.lisp 2014-08-23 06:23:56.000000000 -0700
+++ new-plexippus-xpath/xpath-test.lisp 2014-08-23 06:23:56.000000000 -0700
@@ -627,3 +627,12 @@
(assert-equal
'(("a" (("id" "1"))) ("c" (("id" "6"))))
(all-nodes (evaluate "//c[position()=2]|//a[@id='1']" d)))))
+
+(deftest mappend-lazy
+ (let* ((x nil)
+ (pipe (mappend-pipe (lambda (x) (list (- x)))
+ (make-pipe 1 (progn (setf x t) '(2))))))
+ (assert (not x))
+ (assert (eql -1 (pipe-head pipe)))
+ (assert (eql -2 (pipe-head (pipe-tail pipe))))
+ (assert x)))