[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] html-stream: idiom for large iterations?
From: |
Alejandro Forero Cuervo |
Subject: |
Re: [Chicken-users] html-stream: idiom for large iterations? |
Date: |
Sat, 30 Apr 2005 07:00:33 -0500 |
User-agent: |
Mutt/1.5.6+20040907i |
Hey.
> I'm fairly new to Scheme and streams, and am trying to grok how to use
> the html-stream extension to generate an HTML document with a large
> repeating section. I wrote the following (to compare its performance
> to similar functions I implemented other languages):
>
> (require-extension html-stream)
> (stream->string
> (html-stream
> (html (head (title "Hello, world!"))
> (body (ul
> (apply stream-append
> (map (lambda (x) (html-stream
> (li "This is item " x)))
> (iota 1000))))))))
>
> However, this fails because the (map) generates a list of 1000
> elements, which exceeds the parameter limit for (apply).
>
> Is there an idiomatic way to rewrite this, and avoid the parameter limit?
I would suggest using “stream-concatenate” instead of “apply
stream-append” (or even “fold-right stream-append”). The only
difference is that you wouldn't pass a list but a stream. This has
one advantage (that is usually minor but can, in some cases, make a
very big difference): the information you are appending
(concatenating) is only built as it is consumed.
Your code would thus look like this:
(require-extension html-stream stream-ext)
(stream->string
(html-stream
(html (head (title "Hello, world!"))
(body
(ul
(stream-concatenate
(stream-map (lambda (x) (html-stream (li "This is item " x)))
(stream-iota 1000))))))))
stream-concatenate is defined in the stream-ext egg; since you are
doing stream-append, I suppose you're already using it.
I hope it works.
Alejo.
http://bachue.com/alejo
---=( Comunidad de Usuarios de Software Libre en Colombia )=---
---=( http://bachue.com/colibri )=--=( address@hidden )=---
- Re: [Chicken-users] html-stream: idiom for large iterations?,
Alejandro Forero Cuervo <=