How to write a list literal with conditional items? [duplicate]
This question already has an answer here:
How to evaluate the variables before adding them to a list?
2 answers
Given a list literal '(1 2 3)
is it possible to conditionally include parts of the list?
eg:
'(1 2 3
(when thing-is-true '(10 11 12)))
4 5 6)
Which would result in
'(1 2 3 4 5 6)
or '(1 2 3 10 11 12 4 5 6)
dependent on thing-is-true
.
Python for example supports:
[1, 2, 3, *([10, 11, 12] if thing_is_true else ), 4, 5, 6]
What is the most straightforward way to achieve this using a single expression, without defining a list variable and manipulating it?
elisp list
marked as duplicate by npostavs, DoMiNeLa10♦ 11 hours ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 1 more comment
This question already has an answer here:
How to evaluate the variables before adding them to a list?
2 answers
Given a list literal '(1 2 3)
is it possible to conditionally include parts of the list?
eg:
'(1 2 3
(when thing-is-true '(10 11 12)))
4 5 6)
Which would result in
'(1 2 3 4 5 6)
or '(1 2 3 10 11 12 4 5 6)
dependent on thing-is-true
.
Python for example supports:
[1, 2, 3, *([10, 11, 12] if thing_is_true else ), 4, 5, 6]
What is the most straightforward way to achieve this using a single expression, without defining a list variable and manipulating it?
elisp list
marked as duplicate by npostavs, DoMiNeLa10♦ 11 hours ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Don't think this is a duplicate because I'm not asking about adding items to a list. Rather, how to declare a list. Also notice the answer to this question is quite different.
– ideasman42
16 hours ago
Hmm, I guess there is minor difference in that the dup's answer doesn't mention,@
, only,
; but to my mind the answer in both cases is essentially: "you're looking for backquote".
– npostavs
10 hours ago
I was aware of the feature (that it could be useful in this case), I just didn't know how to use it because I'm not that experienced in elisp.
– ideasman42
9 hours ago
1
I'm not aware of what you're aware of, I just talk about what's written in the question and vote accordingly.
– npostavs
9 hours ago
Seems like a dup to me, regardless of a minor difference in what you're trying to do. The question is really about a misunderstanding, and the referenced duplicate Q&A covers that. (And the tags should includequote
and not includeelisp
, IMO.)
– Drew
6 hours ago
|
show 1 more comment
This question already has an answer here:
How to evaluate the variables before adding them to a list?
2 answers
Given a list literal '(1 2 3)
is it possible to conditionally include parts of the list?
eg:
'(1 2 3
(when thing-is-true '(10 11 12)))
4 5 6)
Which would result in
'(1 2 3 4 5 6)
or '(1 2 3 10 11 12 4 5 6)
dependent on thing-is-true
.
Python for example supports:
[1, 2, 3, *([10, 11, 12] if thing_is_true else ), 4, 5, 6]
What is the most straightforward way to achieve this using a single expression, without defining a list variable and manipulating it?
elisp list
This question already has an answer here:
How to evaluate the variables before adding them to a list?
2 answers
Given a list literal '(1 2 3)
is it possible to conditionally include parts of the list?
eg:
'(1 2 3
(when thing-is-true '(10 11 12)))
4 5 6)
Which would result in
'(1 2 3 4 5 6)
or '(1 2 3 10 11 12 4 5 6)
dependent on thing-is-true
.
Python for example supports:
[1, 2, 3, *([10, 11, 12] if thing_is_true else ), 4, 5, 6]
What is the most straightforward way to achieve this using a single expression, without defining a list variable and manipulating it?
This question already has an answer here:
How to evaluate the variables before adding them to a list?
2 answers
elisp list
elisp list
edited 16 hours ago
ideasman42
asked 19 hours ago
ideasman42ideasman42
1,555625
1,555625
marked as duplicate by npostavs, DoMiNeLa10♦ 11 hours ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by npostavs, DoMiNeLa10♦ 11 hours ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Don't think this is a duplicate because I'm not asking about adding items to a list. Rather, how to declare a list. Also notice the answer to this question is quite different.
– ideasman42
16 hours ago
Hmm, I guess there is minor difference in that the dup's answer doesn't mention,@
, only,
; but to my mind the answer in both cases is essentially: "you're looking for backquote".
– npostavs
10 hours ago
I was aware of the feature (that it could be useful in this case), I just didn't know how to use it because I'm not that experienced in elisp.
– ideasman42
9 hours ago
1
I'm not aware of what you're aware of, I just talk about what's written in the question and vote accordingly.
– npostavs
9 hours ago
Seems like a dup to me, regardless of a minor difference in what you're trying to do. The question is really about a misunderstanding, and the referenced duplicate Q&A covers that. (And the tags should includequote
and not includeelisp
, IMO.)
– Drew
6 hours ago
|
show 1 more comment
Don't think this is a duplicate because I'm not asking about adding items to a list. Rather, how to declare a list. Also notice the answer to this question is quite different.
– ideasman42
16 hours ago
Hmm, I guess there is minor difference in that the dup's answer doesn't mention,@
, only,
; but to my mind the answer in both cases is essentially: "you're looking for backquote".
– npostavs
10 hours ago
I was aware of the feature (that it could be useful in this case), I just didn't know how to use it because I'm not that experienced in elisp.
– ideasman42
9 hours ago
1
I'm not aware of what you're aware of, I just talk about what's written in the question and vote accordingly.
– npostavs
9 hours ago
Seems like a dup to me, regardless of a minor difference in what you're trying to do. The question is really about a misunderstanding, and the referenced duplicate Q&A covers that. (And the tags should includequote
and not includeelisp
, IMO.)
– Drew
6 hours ago
Don't think this is a duplicate because I'm not asking about adding items to a list. Rather, how to declare a list. Also notice the answer to this question is quite different.
– ideasman42
16 hours ago
Don't think this is a duplicate because I'm not asking about adding items to a list. Rather, how to declare a list. Also notice the answer to this question is quite different.
– ideasman42
16 hours ago
Hmm, I guess there is minor difference in that the dup's answer doesn't mention
,@
, only ,
; but to my mind the answer in both cases is essentially: "you're looking for backquote".– npostavs
10 hours ago
Hmm, I guess there is minor difference in that the dup's answer doesn't mention
,@
, only ,
; but to my mind the answer in both cases is essentially: "you're looking for backquote".– npostavs
10 hours ago
I was aware of the feature (that it could be useful in this case), I just didn't know how to use it because I'm not that experienced in elisp.
– ideasman42
9 hours ago
I was aware of the feature (that it could be useful in this case), I just didn't know how to use it because I'm not that experienced in elisp.
– ideasman42
9 hours ago
1
1
I'm not aware of what you're aware of, I just talk about what's written in the question and vote accordingly.
– npostavs
9 hours ago
I'm not aware of what you're aware of, I just talk about what's written in the question and vote accordingly.
– npostavs
9 hours ago
Seems like a dup to me, regardless of a minor difference in what you're trying to do. The question is really about a misunderstanding, and the referenced duplicate Q&A covers that. (And the tags should include
quote
and not include elisp
, IMO.)– Drew
6 hours ago
Seems like a dup to me, regardless of a minor difference in what you're trying to do. The question is really about a misunderstanding, and the referenced duplicate Q&A covers that. (And the tags should include
quote
and not include elisp
, IMO.)– Drew
6 hours ago
|
show 1 more comment
1 Answer
1
active
oldest
votes
You can use the macro (elisp) Backquote, e.g.,
`(1 2 3 ,@(when nil '(10 11 12)) 4 5 6)
;; => (1 2 3 4 5 6)
`(1 2 3 ,@(when t '(10 11 12)) 4 5 6)
;; => (1 2 3 10 11 12 4 5 6)
if you expand the above macros, they are simply using append
(append '(1 2 3) (when nil '(10 11 12)) '(4 5 6))
;; => (1 2 3 4 5 6)
(append '(1 2 3) (when t '(10 11 12)) '(4 5 6))
;; => (1 2 3 10 11 12 4 5 6)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the macro (elisp) Backquote, e.g.,
`(1 2 3 ,@(when nil '(10 11 12)) 4 5 6)
;; => (1 2 3 4 5 6)
`(1 2 3 ,@(when t '(10 11 12)) 4 5 6)
;; => (1 2 3 10 11 12 4 5 6)
if you expand the above macros, they are simply using append
(append '(1 2 3) (when nil '(10 11 12)) '(4 5 6))
;; => (1 2 3 4 5 6)
(append '(1 2 3) (when t '(10 11 12)) '(4 5 6))
;; => (1 2 3 10 11 12 4 5 6)
add a comment |
You can use the macro (elisp) Backquote, e.g.,
`(1 2 3 ,@(when nil '(10 11 12)) 4 5 6)
;; => (1 2 3 4 5 6)
`(1 2 3 ,@(when t '(10 11 12)) 4 5 6)
;; => (1 2 3 10 11 12 4 5 6)
if you expand the above macros, they are simply using append
(append '(1 2 3) (when nil '(10 11 12)) '(4 5 6))
;; => (1 2 3 4 5 6)
(append '(1 2 3) (when t '(10 11 12)) '(4 5 6))
;; => (1 2 3 10 11 12 4 5 6)
add a comment |
You can use the macro (elisp) Backquote, e.g.,
`(1 2 3 ,@(when nil '(10 11 12)) 4 5 6)
;; => (1 2 3 4 5 6)
`(1 2 3 ,@(when t '(10 11 12)) 4 5 6)
;; => (1 2 3 10 11 12 4 5 6)
if you expand the above macros, they are simply using append
(append '(1 2 3) (when nil '(10 11 12)) '(4 5 6))
;; => (1 2 3 4 5 6)
(append '(1 2 3) (when t '(10 11 12)) '(4 5 6))
;; => (1 2 3 10 11 12 4 5 6)
You can use the macro (elisp) Backquote, e.g.,
`(1 2 3 ,@(when nil '(10 11 12)) 4 5 6)
;; => (1 2 3 4 5 6)
`(1 2 3 ,@(when t '(10 11 12)) 4 5 6)
;; => (1 2 3 10 11 12 4 5 6)
if you expand the above macros, they are simply using append
(append '(1 2 3) (when nil '(10 11 12)) '(4 5 6))
;; => (1 2 3 4 5 6)
(append '(1 2 3) (when t '(10 11 12)) '(4 5 6))
;; => (1 2 3 10 11 12 4 5 6)
answered 17 hours ago
xuchunyangxuchunyang
8,8791926
8,8791926
add a comment |
add a comment |
Don't think this is a duplicate because I'm not asking about adding items to a list. Rather, how to declare a list. Also notice the answer to this question is quite different.
– ideasman42
16 hours ago
Hmm, I guess there is minor difference in that the dup's answer doesn't mention
,@
, only,
; but to my mind the answer in both cases is essentially: "you're looking for backquote".– npostavs
10 hours ago
I was aware of the feature (that it could be useful in this case), I just didn't know how to use it because I'm not that experienced in elisp.
– ideasman42
9 hours ago
1
I'm not aware of what you're aware of, I just talk about what's written in the question and vote accordingly.
– npostavs
9 hours ago
Seems like a dup to me, regardless of a minor difference in what you're trying to do. The question is really about a misunderstanding, and the referenced duplicate Q&A covers that. (And the tags should include
quote
and not includeelisp
, IMO.)– Drew
6 hours ago