summaryrefslogtreecommitdiff
path: root/module/srfi/srfi-1.scm
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2010-10-08 11:03:51 +0200
committerLudovic Courtès <ludo@gnu.org>2010-10-08 15:25:56 +0200
commit58ee1beabec22eaad41eecf6fdd4c0032b6608e3 (patch)
tree0ac3ec56ff6e38574c971dea81c4e3b91d7fc90d /module/srfi/srfi-1.scm
parenta6505cb49c0971671bcdc87eb6abbc3204d94d97 (diff)
downloadguile-58ee1beabec22eaad41eecf6fdd4c0032b6608e3.tar.gz
SRFI-1: Rewrite `filter-map' in Scheme.
This partially reverts commit c16359466bcc3f2ebf6d750c069f787f629fc625 (Thu Mar 17 2005). * libguile/srfi-1.c (scm_srfi1_filter_map): Remove. * libguile/srfi-1.h (scm_srfi1_filter_map): Ditto. * module/srfi/srfi-1.scm (filter-map): New procedure.
Diffstat (limited to 'module/srfi/srfi-1.scm')
-rw-r--r--module/srfi/srfi-1.scm22
1 files changed, 22 insertions, 0 deletions
diff --git a/module/srfi/srfi-1.scm b/module/srfi/srfi-1.scm
index d6cefcdbd..5bcc3611c 100644
--- a/module/srfi/srfi-1.scm
+++ b/module/srfi/srfi-1.scm
@@ -511,6 +511,28 @@ has just one element then that's the return value."
;; OPTIMIZE-ME: Re-use cons cells of list1
(define map! map)
+(define (filter-map proc list1 . rest)
+ "Apply PROC to to the elements of LIST1... and return a list of the
+results as per SRFI-1 `map', except that any #f results are omitted from
+the list returned."
+ (if (null? rest)
+ (let lp ((l list1)
+ (rl '()))
+ (if (null? l)
+ (reverse! rl)
+ (let ((res (proc (car l))))
+ (if res
+ (lp (cdr l) (cons res rl))
+ (lp (cdr l) rl)))))
+ (let lp ((l (cons list1 rest))
+ (rl '()))
+ (if (any1 null? l)
+ (reverse! rl)
+ (let ((res (apply proc (map1 car l))))
+ (if res
+ (lp (map1 cdr l) (cons res rl))
+ (lp (map1 cdr l) rl)))))))
+
(define (pair-for-each f clist1 . rest)
(if (null? rest)
(let lp ((l clist1))