News for hackers. Stuff that matters.
Re:Ask HN: How to compose two mapping functions into a third? by paulgb on Wednesday December 10, 2008 @10:45PM (#393908)
You could use lazy lists like (I think) Haskell uses:

  (define (lazy-range start end) ; create a lazy list that returns the next number in the range [start end]
    (if (= start end) (cons start '())
    (cons start
      (delay (lazy-range (add1 start) end)))))

  (define (lazy-map fn lst) ; map a function to every element of a lazy list, returning a lazy list
    (if (empty? lst) '()
      (cons (fn (car lst))
        (delay (lazy-map fn (force (cdr lst)))))))

  (define map1 (curry lazy-map (curry + 10))) ; equivalent to map1 from your example
  (define map2 (curry lazy-map (lambda (x) (expt x 2)))) ; equivalent to map2 from your example

  (define (lazy-list->list lazy) ; convert a lazy list to a list (for printing, etc)
    (if (empty? lazy) '()
      (cons (car lazy) (lazy-list->list (force (cdr lazy))))))

  (lazy-list->list (map1 (map2 (lazy-range 1 10)))) ; example use
Because the lists are lazy, the maps will only be applied as each value is actually needed, like you described.
Re:Ask HN: How to compose two mapping functions into a third? by gruseom on Thursday December 11, 2008 @01:55AM (#394066)
Yes, I'm beginning to think the simple solution to this boils down to some flavor of coroutines/continuations/laziness - basically, the stuff that Scheme has that CL does not.

The reason I find this interesting is that it's the first real problem I've run into where CL didn't have an easy way to do what I want. There are workarounds, of course. But it's interesting to run into a production (as opposed to academic) problem where the elegant solution isn't simply available.

Re:Ask HN: How to compose two mapping functions into a third? by simonb on Thursday December 11, 2008 @02:43AM (#394108)
Have you tried (semi-standard) SERIES?

http://series.sourceforge.net

"A series is a data structure much like a sequence, with similar kinds of operations. The difference is that in many situations, operations on series may be composed functionally and yet execute iteratively, without the need to construct intermediate series values explicitly. In this manner, series provide both the clarity of a functional programming style and the efficiency of an iterative programming style."

Re:Ask HN: How to compose two mapping functions into a third? by gruseom on Saturday December 13, 2008 @11:31PM (#397218)
No, I haven't tried it. In fact, I didn't know it was available open-source. Do you use it? If so, do you like it?
Re:Ask HN: How to compose two mapping functions into a third? by silverlake on Thursday December 11, 2008 @11:22AM (#394406)
You can implement the local transformations for short cut deforestation with CL macros. Then you can easily reuse the technique everywhere. Look up Andy Gill's paper from 1993.
Re:Ask HN: How to compose two mapping functions into a third? by gruseom on Thursday December 11, 2008 @11:42AM (#394429)
Google brings up a few papers from around that time. The closest match looks like "A short cut to deforestation" - is that the one you mean?

http://portal.acm.org/citation.cfm?id=165214