* Use of an associated list with completing-read
@ 2024-04-18 23:48 Heime
2024-04-19 2:14 ` [External] : " Drew Adams
2024-04-23 4:44 ` Jean Louis
0 siblings, 2 replies; 13+ messages in thread
From: Heime @ 2024-04-18 23:48 UTC (permalink / raw)
To: Heime via Users list for the GNU Emacs text editor
Haw can I have an interactive function that displays a list of elements from an
associated list using completing-read ?
^ permalink raw reply [flat|nested] 13+ messages in thread
* RE: [External] : Use of an associated list with completing-read
2024-04-18 23:48 Use of an associated list with completing-read Heime
@ 2024-04-19 2:14 ` Drew Adams
2024-04-19 2:59 ` Heime
2024-04-23 4:44 ` Jean Louis
1 sibling, 1 reply; 13+ messages in thread
From: Drew Adams @ 2024-04-19 2:14 UTC (permalink / raw)
To: Heime, Heime via Users list for the GNU Emacs text editor
> Haw can I have an interactive function that
> displays a list of elements from an associated
> list using completing-read ?
The elements of an alist are conses. Assuming
that you really meant that you want to see and
choose among the elements, then use this:
(completing-read
"Choose an alist element: "
(mapcar #'prin1-to-string my-alist)
nil
t)
That "displays" elements that match your
input. Is that what you meant by "displays
a list of elements"?
It returns a string such as "(a . 42)" for
the alist element (a . 42). Then you can
use (read "(a . 42)") to get a new cons
that's `equal' to the cons in your alist.
___
But if you don't really want to see the
elements of the alist as completions, but
instead you want to see their keys, then
just do this:
(completing-read
"Choose an alist key: "
my-alist
nil
t)
If the alist keys are symbols then apply
`intern' to the chosen string, to get
the symbol key. Then use this to get
the element with that key (the exact
same cons, i.e., `eq', not just `equal):
(car (assq THE-SYMBOL my-alist)
I had to do a lot of guessing as to what
you're really requesting. As usual, you
don't make clear what you're after. And
you don't show any code that you've tried
so far.
^ permalink raw reply [flat|nested] 13+ messages in thread
* RE: [External] : Use of an associated list with completing-read
2024-04-19 2:14 ` [External] : " Drew Adams
@ 2024-04-19 2:59 ` Heime
2024-04-19 3:10 ` Heime
0 siblings, 1 reply; 13+ messages in thread
From: Heime @ 2024-04-19 2:59 UTC (permalink / raw)
To: Drew Adams; +Cc: Heime via Users list for the GNU Emacs text editor
On Friday, April 19th, 2024 at 2:14 PM, Drew Adams <drew.adams@oracle.com> wrote:
> > Haw can I have an interactive function that
> > displays a list of elements from an associated
> > list using completing-read ?
>
>
> The elements of an alist are conses. Assuming
> that you really meant that you want to see and
> choose among the elements, then use this:
>
> (completing-read
> "Choose an alist element: "
> (mapcar #'prin1-to-string my-alist)
> nil
> t)
>
> That "displays" elements that match your
> input. Is that what you meant by "displays
> a list of elements"?
>
> It returns a string such as "(a . 42)" for
> the alist element (a . 42). Then you can
> use (read "(a . 42)") to get a new cons
> that's `equal' to the cons in your alist. ___
> But if you don't really want to see the elements of the alist as completions,
> but instead you want to see their keys, then just do this:
> (completing-read "Choose an alist key: " my-alist nil t)
Yes, I wanted to see the keys as completing entries. Right, looking
at the documentation COLLECTION can be an alist and will print the keys.
I had the cons cells the other way round, with the actual key as the cdr,
making completing-read display the wrong entry.
> If the alist keys are symbols then apply` intern' to the chosen string, to get
> the symbol key. Then use this to get the element with that key (the exact
> same cons, i.e., `eq', not just` equal):
>
> (car (assq THE-SYMBOL my-alist)
>
> I had to do a lot of guessing as to what
> you're really requesting. As usual, you
> don't make clear what you're after. And
> you don't show any code that you've tried
> so far.
^ permalink raw reply [flat|nested] 13+ messages in thread
* RE: [External] : Use of an associated list with completing-read
2024-04-19 2:59 ` Heime
@ 2024-04-19 3:10 ` Heime
2024-04-19 15:10 ` Drew Adams
0 siblings, 1 reply; 13+ messages in thread
From: Heime @ 2024-04-19 3:10 UTC (permalink / raw)
To: Heime; +Cc: Drew Adams, Heime via Users list for the GNU Emacs text editor
On Friday, April 19th, 2024 at 2:59 PM, Heime <heimeborgia@protonmail.com> wrote:
> On Friday, April 19th, 2024 at 2:14 PM, Drew Adams drew.adams@oracle.com wrote:
>
> > > Haw can I have an interactive function that
> > > displays a list of elements from an associated
> > > list using completing-read ?
> >
> > The elements of an alist are conses. Assuming
> > that you really meant that you want to see and
> > choose among the elements, then use this:
> >
> > (completing-read
> > "Choose an alist element: "
> > (mapcar #'prin1-to-string my-alist)
> > nil
> > t)
> >
> > That "displays" elements that match your
> > input. Is that what you meant by "displays
> > a list of elements"?
> >
> > It returns a string such as "(a . 42)" for
> > the alist element (a . 42). Then you can
> > use (read "(a . 42)") to get a new cons
> > that's `equal' to the cons in your alist. ___
>
> > But if you don't really want to see the elements of the alist as completions,
> > but instead you want to see their keys, then just do this:
>
> > (completing-read "Choose an alist key: " my-alist nil t)
>
>
> Yes, I wanted to see the keys as completing entries. Right, looking
> at the documentation COLLECTION can be an alist and will print the keys.
>
> I had the cons cells the other way round, with the actual key as the cdr,
> making completing-read display the wrong entry.
>
> > If the alist keys are symbols then apply`intern' to the chosen string, to get the symbol key. Then use this to get the element with that key (the exact same cons, i.e.,`eq', not just` equal):
> >
> > (car (assq THE-SYMBOL my-alist)
> >
> > I had to do a lot of guessing as to what
> > you're really requesting. As usual, you
> > don't make clear what you're after. And
> > you don't show any code that you've tried
> > so far.
How can I get the value associated with a key. The following does not get me the
value associated with my-key.
(let ( (lnum (cdr (rassoc my-key my-alist))))
(when lnum (goto-line lnum))))) )
^ permalink raw reply [flat|nested] 13+ messages in thread
* RE: [External] : Use of an associated list with completing-read
2024-04-19 3:10 ` Heime
@ 2024-04-19 15:10 ` Drew Adams
2024-04-19 19:09 ` Heime
0 siblings, 1 reply; 13+ messages in thread
From: Drew Adams @ 2024-04-19 15:10 UTC (permalink / raw)
To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor
> How can I get the value associated with a key.
> The following does not get me the value
> associated with my-key.
>
> (let ( (lnum (cdr (rassoc my-key my-alist))))
> (when lnum (goto-line lnum))))) )
You don't show your key or alist.
And you include the irrelevant code `goto-line'.
If `my-key' is `((4 score (and 7) years) ago)'
and `my-alist' is `((humpty . dumpty)
(sat . on-a-wall))', then it's no wonder you're
not getting the value associated with `my-key',
because there isn't any.
^ permalink raw reply [flat|nested] 13+ messages in thread
* RE: [External] : Use of an associated list with completing-read
2024-04-19 15:10 ` Drew Adams
@ 2024-04-19 19:09 ` Heime
2024-04-19 23:32 ` Stefan Monnier via Users list for the GNU Emacs text editor
0 siblings, 1 reply; 13+ messages in thread
From: Heime @ 2024-04-19 19:09 UTC (permalink / raw)
To: Drew Adams; +Cc: Heime via Users list for the GNU Emacs text editor
On Saturday, April 20th, 2024 at 3:10 AM, Drew Adams <drew.adams@oracle.com> wrote:
> > How can I get the value associated with a key.
> > The following does not get me the value
> > associated with my-key.
> >
> > (let ( (lnum (cdr (rassoc my-key my-alist))))
> > (when lnum (goto-line lnum))))) )
>
>
> You don't show your key or alist.
> And you include the irrelevant code `goto-line'. If` my-key' is `((4 score (and 7) years) ago)' and` my-alist' is `((humpty . dumpty) (sat . on-a-wall))', then it's no wonder you're not getting the value associated with` my-key',
> because there isn't any.
Originally I had the associated list made by
;; First Version
(setq-local tema-lugar
(append tema--lugar (list (cons lnum strg))))
But the key I was looking for is in the cdr of each element, a string
named strg.
Then changed so that the key is the the car, thusly I have
;; Second Version
(setq-local tema-lugar
(append tema--lugar (list (cons strg lnum))))
But rassoc was taking the cdr as the key, corresponding to the first
version.
Example: (("Preamble" . 82) ("Conditions" . 167))
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [External] : Use of an associated list with completing-read
2024-04-19 19:09 ` Heime
@ 2024-04-19 23:32 ` Stefan Monnier via Users list for the GNU Emacs text editor
2024-04-20 1:13 ` Drew Adams
0 siblings, 1 reply; 13+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2024-04-19 23:32 UTC (permalink / raw)
To: help-gnu-emacs
> Originally I had the associated list made by
"alist" is an abbreviation for "association list" rather than "associated
list".
> ;; First Version
> (setq-local tema-lugar
> (append tema--lugar (list (cons lnum strg))))
This is the classic recipe for poor scaling performance since the above
operation takes time proportional to the length of the list, so if you
execute this N times (in a loop), the loop builds a list of length N but
takes time N² to do it. When N is small, noone notices, and as it gets
large the performance starts to suck.
Stefan
^ permalink raw reply [flat|nested] 13+ messages in thread
* RE: [External] : Use of an associated list with completing-read
2024-04-19 23:32 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2024-04-20 1:13 ` Drew Adams
2024-04-20 1:52 ` Stefan Monnier via Users list for the GNU Emacs text editor
0 siblings, 1 reply; 13+ messages in thread
From: Drew Adams @ 2024-04-20 1:13 UTC (permalink / raw)
To: Stefan Monnier, 'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'
[-- Attachment #1: Type: text/plain, Size: 1059 bytes --]
> > (setq-local tema-lugar
> > (append tema--lugar (list (cons lnum strg))))
>
> This is the classic recipe for poor scaling performance since the above
> operation takes time proportional to the length of the list, so if you
> execute this N times (in a loop), the loop builds a list of length N
> but takes time N² to do it. When N is small, noone notices, and as it gets
> large the performance starts to suck.
Heime:
One programming cliche for this is to
(1) start with a list that you create
(e.g., a let-binding to nil), so you
don't modify any existing list that
you might not want to mess with, (2)
use `nconc' instead of `append', to
append quickly (the _list structure_
is modified - destructive), (3) being
sure to set your list variable to the
result of each modification.
An even more common cliche for doing
the same thing is to do #1, then (2)
cons instead of append, and (3) when
finished adding list elements, do an
`nreverse' of the list you created.
That too is a destructive operation.
[-- Attachment #2: winmail.dat --]
[-- Type: application/ms-tnef, Size: 15688 bytes --]
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [External] : Use of an associated list with completing-read
2024-04-20 1:13 ` Drew Adams
@ 2024-04-20 1:52 ` Stefan Monnier via Users list for the GNU Emacs text editor
2024-04-20 1:59 ` Emanuel Berg
2024-04-20 6:14 ` Heime
0 siblings, 2 replies; 13+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2024-04-20 1:52 UTC (permalink / raw)
To: help-gnu-emacs
>> > (setq-local tema-lugar
>> > (append tema--lugar (list (cons lnum strg))))
>>
>> This is the classic recipe for poor scaling performance since the above
>> operation takes time proportional to the length of the list, so if you
>> execute this N times (in a loop), the loop builds a list of length N
>> but takes time N² to do it. When N is small, noone notices, and as it gets
>> large the performance starts to suck.
>
> Heime:
>
> One programming cliche for this is to
> (1) start with a list that you create
> (e.g., a let-binding to nil), so you
> don't modify any existing list that
> you might not want to mess with, (2)
> use `nconc' instead of `append', to
> append quickly (the _list structure_
> is modified - destructive), (3) being
> sure to set your list variable to the
> result of each modification.
While this is slightly better because it avoid the O(N²) memory
allocation, it's still O(N²) operations.
> An even more common cliche for doing
> the same thing is to do #1, then (2)
> cons instead of append, and (3) when
> finished adding list elements, do an
> `nreverse' of the list you created.
> That too is a destructive operation.
That's the usual solution, with the desired linear
(i.e. optimal) complexity, indeed.
Stefan
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [External] : Use of an associated list with completing-read
2024-04-20 1:52 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2024-04-20 1:59 ` Emanuel Berg
2024-04-20 6:14 ` Heime
1 sibling, 0 replies; 13+ messages in thread
From: Emanuel Berg @ 2024-04-20 1:59 UTC (permalink / raw)
To: help-gnu-emacs
Stefan Monnier via Users list for the GNU Emacs text editor wrote:
>> An even more common cliche for doing the same thing is to
>> do #1, then (2) cons instead of append, and (3) when
>> finished adding list elements, do an `nreverse' of the list
>> you created. That too is a destructive operation.
>
> That's the usual solution, with the desired linear (i.e.
> optimal) complexity, indeed.
AKA O(N) which is linear, or proportional to the problem
size N.
Even better is constant complexity, O(1), where it does not
matter how big the problem is, since that, N, isn't even in
the equation, as we saw.
Probably not possible in a lot of cases, including this one.
--
underground experts united
https://dataswamp.org/~incal
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [External] : Use of an associated list with completing-read
2024-04-20 1:52 ` Stefan Monnier via Users list for the GNU Emacs text editor
2024-04-20 1:59 ` Emanuel Berg
@ 2024-04-20 6:14 ` Heime
2024-04-20 6:37 ` Heime
1 sibling, 1 reply; 13+ messages in thread
From: Heime @ 2024-04-20 6:14 UTC (permalink / raw)
To: Stefan Monnier; +Cc: help-gnu-emacs
On Saturday, April 20th, 2024 at 1:52 PM, Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
> > > > (setq-local tema-lugar
> > > > (append tema--lugar (list (cons lnum strg))))
> > >
> > > This is the classic recipe for poor scaling performance since the above
> > > operation takes time proportional to the length of the list, so if you
> > > execute this N times (in a loop), the loop builds a list of length N
> > > but takes time N² to do it. When N is small, noone notices, and as it gets
> > > large the performance starts to suck.
> >
> > Heime:
> >
> > One programming cliche for this is to
> > (1) start with a list that you create
> > (e.g., a let-binding to nil), so you
> > don't modify any existing list that
> > you might not want to mess with, (2)
> > use `nconc' instead of` append', to
> > append quickly (the list structure
> > is modified - destructive), (3) being
> > sure to set your list variable to the
> > result of each modification.
>
>
> While this is slightly better because it avoid the O(N²) memory
> allocation, it's still O(N²) operations.
>
> > An even more common cliche for doing
> > the same thing is to do #1, then (2)
> > cons instead of append, and (3) when
> > finished adding list elements, do an
> > `nreverse' of the list you created.
> > That too is a destructive operation.
>
>
> That's the usual solution, with the desired linear
> (i.e. optimal) complexity, indeed. - Stefan
How would the implementation be like exactly ?
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [External] : Use of an associated list with completing-read
2024-04-20 6:14 ` Heime
@ 2024-04-20 6:37 ` Heime
0 siblings, 0 replies; 13+ messages in thread
From: Heime @ 2024-04-20 6:37 UTC (permalink / raw)
To: Heime; +Cc: Stefan Monnier, help-gnu-emacs
On Saturday, April 20th, 2024 at 6:14 PM, Heime <heimeborgia@protonmail.com> wrote:
> On Saturday, April 20th, 2024 at 1:52 PM, Stefan Monnier via Users list for the GNU Emacs text editor help-gnu-emacs@gnu.org wrote:
>
> > > > > (setq-local tema-lugar
> > > > > (append tema--lugar (list (cons lnum strg))))
> > > >
> > > > This is the classic recipe for poor scaling performance since the above
> > > > operation takes time proportional to the length of the list, so if you
> > > > execute this N times (in a loop), the loop builds a list of length N
> > > > but takes time N² to do it. When N is small, noone notices, and as it gets
> > > > large the performance starts to suck.
> > >
> > > Heime:
> > >
> > > One programming cliche for this is to
> > > (1) start with a list that you create
> > > (e.g., a let-binding to nil), so you
> > > don't modify any existing list that
> > > you might not want to mess with, (2)
> > > use `nconc' instead of` append', to
> > > append quickly (the list structure
> > > is modified - destructive), (3) being
> > > sure to set your list variable to the
> > > result of each modification.
> >
> > While this is slightly better because it avoid the O(N²) memory
> > allocation, it's still O(N²) operations.
> >
> > > An even more common cliche for doing
> > > the same thing is to do #1, then (2)
> > > cons instead of append, and (3) when
> > > finished adding list elements, do an
> > > `nreverse' of the list you created.
> > > That too is a destructive operation.
> >
> > That's the usual solution, with the desired linear
> > (i.e. optimal) complexity, indeed. - Stefan
>
>
> How would the implementation be like exactly ?
Would a solution be like this ?
(push (cons kfrz lnum) tema-lugar)
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Use of an associated list with completing-read
2024-04-18 23:48 Use of an associated list with completing-read Heime
2024-04-19 2:14 ` [External] : " Drew Adams
@ 2024-04-23 4:44 ` Jean Louis
1 sibling, 0 replies; 13+ messages in thread
From: Jean Louis @ 2024-04-23 4:44 UTC (permalink / raw)
To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor
* Heime <heimeborgia@protonmail.com> [2024-04-19 02:50]:
> Haw can I have an interactive function that displays a list of elements from an
> associated list using completing-read ?
In my work on RCD Notes & Hyperscope for GNU Emacs, The Dynamic
Knowledge Repository:
https://gnu.support/gnu-emacs/rcd-notes-for-gnu-emacs/index.html I am
using daily many many times completing-read that is offering me
various choices.
In general, each choice has unique ID assigned, as that is how the database works.
I have faced similar problem as described as I wanted to choose items
by the name and get that unique ID as the result.
But guess what, people may be named same, and items could have same
name, with the different ID, as items could be in different contexts,
or belonging to different sets, but having same name.
The way how I have solved it, forever, is that items appear with the
unique ID in the same line, and their selection is based on combo view
in the database.
Combo view could combine the name with some other parameters, such as:
First name, last name, email address -- as this could make the name
enough unique and distinguishable rather than only choosing by first
name, last name, as there are many same entries in my database.
Here are some not so used functions which may demonstrate closer what I mean:
(defun cf-search-by-sql (sql prompt)
"Finds items by SQL that shall contain at first place the ID of the item."
(let* ((collection (rcd-sql-list sql cf-db))
(choice (completing-read prompt collection))
(id (string-cut-id choice)))
id))
(cf-search-by-sql "SELECT hyobjectsubtypes_id || ' ' || hyobjectsubtypes_name FROM hyobjectsubtypes" "Find Hyperdocuments by subtypes: ")
The above function gives me results such as below:
128 possible completions:
1 Default 10 Call 100 Deja-Vu 101 Minutes
102 Technical 103 Police 104 Daily Routine 105 Presentation
106 DISEASE 107 Project 108 Sales Flow 109 Sales stage
11 Pay 110 Recommendation 111 Policy 112 Financial Planning Program No. 1
Choosing right one could involve using joker as prefix and object's name:
*Recom TAB -- would expand into 110 Recommendation
and function `string-cut-id' would then give me as result 110.
(defun string-cut-id (s)
"Return the ID number as integer from beginning of a string S.
A space must follow the ID number, without many checks.
When string S is `123 Hello there' this function will return 123."
(let* ((until (string-match " " s)))
(if until
(string-to-number (substring s nil until))
nil)))
Today, many of those functions changed, and I am not using the ID number as prefix, rather as suffix, and to make it more distinguishable, I am enclosing it in square brackets:
(defun rcd-completing-read-sql-hash (prompt sql pg &optional history initial-input not-require-match auto-initial-input)
"Complete selection by using SQL.
First column shall be unique id, followed by text
representation. Example SQL query:
SELECT people_id, people_firstname || ' ' || people_lastname FROM people
PG is database handle. HISTORY is supported with INITIAL-INPUT
Argument PROMPT will be displayed to user."
(let* ((gc-cons-threshold 5000000)
(hash (rcd-sql-hash-with-key sql pg))
(completion-ignore-case t)
(require-match (if not-require-match nil t))
(history (or history (rcd-ask-history-variable prompt)))
(initial-input (or initial-input (when auto-initial-input (car (symbol-value history)))))
(choice (completing-read prompt hash nil require-match initial-input history))
(choice (string-trim choice))
(id (gethash choice hash)))
(if id id
(if not-require-match
choice))))
The above function simplifies it for me so that I can make SQL queries in such way that first item is the ID followed by string representing the item.
The SQL statement "SELECT hyobjectsubtypes_id, hyobjectsubtypes_name FROM hyobjects" would then result with following choices in completing-read:
(rcd-completing-read-sql-hash "Select subtype: " "SELECT hyobjectsubtypes_id, hyobjectsubtypes_name FROM hyobjectsubtypes" cf-db)
128 possible completions:
Acknowledgment [86] Administrative Scale [95] Agreement [17]
Appointment [27] Archive [99] Article [2]
Attachment [9] Battle Plan [90] Book [6]
Borrowed Item [38] Business Plan [80] Business Profile [13]
Even if there would be 2 same names, there would be distinguishable
unique ID, for example, there could be "Business Plan [80]" and
"Business Plan [81]" to choose from.
Following function is internally preparing the hash for
completing-read function. It makes hash names unique, and the ID is
obtained by using (gethash choice hash) in the
rcd-completing-read-sql-hash function.
(defun rcd-sql-hash-with-key (sql pg)
"Return hash with key TEXT [ID] and value ID from SQL result.
Output is in the form ID, TEXT
Argument PG is database handle."
(let ((hash (make-hash-table :test 'equal))
(res (rcd-sql sql pg)))
(while res
(let ((item (pop res)))
(cond ((eq (type-of item) 'vector) (puthash (format "%s [%s]" (elt item 1) (elt item 0)) (elt item 0) hash)))))
hash))
While this may all sound complicated the point is that one could embed
the "key" in the choice itself, and such need not be a number, it
could be string; additionally using hash with completing-read is
additional solution to general problems.
--
Jean
Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns
In support of Richard M. Stallman
https://stallmansupport.org/
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2024-04-23 4:44 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-18 23:48 Use of an associated list with completing-read Heime
2024-04-19 2:14 ` [External] : " Drew Adams
2024-04-19 2:59 ` Heime
2024-04-19 3:10 ` Heime
2024-04-19 15:10 ` Drew Adams
2024-04-19 19:09 ` Heime
2024-04-19 23:32 ` Stefan Monnier via Users list for the GNU Emacs text editor
2024-04-20 1:13 ` Drew Adams
2024-04-20 1:52 ` Stefan Monnier via Users list for the GNU Emacs text editor
2024-04-20 1:59 ` Emanuel Berg
2024-04-20 6:14 ` Heime
2024-04-20 6:37 ` Heime
2024-04-23 4:44 ` Jean Louis
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).