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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to