I'm wondering, is there any implementation of map-filter in CPS version in Scheme?
example:
(map-filter square even? '(1 2 3 4)) -> '(4 16)
I'm wondering, is there any implementation of map-filter in CPS version in Scheme?
example:
(map-filter square even? '(1 2 3 4)) -> '(4 16)
You can define map-filter&
using filter&
and map&
:
(define (map-filter& proc& pred?& lst k)
(filter& pred?&
lst
(lambda (new-lst)
(map& proc& new-lst k))))
[This answer give two versions of the function the person described: not CPS versions of it. I think the question is ambiguous but this is probably not the right answer: Sylwester's is.]
If what you want is what you've described (which I'm fairly sure is not CPS), then something like this works:
(define (map-filter mapper filterer lst)
(map mapper (filter filterer lst)))
In Racket then this might be better as this:
(define (map-filter mapper filterer lst)
(for/list ([e (in-list lst)]
#:when (filterer e))
(mapper e)))
which perhaps has a better chance of avoiding consing an intermediate list but smells nastier to me.