6
$\begingroup$

This may be a simple question. Please close it if it is already answered.

I have a nested list

kr = {{35, 32, 27, 20}, {36, 33, 28, 21}, {37, 34, 29, 22}} 

I want to use a function flo[x_,y_,z_] on each of the lists after modifying it. So the Mapthread on a given list k0 (eg. {35, 32, 27, 20}) will look like this

MapThread[flo[L, ##] &, {Most@k0, Rest@k0}];

I want the output to look like this.

qfr = {{flo[L,35,32], flo[L,32,27],flo[L,27,20]},{flo[L,36,33], 
           flo[L,33,28],flo[L,28,21]},{flo[L,37,34], flo[L,34,29],flo[L,29,22]}}

I tried to do the following without much luck. what am I missing?

MapThread[flo[L, ##] &, {Most@#, Rest@#}] & kr
$\endgroup$
1
  • 3
    $\begingroup$ You're almost there... you just need to Map it on to kr like this: MapThread[flo[L, ##] &, {Most@#, Rest@#}] & /@ kr $\endgroup$
    – rm -rf
    Commented Mar 12, 2014 at 14:37

5 Answers 5

6
$\begingroup$

What about

flo[L, ##] & @@@ Partition[#, 2, 1] & /@ kr

Regarding your initial try, this would work:

MapThread[flo[L, ##] &, {Most@#, Rest@#}] & /@ kr

Without the /@ you are multiplying the expression MapThread[flo[L, ##] &, {Most@#, Rest@#}] & with every element in kr. The /@ on the opposite uses the expression as a function and applies it to every element in kr treated as a list.

$\endgroup$
3
$\begingroup$

I'd probably use BlockMap something like this:

BlockMap[flo[L, Sequence @@ Flatten[##]] &, kr, {1, 2}, 1]

Or you could be a bit more explicit about the arguments:

BlockMap[flo[L, #[[1, 1]], #[[1, 2]]] &, kr, {1, 2}, 1]

Or you could go point-free:

BlockMap[Apply[CurryApplied[flo, 3][L]]@*Flatten, kr, {1, 2}, 1]
$\endgroup$
2
$\begingroup$

Using Outer:

kr = {{35, 32, 27, 20}, {36, 33, 28, 21}, {37, 34, 29, 22}};
Outer[flo[L, Sequence @@ ##] &, Partition[#, 2, 1] & /@ kr, 2]

outer


Using BlockMap:

BlockMap[flo[L, Sequence @@ ##] &, #, 2, 1] & /@ kr

same result as above

$\endgroup$
2
$\begingroup$

There is also MovingMap

kr = {{35, 32, 27, 20}, {36, 33, 28, 21}, {37, 34, 29, 22}} 

MovingMap[flo[L, Sequence @@ #] &, #, 1] & /@ kr

(* {{flo[L, 35, 32], flo[L, 32, 27], flo[L, 27, 20]}, 
    {flo[L, 36, 33], flo[L, 33, 28], flo[L, 28, 21]},
    {flo[L, 37, 34], flo[L, 34, 29], flo[L, 29, 22]}} *)

In addition,

MovingMap[flo[L, Sequence @@ #Values] &, #, 1] & /@ kr == qfr
MovingMap[flo[L, Sequence @@ #BoundaryValues] &, #, 1] & /@ kr == qfr

(* True
   True *) 

For comparison

MovingMap[flo[L, Sequence @@ #BoundaryValues] &, #, 2] & /@ kr

(* {{flo[L, 35, 27], flo[L, 32, 20]}, 
    {flo[L, 36, 28], flo[L, 33, 21]}, 
    {flo[L, 37, 29], flo[L, 34, 22]}} *) 
$\endgroup$
2
$\begingroup$
kr = {{35, 32, 27, 20}, {36, 33, 28, 21}, {37, 34, 29, 22}};

Another way using Subsets and GatherBy:

f = flo[L, Sequence @@ #] &@*First /@ GatherBy[Subsets[#, {2}], First] &;

(f /@ kr) === (MovingMap[flo[L, Sequence @@ #] &, #, 1] & /@ kr)

(*True*)
$\endgroup$

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.