2

I come across this problem in many different forms when doing arithmetic in MMA and have until now just suffered along doing it the long way – having given up trying to figure it out myself – but now I have to admit that's getting a bit repetitive.

How does one get two slots working in a pure function? I'm trying to work out the % differences in the following list but it just returns can not be filled errors.

data={16,24,36,54,81};
(100*#2)/#1 &/@ Partition[data,2,1]

sample image

BBirdsell
  • 1,196
  • 8
  • 21

1 Answers1

5

With p=Partition[data,2,1], you can do any of the following :

(100*#2)/#1 & @@@ p   (* parsed as Apply[ (100*#2)/#1 &, p, {1} ] *)
Apply[ (100*#2)/#1 & ] /@ p
(100*#[[2]])/(#[[1]]) & /@ p  (* Credit to Nasser in the comments *)

All evaluate to {150, 150, 150, 150}.


Your original try failed because Map (/@) passes only one argument to the function -- for example, #1 gets replaced by {16,24} for the first element of p. You need to take parts of #1 (which is equivalent to #), as in Nasser's example (the third example I included above), or you need to use Apply to pass 16 and 24 as the first and second arguments, as you seem to desire.

jjc385
  • 3,473
  • 1
  • 17
  • 29