7
$\begingroup$

I'm aware that this may be closed as offtopic but there are people around with "offtopic" knowledge :)

So, in List context we can: #[[n ;; ]] and #[[ ;; n]], but we can't do this in context of Sequence. Only ##n is available.

One may do

f[n_] := Unevaluated[##][[;; n]] &
f[2][1, 2, 3, 4]
Sequence[1, 2]

but it is not what I'm looking for.

It is very often when I would like to use it. Of course there are no problems with workarounds. I'm just curious.

p.s. Syntax is an issue for sure, I thought something like ###2 could stand for this but ### is automatically interpreted as ##1 #1.

$\endgroup$
3
  • $\begingroup$ Just a note, f[1][1] won't work. Even more minor: f[1][1, 2, 3] gives Sequence[1], whereas ##3 &[1, 2, 3] gives 3. Oh well it would be nice to see an answer/"response" :). $\endgroup$ – Jacob Akkerboom Mar 21 '14 at 12:24
  • $\begingroup$ @JacobAkkerboom good points. :) $\endgroup$ – Kuba Mar 21 '14 at 12:29
  • 2
    $\begingroup$ I think it could be connected to the reason there is HoldFirst and HoldRest, but no HoldMost and HoldLast. Obviously not quite the same, but perhaps related. $\endgroup$ – b3m2a1 Jan 11 '18 at 8:50
9
$\begingroup$

How about something like:

seqChopper[seq_Sequence, span_Span] := Sequence @@ ((List@seq)[[span]])

SetAttributes[seqChopper, SequenceHold]

seqChopper[Sequence[1, 2, 3, 4, 5], 2 ;;]
seqChopper[Sequence[1, 2, 3, 4, 5], 2 ;; -2]
seqChopper[Sequence[1, 2, 3, 4, 5], -2 ;; 1 ;; -2]

(*
Sequence[2, 3, 4, 5]
Sequence[2, 3, 4]
Sequence[4, 2]
*)
$\endgroup$
1
  • 2
    $\begingroup$ f[n_] := Unevaluated[##][[n]] & will do too. What I'm interested in is rather "why" there is nothing built-in than w workaround. +1 for SequenceHold I've not seen it for a while., $\endgroup$ – Kuba Mar 21 '14 at 10:16

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.