* Two questions about generalized variables
@ 2016-02-21 5:16 Eric Abrahamsen
2016-02-21 8:08 ` Eric Abrahamsen
2016-02-21 13:34 ` Michael Heerdegen
0 siblings, 2 replies; 8+ messages in thread
From: Eric Abrahamsen @ 2016-02-21 5:16 UTC (permalink / raw)
To: help-gnu-emacs
I'm starting to learn a bit more about generalized variables, the gv-*
stuff and macroexpand-*, what have you, and I have two
practical/learning questions I hope someone can help me with.
The first is working with plists. It can be a pain setting many plist
entries at once, particularly when there's lots of surrounding
conditional code. You either have to keep doing
"(setq plist-var (plist-put plist-var ..."
Or else use a backquote template, but that's not always practical.
It looks like plist-get doesn't return a setf-able place. I was thinking
of making a "with-plist-slots" macro, and looking at the `with-slots'
macro as inspiration, but `with-slots' also requires setf-able places.
Is there a way to define something to make that work?
The second question is setting hashtable entries. Right now I have code
like this:
(setf (gethash uuid my-hashtable)
(append (list (list 'thingone 'thingtwo))
(gethash uuid my-hashtable)))
I assume this is no more or less efficient than let-ting the gethash,
manipulating the value, then using puthash to put it back in. Now I've
written this:
(macroexp-let2 nil entry (gethash uuid my-hashtable)
(setf entry (append (list (list 'thingone 'thingtwo))
entry)))
This is a simplistic example, but -- is this actually going to be any
faster or more efficient than the first version? Does it only access the
hashtable once? Have I just been blinded by the shiny?
Any insights very welcome!
Thanks,
Eric
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Two questions about generalized variables
2016-02-21 5:16 Two questions about generalized variables Eric Abrahamsen
@ 2016-02-21 8:08 ` Eric Abrahamsen
2016-02-21 13:34 ` Michael Heerdegen
1 sibling, 0 replies; 8+ messages in thread
From: Eric Abrahamsen @ 2016-02-21 8:08 UTC (permalink / raw)
To: help-gnu-emacs
Eric Abrahamsen <eric@ericabrahamsen.net> writes:
[...]
> (macroexp-let2 nil entry (gethash uuid my-hashtable)
> (setf entry (append (list (list 'thingone 'thingtwo))
> entry)))
Bah, this was supposed to be:
(cl-symbol-macrolet ((entry (gethash uuid my-hashtable)))
(setf entry (append (list (list 'thingone 'thingtwo))
entry)))
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Two questions about generalized variables
2016-02-21 5:16 Two questions about generalized variables Eric Abrahamsen
2016-02-21 8:08 ` Eric Abrahamsen
@ 2016-02-21 13:34 ` Michael Heerdegen
2016-02-21 15:01 ` Eric Abrahamsen
1 sibling, 1 reply; 8+ messages in thread
From: Michael Heerdegen @ 2016-02-21 13:34 UTC (permalink / raw)
To: help-gnu-emacs
Eric Abrahamsen <eric@ericabrahamsen.net> writes:
> It looks like plist-get doesn't return a setf-able place.
But `symbol-plist' has a gv-expander and refers to an alist.
`alist-get' has a gv-expander, too. So i think you would end up with
something like
(setf (alist-get key (symbol-plist 'symbol)) value)
I think.
> The second question is setting hashtable entries. Right now I have code
> like this:
>
> (setf (gethash uuid my-hashtable)
> (append (list (list 'thingone 'thingtwo))
> (gethash uuid my-hashtable)))
Isn't that more or less `push'?
> I assume this is no more or less efficient than let-ting the gethash,
> manipulating the value, then using puthash to put it back in. Now I've
> written this:
> [corrected version from the second message]
> (cl-symbol-macrolet ((entry (gethash uuid my-hashtable)))
> (setf entry (append (list (list 'thingone 'thingtwo))
> entry)))
>
> This is a simplistic example, but -- is this actually going to be any
> faster or more efficient than the first version? Does it only access
> the hashtable once?
`push' with `gethash' and the above code expand more or less to the
same. And I think this must access the hash-table twice: once
`gethash', and once `puthash'.
Michael.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Two questions about generalized variables
2016-02-21 13:34 ` Michael Heerdegen
@ 2016-02-21 15:01 ` Eric Abrahamsen
2016-02-21 18:08 ` Michael Heerdegen
0 siblings, 1 reply; 8+ messages in thread
From: Eric Abrahamsen @ 2016-02-21 15:01 UTC (permalink / raw)
To: help-gnu-emacs
Michael Heerdegen <michael_heerdegen@web.de> writes:
> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
>> It looks like plist-get doesn't return a setf-able place.
>
> But `symbol-plist' has a gv-expander and refers to an alist.
> `alist-get' has a gv-expander, too. So i think you would end up with
> something like
>
> (setf (alist-get key (symbol-plist 'symbol)) value)
>
> I think.
I didn't mean symbol property lists! Just the plain old '(:key1 "value1"
:key2 "value2") kind. They're useful for a few different things -- in
this case, initializing class instances.
>> The second question is setting hashtable entries. Right now I have code
>> like this:
>>
>> (setf (gethash uuid my-hashtable)
>> (append (list (list 'thingone 'thingtwo))
>> (gethash uuid my-hashtable)))
>
> Isn't that more or less `push'?
I started off with `push', but the code in question doesn't know if the
key exists or not, and you can't push to nil. Once you've checked
whether the key has a value or not, you might as well be doing
gethash/puthash.
>> I assume this is no more or less efficient than let-ting the gethash,
>> manipulating the value, then using puthash to put it back in. Now I've
>> written this:
>
>> [corrected version from the second message]
>> (cl-symbol-macrolet ((entry (gethash uuid my-hashtable)))
>> (setf entry (append (list (list 'thingone 'thingtwo))
>> entry)))
>>
>> This is a simplistic example, but -- is this actually going to be any
>> faster or more efficient than the first version? Does it only access
>> the hashtable once?
>
> `push' with `gethash' and the above code expand more or less to the
> same. And I think this must access the hash-table twice: once
> `gethash', and once `puthash'.
Right, I would assume that "(push (list ...) (gethash uuid
my-hashtable))" is as efficient as you can get. But I don't think I can
use it.
But your response nudged me in the right direction -- of course I should
have simply expanded the macro to see what it does. And indeed the
`cl-symbol-macrolet' phrase expands to a `puthash' plus a `gethash'. So
that's my answer -- I guess it's just a syntactic convenience.
Thanks!
Eric
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Two questions about generalized variables
2016-02-21 15:01 ` Eric Abrahamsen
@ 2016-02-21 18:08 ` Michael Heerdegen
2016-02-23 3:19 ` Eric Abrahamsen
0 siblings, 1 reply; 8+ messages in thread
From: Michael Heerdegen @ 2016-02-21 18:08 UTC (permalink / raw)
To: help-gnu-emacs
Eric Abrahamsen <eric@ericabrahamsen.net> writes:
> I didn't mean symbol property lists! Just the plain old '(:key1 "value1"
> :key2 "value2") kind. They're useful for a few different things -- in
> this case, initializing class instances.
Oh, I see. Hmm, one could define a place expander quite similarly to
the one of `alist-get', I think, but it doesn't seem one to be
predefined, indeed.
> >> The second question is setting hashtable entries. Right now I have code
> >> like this:
> >>
> >> (setf (gethash uuid my-hashtable)
> >> (append (list (list 'thingone 'thingtwo))
> >> (gethash uuid my-hashtable)))
> >
> > Isn't that more or less `push'?
>
> I started off with `push', but the code in question doesn't know if the
> key exists or not, and you can't push to nil.
The gv setter is calculated using the (unevaluated) place expression, so
"pushing to nil" doesn't happen. See the gv-expander of `alist-get' for
example, which of course also works for nonexistent keys.
Regards,
Michael.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Two questions about generalized variables
2016-02-21 18:08 ` Michael Heerdegen
@ 2016-02-23 3:19 ` Eric Abrahamsen
2016-02-23 13:28 ` Michael Heerdegen
0 siblings, 1 reply; 8+ messages in thread
From: Eric Abrahamsen @ 2016-02-23 3:19 UTC (permalink / raw)
To: help-gnu-emacs
Michael Heerdegen <michael_heerdegen@web.de> writes:
> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
>> I didn't mean symbol property lists! Just the plain old '(:key1 "value1"
>> :key2 "value2") kind. They're useful for a few different things -- in
>> this case, initializing class instances.
>
> Oh, I see. Hmm, one could define a place expander quite similarly to
> the one of `alist-get', I think, but it doesn't seem one to be
> predefined, indeed.
Right, that was my question, I guess -- I would do this with
`gv-define-setter', right? Its docstring is a bit impenetrable, but if
that's the right place to start I can figure it out.
>> >> The second question is setting hashtable entries. Right now I have code
>> >> like this:
>> >>
>> >> (setf (gethash uuid my-hashtable)
>> >> (append (list (list 'thingone 'thingtwo))
>> >> (gethash uuid my-hashtable)))
>> >
>> > Isn't that more or less `push'?
>>
>> I started off with `push', but the code in question doesn't know if the
>> key exists or not, and you can't push to nil.
>
> The gv setter is calculated using the (unevaluated) place expression, so
> "pushing to nil" doesn't happen. See the gv-expander of `alist-get' for
> example, which of course also works for nonexistent keys.
Ah, of course! It's pushing to a place, not a value, that's the whole
point. Thank you for turning the light on there.
Last question: How do I actually see the gv-expander of `alist-get'?
Where is that?
Thanks again,
Eric
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Two questions about generalized variables
2016-02-23 3:19 ` Eric Abrahamsen
@ 2016-02-23 13:28 ` Michael Heerdegen
2016-02-24 2:03 ` Eric Abrahamsen
0 siblings, 1 reply; 8+ messages in thread
From: Michael Heerdegen @ 2016-02-23 13:28 UTC (permalink / raw)
To: help-gnu-emacs
Eric Abrahamsen <eric@ericabrahamsen.net> writes:
> > Oh, I see. Hmm, one could define a place expander quite similarly to
> > the one of `alist-get', I think, but it doesn't seem one to be
> > predefined, indeed.
>
> Right, that was my question, I guess -- I would do this with
> `gv-define-setter', right? Its docstring is a bit impenetrable, but if
> that's the right place to start I can figure it out.
I think even `gv-define-simple-setter' should be sufficient.
> Last question: How do I actually see the gv-expander of `alist-get'?
> Where is that?
Look into gv.el and search for `alist-get'.
That expander definition produces more efficient code than a separate
setter definition would - but one has to learn a bit about the concept
behind the library to understand how that works.
Regards,
Michael.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Two questions about generalized variables
2016-02-23 13:28 ` Michael Heerdegen
@ 2016-02-24 2:03 ` Eric Abrahamsen
0 siblings, 0 replies; 8+ messages in thread
From: Eric Abrahamsen @ 2016-02-24 2:03 UTC (permalink / raw)
To: help-gnu-emacs
Michael Heerdegen <michael_heerdegen@web.de> writes:
> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
>> > Oh, I see. Hmm, one could define a place expander quite similarly to
>> > the one of `alist-get', I think, but it doesn't seem one to be
>> > predefined, indeed.
>>
>> Right, that was my question, I guess -- I would do this with
>> `gv-define-setter', right? Its docstring is a bit impenetrable, but if
>> that's the right place to start I can figure it out.
>
> I think even `gv-define-simple-setter' should be sufficient.
>
>
>> Last question: How do I actually see the gv-expander of `alist-get'?
>> Where is that?
>
> Look into gv.el and search for `alist-get'.
>
> That expander definition produces more efficient code than a separate
> setter definition would - but one has to learn a bit about the concept
> behind the library to understand how that works.
Learning about the concepts was the whole idea!
Thanks again for your help,
Eric
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2016-02-24 2:03 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-02-21 5:16 Two questions about generalized variables Eric Abrahamsen
2016-02-21 8:08 ` Eric Abrahamsen
2016-02-21 13:34 ` Michael Heerdegen
2016-02-21 15:01 ` Eric Abrahamsen
2016-02-21 18:08 ` Michael Heerdegen
2016-02-23 3:19 ` Eric Abrahamsen
2016-02-23 13:28 ` Michael Heerdegen
2016-02-24 2:03 ` Eric Abrahamsen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).