3
$\begingroup$
list = Partition[Range[20], 5, 5, 1, {}]
{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}}
pos = Transpose[{Through[First, Last] /@ list, Through[Last, First] /@ list}]
{{1, 5}, {6, 10}, {11, 15}, {16, 20}}

As you see, I've used Through twice. I think once should be enough, but I haven't been able to see how to do it with one Through. Any ideas?

$\endgroup$
12
  • 1
    $\begingroup$ Why not {First[#], Last[#]} & /@ list ? $\endgroup$ Jun 12, 2013 at 6:09
  • $\begingroup$ @b.gatessucks yes, me silly, haha, you can post that as one answer. $\endgroup$ Jun 12, 2013 at 6:10
  • 3
    $\begingroup$ To properly use Through[]: Through[{First, Last}[#]] & /@ list. Slot-free version: Composition[Through, {First, Last}] /@ list. $\endgroup$ Jun 12, 2013 at 6:19
  • 1
    $\begingroup$ I didn't ask you to delete the question; I asked you if you could write an answer to your question (answering your own question is acceptable in this site, in case that wasn't made clear to you.) You can stand to gain rep from upvotes to your answer, and we can critique your understanding of the matter at hand. $\endgroup$ Jun 12, 2013 at 6:46
  • 4
    $\begingroup$ Are you aware that you can do this with Part? list[[All,{1,-1}]] $\endgroup$ Jun 12, 2013 at 7:45

1 Answer 1

7
$\begingroup$

I have tried this

list = Partition[Range[20], 5, 5, 1, {}];
Through[{First, Last}] /@ list
(* {Through[{First,Last}][{1,2,3,4,5}],Through[{First,Last}][{6,7,8,9,10}],
    Through[{First,Last}][{11,12,13,14,15}],Through[{First,Last}][{16,17,18,19,20}]} *)

Let's see the example in help page of Through

Through[{f, g, h}[x]]   
(* {f[x],g[x],h[x]} *)

as @0x4A4D metioned in the comment. We need one argument for

Through[{First, Last}[#]] & /@ list
(* {{1,5},{6,10},{11,15},{16,20}} *)

this is one error usage

Through[{f, g, h}][x]
(* Through[{f,g,h}][x] *)

Through[{f, g, h}[#]] &[x]
(* {f[x],g[x],h[x]} *)
$\endgroup$
9
  • $\begingroup$ Well done. ${}$ $\endgroup$ Jun 12, 2013 at 7:21
  • 2
    $\begingroup$ Please don't make this an orgy of different formatting styles... $\endgroup$
    – rm -rf
    Jun 12, 2013 at 8:34
  • $\begingroup$ @rm-rf hi,Do you mean the <kbd>tag? I'm learning format in SE, Do you think the hint word Output is necessary? $\endgroup$ Jun 12, 2013 at 8:37
  • $\begingroup$ @HyperGroups nice answer (+1). The <kbd> tag stands for keyboard, which is usually used for indicating a key on the keyboard, e.g., <kbd>Ctrl</kbd>. So it may not be a best choice for "Output". In fact, on this site, people usually just put the output results in blockquotes without any "hint word". $\endgroup$
    – Silvia
    Jun 12, 2013 at 8:52
  • $\begingroup$ @Silvia Ok, I learned that. $\endgroup$ Jun 12, 2013 at 8:57

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.