(sequence-constructor (list 1 2 3)) = list
(sequence-constructor (vector 1 2 3)) = vector
etc
I'd like to use it for a declare-mappable macro that extends functions
of one argument
to map over sequences. As in (sin (list 1 2 3)) = (list (sin 1) (sin
2) (sin 3)).
The sequence-map function is close, but it produces sequences.
Now I could of course hard code the usual suspects, but it seems
somewhat inelegant.
--
Jens Axel Søgaard
____________________
Racket Users list:
http://lists.racket-lang.org/users
The Scala collections classes go to a *lot* of work to make this all
work nicely in a typed setting. But it's way more than just bounded
polymorphism that they use.
>
> -- Matthias
>
>
>
>
> On Apr 29, 2012, at 8:12 AM, Jens Axel Søgaard wrote:
>
>> Given a sequence is there way to get the "constructor" of the sequence?
>>
>> (sequence-constructor (list 1 2 3)) = list
>> (sequence-constructor (vector 1 2 3)) = vector
>> etc
>>
>> I'd like to use it for a declare-mappable macro that extends functions
>> of one argument
>> to map over sequences. As in (sin (list 1 2 3)) = (list (sin 1) (sin
>> 2) (sin 3)).
>>
>> The sequence-map function is close, but it produces sequences.
>> Now I could of course hard code the usual suspects, but it seems
>> somewhat inelegant.
>>
>> --
>> Jens Axel Søgaard
>>
>> ____________________
>> Racket Users list:
>> http://lists.racket-lang.org/users
>
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
--
sam th
sa...@ccs.neu.edu
I think you are seeking a distributive mapping of two functions f and g
;(f (g . rest)) -> g (f(arg) . f(arg2).....)
So (I think) the evaluative order of racket becomes the impediment, and not the
type of the sequence
try this:
(require mzlib/defmacro)
(define-macro (distribute . (f g . rest))
`(,g ,@(map (λ (x) `(,f ,x)) rest)))
> (distribute sin list 1 2 3)
(0.8414709848078965 0.9092974268256817 0.1411200080598672)
> (distribute sin vector 1 2 3)
#(0.8414709848078965 0.9092974268256817 0.1411200080598672)
>> (distribute sin list 1 2 3)
> (0.8414709848078965 0.9092974268256817 0.1411200080598672)
This makes the constructor explicit. That's what I want to avoid.
The problem is that a user can define his own type of sequence,
and I'd like my function work with user defined sequences too.
--
Jens Axel Søgaard
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
I believe that the Scala folks have handled this too. I really
recommend looking a what they've done.
--
sam th
sa...@ccs.neu.edu
Off-topic, but it bugs me to see posted examples that use
`define-macro'... Note that using plain macros is not only more
robust, it's also much easier to read and to write. Following the
above macro is IMO much harder than following this:
(define-syntax-rule (distribute f g x ...) (g (f x) ...))
--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://barzilay.org/ Maze is Life!