1
$\begingroup$

How can I get an element of a sequence?
For example Sequence[a,b,c,d]
How can get c?

$\endgroup$
5
  • 3
    $\begingroup$ Wrap it in a list and then take parts of that list? $\endgroup$
    – J. M.'s torpor
    Feb 4 at 0:36
  • $\begingroup$ @J.M. yes, I did that before but was wondering if that is how people normally do it. $\endgroup$
    – emnha
    Feb 4 at 0:47
  • 6
    $\begingroup$ Part is more natural. But you can also use Slot: i.e., #3 &@Sequence[a, b, c, d]. $\endgroup$
    – kglr
    Feb 4 at 1:05
  • $\begingroup$ With Part are you talking about the method that J. M. mentioned above? @kglr $\endgroup$
    – emnha
    Feb 5 at 10:14
  • $\begingroup$ that's right... $\endgroup$
    – kglr
    Feb 5 at 12:34
3
$\begingroup$
seq = Sequence[a, b, c, d]
Sequence[a, b, c, d]

You can use a composition of Part ([[...]]) and List as suggested by J.M.'sennui:

{seq}[[3]]
c
(Part[#, 3] & @* List)[seq]
c

Alternatively, you can use Slot[3] (that is, #3):

#3 & @ seq
c
Slot[3] &[seq]
c
$\endgroup$
2
  • $\begingroup$ Is there any advantage of writing (Part[#, 3] & @* List)[seq] instead of {seq}[[3]]? $\endgroup$
    – emnha
    Feb 8 at 7:37
  • 1
    $\begingroup$ @anhnha, can't think of any. $\endgroup$
    – kglr
    Feb 8 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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