Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> I wonder how the lazy sequence support stacks up to Clojure, Haskell, etc. support.

A bit of a tangent, but I've been looking at the series library[0] recently. It makes it possible to write functional style code which is compiled into a loop for efficiency. For example:

  (defun integers ()
    "Returns a series of all of the integers."
    (declare (optimizable-series-function))
    (scan-range :from 1))

  (defun squares ()
    "Returns a series of all of the square numbers."
    (declare (optimizable-series-function))
    (map-fn t 
            (lambda (x) (* x x)) 
            (integers)))

  (defun sum-squares (n)
    "Returns the sum of the first N square numbers."
    (collect-sum (subseries (squares) 0 k)))
Although the above definition of sum-squares seems like it would be inefficient, it is roughly the same as the following:

  (defun sum-squares (n)
    "Returns the sum of the first N square numbers."
    (loop for i from 1 to n
          for square = (* i i)
          sum square))
I find it pretty awesome that it is possible to write code that is efficient and functional like that, but I guess series is a bit too magical to be put to actual use.

[0] http://series.sourceforge.net/



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: