4
$\begingroup$

This question is related to:

Composition of functions using slots

`Slot` (#) interfering with evaluation

Consider a toy example:

 list1 = { {a1, b1, c1, d1}, {a2, b2, c2, d2}, {a3, b3, c3, d3} };

I’d like to take elements from the list as follows example:

{#1, #2}@@@ list1

But I’d like to able to define #1, #2 programmatically.

I tried:

n=1;
{Slot@Evaluate[n], Slot@Evaluate[n + 1]} & @@@ list1

But it gives me an error…

Answers appreciated.Even better if the answer uses the # notation rather than the Slot function.

$\endgroup$

2 Answers 2

8
$\begingroup$

You can use With to insert values into held expressions:

With[{n=1,m=2},
  {Slot[n], Slot[m]} & @@@ list1
]

{{a1, b1}, {a2, b2}, {a3, b3}}

If you're so inclined, you can do a nested With:

With[{n = 1},
 With[{m = n + 1},
  {Slot[n], Slot[m]} & @@@ list1
 ]
]

Or with Leonid's exceedingly nice LetL:

LetL[{n = 1, m = n + 1},
  {Slot[n], Slot[m]} & @@@ list1
]
$\endgroup$
4
  • $\begingroup$ oh. I was too late! $\endgroup$
    – acl
    Jul 17, 2014 at 11:34
  • $\begingroup$ @acl Sorry, I cheated a bit by first posting half of my answer. Too bad you removed yours, as I wasn't aware that With[{n = n},...] was also allowed. $\endgroup$ Jul 17, 2014 at 11:41
  • $\begingroup$ +1. You can also use Where in V10 see my answer. $\endgroup$
    – RunnyKine
    Jul 17, 2014 at 11:43
  • $\begingroup$ That's a very useful trick. Why don't you add it to your answer? $\endgroup$
    – acl
    Jul 17, 2014 at 11:57
3
$\begingroup$

There's a nice function in the GeneralUtilities package included in Version 10 called Where. You can use this as:

Needs["GeneralUtilities`"]

Where[n = 1, m = n + 1, {Slot[n], Slot[m]} & @@@ list1]

Gives:

{{a1, b1}, {a2, b2}, {a3, b3}}
$\endgroup$
5
  • $\begingroup$ +1 Nice to hear indeed. I wonder if this is in fact based upon @LeonidShifrin's LetL. $\endgroup$ Jul 17, 2014 at 11:44
  • $\begingroup$ @TeakeNutma I wonder too. I just hope it is added as an internal function in a future release as I always use such functionality. $\endgroup$
    – RunnyKine
    Jul 17, 2014 at 11:46
  • $\begingroup$ Thanks… I haven’t upgraded to MMA 10… but this is great…. $\endgroup$
    – Pam
    Jul 17, 2014 at 11:52
  • $\begingroup$ @TeakeNutma AFAIK its not. LetL also supports shared local variables in definitions, as well as macro-expands at definition time, which makes it zero overhead w.r.t. manually-written nested With in function definitions (these are related). Again AFAIK, Where does not have this functionality. $\endgroup$ Jul 17, 2014 at 13:59
  • 1
    $\begingroup$ Where seems to have a definition intended to catch things like Where[{x,y}={2, 3}, f[x,y]] that they added last and is never tried. To fix it, use DownValues[Where] = RotateRight@DownValues@Where $\endgroup$
    – Rojo
    Jul 24, 2014 at 14:21

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.