newLISP Fan Club

Forum => Anything else we might add? => Topic started by: jopython on August 30, 2013, 12:12:13 PM

Title: Reducers in newlisp?
Post by: jopython on August 30, 2013, 12:12:13 PM
Is it possible to have to have reducers in newlisp too as mentioned in this //http://adambard.com/blog/Reducers-explained-through-Python/ ?



The key takeaway from the article is
Quote
This is the idea behind reducers. If you take your mapping function (map, filter, flatten, etc.), and have it modify the reducing function, you can perform any number and combination of mappings without having to repeatedly iterate through the list.
Title: Re: Reducers in newlisp?
Post by: TedWalther on August 30, 2013, 10:40:29 PM
Thanks, that is a very cool URL.



I sure could have used Reducers when I implemented the (combinations) function.



To make it efficient, I had to switch from map to dolist to avoid that exact problem that the Reducer functions address.



http://www.newlisp.org/index.cgi?page=Code_Snippets



  (define (combinations n k (r '()))
     (if (= (length r) n)
       (list (sort r <))
       (apply append (map
                (fn (x) (combinations n ((+ 1 $idx) k) (cons x r))) k))))

 ; (combinations 2 '(a b c))
 ; =>  ((a b) (a c) (b c))


I'd really like to know how that would look using Reducers instead of dolist.  dolist did do the job very efficiently, map blew up my computer.