Several additional alternatives:
MapThread
:
MapThread[g, #] &@{Most@#, Rest@#} &@{r, s, t, u, v, w}
{g[r, s], g[s, t], g[t, u], g[u, v], g[v, w]}
or,
MapThread[g, #] &@Transpose@Partition[#, 2,1] &@{r, s, t, u, v, w}
{g[r, s], g[s, t], g[t, u], g[u, v], g[v, w]}
which allows more flexibility to specify the lists to thread over, like:
MapThread[g, #] &@Transpose@Partition[#, 3, 2, 1] &@{r, s, t, u, v, w}
{g[r, s, t], g[s, t, u], g[t, u, v], g[u, v, w]}
Inner
:
With last argument set to List
gives the same result as MapThread
:
Inner[g, Sequence @@ #, List] &@Transpose@Partition[#, 2, 1] &@{r, s,t, u, v, w}
{g[r, s], g[s, t], g[t, u], g[u, v], g[v, w]}
Thread
:
Thread[g[Most@#, Rest@#]] &@{r, s, t, u, v, w};
Thread[g[Sequence @@ #]] &@({Most@#, Rest@#} &@{r, s, t, u, v, w});
Thread[g[Sequence @@ #]] &@(Transpose@Partition[#, 2, 1] &@{r, s, t,u, v, w});
From docs on Thread
:
Functions with attribute Listable
are threaded automatically over lists.
Hence for Listable
functions, e.g., for h
in the following example:
SetAtrributes[h, Listable];
h[Sequence @@ #] &@(Transpose@Partition[#, 2, 1] &@{r, s, t, u, v, w})
gives the same result as does
Thread[h[Sequence @@ #]] &@(Transpose@Partition[#, 2, 1] &@{r, s, t, u, v, w}).
Also from docs:
MapThread
takes the function and its arguments separately.
Thread
evaluates the whole expression before threading.
Hence, using MapThread
is "safer" as pointed out in Mr.Wizard's comments.
Timings:
Test data:
tsts = Table[RandomInteger[1000, 1000000], {10}];
Results table (apologies for not figuring out how to apply Thread
in the following):
Grid[{{"method", "timing"},
{HoldForm[Thread[g[Sequence @@ #]] &@(Transpose@Partition[#, 2, 1] &)],
Table[AbsoluteTiming[ClearSystemCache[];
Thread[g[Sequence @@ #]] &@ (Transpose@Partition[#, 2, 1] &@ tsts[[i]])][[1]],
{i, 1, 10}] // Mean},
{HoldForm[Thread[g[Most@#, Rest@#]] &],
Table[AbsoluteTiming[ClearSystemCache[];
Thread[g[Most@#, Rest@#]] &@tsts[[i]]][[1]], {i, 1, 10}] // Mean},
{HoldForm[MapThread[g, Transpose@Partition[#, 2, 1]] &],
Table[AbsoluteTiming[ClearSystemCache[];
MapThread[g, Transpose@Partition[#, 2, 1]] &@tsts[[i]]][[1]], {i, 1, 10}] // Mean},
{HoldForm[MapThread[g, {Most@#, Rest@#}] &],
Table[AbsoluteTiming[ClearSystemCache[];
MapThread[g, {Most@#, Rest@#}] &@tsts[[i]]][[1]], {i, 1, 10}] // Mean},
{HoldForm[Inner[g, Sequence @@ #, List] &@{Most@#, Rest@#} &],
Table[AbsoluteTiming[ClearSystemCache[];
Inner[g,Sequence @@ #,List] &@{Most@#,Rest@#} &@tsts[[i]]][[1]], {i, 1, 10}] // Mean},
{HoldForm[Inner[g, Sequence @@ #, List] &@Transpose@Partition[#, 2, 1] &],
Table[AbsoluteTiming[ClearSystemCache[];
Inner[g, Sequence @@ #, List] &@Transpose@Partition[#, 2, 1] &@
tsts[[i]]][[1]], {i, 1, 10}] // Mean},
{HoldForm[Developer`PartitionMap[g @@ # &, tsts[[i]], 2, 1]],
Table[AbsoluteTiming[ClearSystemCache[];
Developer`PartitionMap[g @@ # &, tsts[[i]], 2, 1]][[1]], {i, 1, 10}] // Mean},
{HoldForm[g @@@ Partition[tsts[[i]], 2, 1]],
Table[AbsoluteTiming[ClearSystemCache[];
g @@@ Partition[tsts[[i]], 2, 1]][[1]], {i, 1, 10}] // Mean},
{HoldForm[ g @@@ Most[{tsts[[i]], RotateLeft@tsts[[i]]}\[Transpose]]],
Table[AbsoluteTiming[ClearSystemCache[];
g @@@ Most[{tsts[[i]], RotateLeft@tsts[[i]]}\[Transpose]]][[1]], {i, 1, 10}] // Mean},
{HoldForm[Fold[(Sow[g[#1, #2]]; #2) &, First@#, Rest@#] &@tsts[[i]]; //
Reap // Last],
Table[AbsoluteTiming[ClearSystemCache[];
Fold[(Sow[g[#1, #2]]; #2) &, First@#, Rest@#] &@tsts[[i]]; //
Reap // Last][[1]], {i, 1, 10}] // Mean},
{HoldForm[g[#[[1]], #[[2]]] & /@ Partition[tsts[[i]], 2, 1]],
Table[AbsoluteTiming[ClearSystemCache[];
g[#[[1]], #[[2]]] & /@ Partition[tsts[[i]], 2, 1]][[1]], {i, 1, 10}] // Mean}},
Frame -> All]
Timing results: