[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

what's the best way to get one of several multiple-values

Jim Newton

7/9/2015 3:18:00 PM

For example if I have a function FOO which returns (values a b c)
I'd like to write three accessor functions which return the a, b, and c respectively.

I know several ways to write this, but i'm not sure which is the most efficient.

(define get-1 (x)
(multiple-value-bind (a b c) (FOO x)
(declare (ignore b c))
a)))

(define get-2 (x)
(multiple-value-bind (a b c) (FOO x)
(declare (ignore a c))
b)))

(define get-3 (x)
(multiple-value-bind (a b c) (FOO x)
(declare (ignore a b))
c)))
12 Answers

Kaz Kylheku

7/9/2015 3:36:00 PM

0

On 2015-07-09, Jim Newton <jimka.issy@gmail.com> wrote:
> For example if I have a function FOO which returns (values a b c)
> I'd like to write three accessor functions which return the a, b, and c respectively.
>
> I know several ways to write this, but i'm not sure which is the most efficient.
>
> (define get-1 (x)

Define?

> (multiple-value-bind (a b c) (FOO x)
> (declare (ignore b c))
> a)))

You probably want these mini-functions inlined. You could make it a macro,
in which the symbol plays an API role:

(defmacro value (func sym x)
(let ((vars '(a b c)))
(unless (member sym vars)
(error "value: ~s must be one of ~s" sym vars))
`(multiple-value-bind (,@vars) (,func ,x)
(declare (ignore ,@(remove sym vars))
,sym))))

(value foo a (whatever))

The names could be descriptive ("pseudo-slots") rather than A, B, C.

Matthew Carter

7/9/2015 4:14:00 PM

0

Jim Newton <jimka.issy@gmail.com> writes:

> For example if I have a function FOO which returns (values a b c)
>
> I'd like to write three accessor functions which return the a, b, and
> c respectively.
>
> I know several ways to write this, but i'm not sure which is the most
> efficient.
>
> (define get-1 (x)
> (multiple-value-bind (a b c) (FOO x)
> (declare (ignore b c))
> a)))
>
> (define get-2 (x)
> (multiple-value-bind (a b c) (FOO x)
> (declare (ignore a c))
> b)))
>
> (define get-3 (x)
> (multiple-value-bind (a b c) (FOO x)
> (declare (ignore a b))
> c)))

How about nth-value? Or am I missing something.

(defun blub ()
(values 'a 'b 'c))

(nth-value 1 (blub)) => B

--
Matthew Carter (m@ahungry.com)
http://a...

Jim Newton

7/10/2015 8:02:00 AM

0

Hi Kaz, define vs defun? This might have been a case of my mac autocorrecting. I hate it when that happens.

Jim Newton

7/10/2015 8:07:00 AM

0

Sounds like nth-value is the winner.
Funny, when I looked in the hyper spec at multiple-value-bind, multiple-value-list, multiple-value-call etc, none of the See-Also sections mentioned nth-value.

Kenneth Tilton

7/10/2015 8:52:00 AM

0

On Thursday, July 9, 2015 at 11:18:24 AM UTC-4, Jim Newton wrote:
> For example if I have a function FOO which returns (values a b c)
> I'd like to write three accessor functions which return the a, b, and c respectively.
>
> I know several ways to write this, but i'm not sure which is the most efficient.
>
> (define get-1 (x)
> (multiple-value-bind (a b c) (FOO x)
> (declare (ignore b c))
> a)))
>
> (define get-2 (x)
> (multiple-value-bind (a b c) (FOO x)
> (declare (ignore a c))
> b)))
>
> (define get-3 (x)
> (multiple-value-bind (a b c) (FOO x)
> (declare (ignore a b))
> c)))

Wow, what a train wreck.

As Kaz pointed out: define?

As Matthew wondered: nth-value? Too easy?

My minor quibble is that you said you could think of several ways then typed in several functions all using the same way. Hunh?

Next up: efficiency? You see this as a performance issue? What are we worried about here, ram or cpu?

But here is the whopper and I am disappointed the yobbos of c.l.l seem to have gotten no better since Himself last dwelt here regularly and have obediently gone to work on giving you what you want instead of pointing out you do not want for what you asked: an accessor dedicated to a selected multiple value?! You must be abusing multiple values.

OK, the spec includes nth-value, but the spec includes everything. An application hiding nth-value behind an accessor is abuse, not completeness.

Himself just checked 100kloc of lisp and found one use of nth-value: postmodern's query returns the number of records affected as a second value and I am just verifying that, in my case, an update I know should update something updated something.

Multiple values are used to return "oh, by the way" information ancillary to the function result and it is inconceivable that dedicated accessors would be needed for ancillary results.

What are you up to over there?

-hk

Kenneth Tilton

7/10/2015 8:54:00 AM

0

On Friday, July 10, 2015 at 4:06:45 AM UTC-4, Jim Newton wrote:
> Sounds like nth-value is the winner.
> Funny, when I looked in the hyper spec at multiple-value-bind, multiple-value-list, multiple-value-call etc, none of the See-Also sections mentioned nth-value.

(apropos "-value" (find-package :common-lisp))

-hk

Jim Newton

7/10/2015 9:38:00 AM

0

Not sure as I agree with the idea that multiple values are used only for oh by the way situations. They are also used to avoid boxing. See for example: GET-DECODED-TIME.

Nicolas Neuss

7/10/2015 11:49:00 AM

0

Jim Newton <jimka.issy@gmail.com> writes:

> Not sure as I agree with the idea that multiple values are used only
> for oh by the way situations. They are also used to avoid boxing. See
> for example: GET-DECODED-TIME.

But then you'll usually need more than one value and m-v-b is your
friend.

Kaz Kylheku

7/10/2015 3:38:00 PM

0

On 2015-07-10, Jim Newton <jimka.issy@gmail.com> wrote:
> Hi Kaz, define vs defun? This might have been a case of my mac
> autocorrecting. I hate it when that happens.

Mac autocorrect was written by rabid Scheme advocates! Wow ...

Kaz Kylheku

7/10/2015 3:40:00 PM

0

On 2015-07-10, Jim Newton <jimka.issy@gmail.com> wrote:
> Not sure as I agree with the idea that multiple values are used only for oh
> by the way situations.

Such as, "Oh, by the way, here are four more pieces of Lisp you need to
implement your place-manipulating macro".

:)