The optimizer doesn't make the `call-with-values` free, but the cost isn't
huge. This program: https://gist.github.com/3e306257338f2ec44854 shows
about 15% slowdown using your versions over using `let-values` (and
requiring a specific number of values).

Sam

On Thu, Oct 22, 2015 at 11:52 AM Jens Axel Søgaard <[email protected]>
wrote:

> Hi All,
>
> This is an old issue, but as far as I know, there is no convenient to
> discard extra values in a context where multiple values are received.
>
> I propose we add first-value, second-value, and, nth-value:
>
>     > (first-value (values 'a 'b 'c))
>     'a
>
>     > (second-value (values 'a 'b 'c 'd))
>     'b
>
>     > (nth-value    (values 'a 'b 'c 'd) 2)
>     'c
>
> The most common use-case for me is discarding extra values from for/fold.
>
> These operations could be implemented like this:
>
>     (define-syntax-rule (first-value expr)
>       (call-with-values (λ () expr) (λ (a . _) a)))
>
>     (define-syntax-rule (second-value expr)
>       (call-with-values (λ () expr) (λ (a b . _) b)))
>
>     (define-syntax-rule (nth-value expr n)
>       (call-with-values (λ () expr)
>                         (λ vs
>                           (unless (>= (length vs) n)
>                             (error 'nth-value (~a "expected at least " n
> "values")))
>                           (list-ref vs n))))
>
> However thinking about the efficiency - it feels wrong to use
> call-with-values.
> Does the optimizer handle call-with-values so well, that first-value and
> second-value becomes efficient?
>
> /Jens Axel
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-dev/CABefVgy%2Bzfu0N730TYFXtTsk2kOKsOFrHX6v4B%2B43Zm-ZUkuHw%40mail.gmail.com
> <https://groups.google.com/d/msgid/racket-dev/CABefVgy%2Bzfu0N730TYFXtTsk2kOKsOFrHX6v4B%2B43Zm-ZUkuHw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-dev/CAK%3DHD%2BZ8GaGrXyFrfzbCWZTmNi9dXZ2eHjG3eGNAetg83GS4ng%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to