[racket] Constructors for sequences

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Mon Apr 30 03:06:30 EDT 2012

2012/4/30 Asumu Takikawa <asumu at ccs.neu.edu>:
> On 2012-04-29 12:43:48 -0400, Matthias Felleisen wrote:
>> What you're really saying is that sequence-map uses the wrong kind of type. Specifically, it should be polymorphic in the sequence constructor instead of mapping everything to the top of the class hierarchy (sequence).
> I don't think this is just a type issue. The sequence that is returned
> by `sequence-map` is lazy, even if the original sequence was not. That's
> why you can't get the original type.

Good point.

However, I don't mind doing some manual labor:

(define (map-sqr a-sequence)
    (let ([construct (sequence-constructor a-sequence)])
       (apply construct (sequence->list (sequence-map sqr a-sequence)))))

(map-sqr '(1 2 3))  ; => (1 4 9)
(map-sqr #(1 2 3)) ; => #(1 4 9)

Jens Axel Søgaard

Posted on the users mailing list.