4

If I have a list, (“apple” “pear” “grape” “apricot”) how do I create a new list with elements starting with the letter “a”? The new list will be: (“apple” “apricot”).

9
  • Are the words present together in the buffer, or are they scattered among other text? What do you mean by "copy" - copy all of the words together, to the kill-ring/clipboard, so you can paste them all together? The question really isn't clear (unless maybe someone knows just what that awk expression does).
    – Drew
    Sep 20, 2023 at 20:07
  • @Drew I added a sample list for the awk code. I was thinking a function that takes these words and returns only a list with words starting with the letter k.
    – zeynel
    Sep 20, 2023 at 20:35
  • 1
    This may help: emacs.stackexchange.com/help/how-to-ask
    – Drew
    Sep 21, 2023 at 0:23
  • 1
    @Drew Sorry, the question was not clear. I changed it. Does it make sense now?
    – zeynel
    Sep 21, 2023 at 3:52
  • 1
    @shynur Sorry about that. I changed the question. I hope it makes sense now.
    – zeynel
    Sep 21, 2023 at 3:54

2 Answers 2

9
(seq-filter (apply-partially #'string-prefix-p "a")
            '("apple" "pear" "grape" "apricot"))

If you have other similar questions, consult the seq library, which provides some functions to manipulate sequence (including list).

3
  • 2
    Thanks. This is what I was trying to do. Functions listed here gnu.org/software/emacs/manual/html_node/elisp/… is the seq librarary, right?
    – zeynel
    Sep 21, 2023 at 7:48
  • 2
    @zeynel Just type C-h f seq- <tab> in Emacs
    – shynur
    Sep 21, 2023 at 7:57
  • 1
    This is the answer, IMO. Corresponds exactly to the question posed: nothing missing or hidden.
    – Drew
    Sep 21, 2023 at 15:17
4

For this specific use-case, you can also reuse a builtin function related to text completion named all-completions:

(all-completions "a" '("apple" "pear" "grape" "apricot"))
;; => ("apple" "apricot")

For more details, see C-h f all-completions RET.

7
  • This is so simple. I accepted this one. Is @shynur's answer more general?
    – zeynel
    Sep 21, 2023 at 14:41
  • 2
    I think the accepted answer should mention completion-regexp-list, since it's not necessarily true that all-completions uses basic (i.e., plain prefix) completion. See Basic Completion.
    – Drew
    Sep 21, 2023 at 15:06
  • 2
    IOW, this answer really hides what's needed and how it might be solved by all-completions: filtering according to a prefix string.
    – Drew
    Sep 21, 2023 at 15:12
  • @zeynel Yes, @shynur's answer is more general since seq-filter accepts arbitrary predicates as its first argument. Furthermore, it works on any type that satisfies the predicate seqp, whereas all-completions works on specific collection types (each with slightly different semantics) and is not extensible.
    – Basil
    Sep 21, 2023 at 20:37
  • 1
    @Basil: You're right, I shouldn't have mentioned completion-regexp-list, any more than arg PREDICATE - those are additional, optional tests made after matching. And yes, all-completions does use only literal-prefix comparison to do the matching. I'll leave my mistaken comments, for info. And I've just filed Emacs bug #66147, to have the doc specify that matching for functions such as all-completions means literal-prefix string matching.
    – Drew
    Sep 21, 2023 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.