MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Mapping to Create Nested Loops

  • To: mathgroup at smc.vnet.net
  • Subject: [mg119426] Re: Mapping to Create Nested Loops
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Thu, 2 Jun 2011 19:11:59 -0400 (EDT)

As long as the inner pure function is used in a criteria or pattern that is not
evaluated until the outer pure function is mapped, it seems to work.

data = RandomInteger[9, {3, 5, 5}]

{{{4, 0, 5, 0, 5}, {0, 1, 1, 2, 8}, {0, 1, 8, 1, 5}, {8, 3, 2, 9, 1}, {9, 1,
   0, 1, 3}}, {{8, 4, 3, 5, 5}, {0, 6, 1, 0, 5}, {1, 1, 7, 8, 0}, {8, 3, 9, 4,
    7}, {0, 5, 4, 7, 8}}, {{1, 9, 0, 4, 8}, {8, 2, 9, 7, 8}, {7, 9, 9, 5,
   8}, {8, 4, 3, 0, 3}, {0, 7, 5, 0, 0}}}

Select[#, OddQ[#[[3]]] &] & /@ data

{{{4, 0, 5, 0, 5}, {0, 1, 1, 2, 8}}, {{8, 4, 3, 5, 5}, {0, 6, 1, 0, 5}, {1, 1,
    7, 8, 0}, {8, 3, 9, 4, 7}}, {{8, 2, 9, 7, 8}, {7, 9, 9, 5, 8}, {8, 4, 3 ,
   0, 3}, {0, 7, 5, 0, 0}}}

Cases[#, _?(OddQ[#[[3]]] &)] & /@ data

{{{4, 0, 5, 0, 5}, {0, 1, 1, 2, 8}}, {{8, 4, 3, 5, 5}, {0, 6, 1, 0, 5}, {1, 1,
    7, 8, 0}, {8, 3, 9, 4, 7}}, {{8, 2, 9, 7, 8}, {7, 9, 9, 5, 8}, {8, 4, 3 ,
   0, 3}, {0, 7, 5, 0, 0}}}

DeleteCases[#, _?(EvenQ[#[[3]]] &)] & /@ data

{{{4, 0, 5, 0, 5}, {0, 1, 1, 2, 8}}, {{8, 4, 3, 5, 5}, {0, 6, 1, 0, 5}, {1, 1,
    7, 8, 0}, {8, 3, 9, 4, 7}}, {{8, 2, 9, 7, 8}, {7, 9, 9, 5, 8}, {8, 4, 3 ,
   0, 3}, {0, 7, 5, 0, 0}}}

GatherBy[#, OddQ[#[[3]]] &] & /@ data

{{{{4, 0, 5, 0, 5}, {0, 1, 1, 2, 8}}, {{0, 1, 8, 1, 5}, {8, 3, 2, 9, 1}, {9 ,
    1, 0, 1, 3}}}, {{{8, 4, 3, 5, 5}, {0, 6, 1, 0, 5}, {1, 1, 7, 8, 0}, {8, 3,
     9, 4, 7}}, {{0, 5, 4, 7, 8}}}, {{{1, 9, 0, 4, 8}}, {{8, 2, 9, 7, 8}, { 7,
    9, 9, 5, 8}, {8, 4, 3, 0, 3}, {0, 7, 5, 0, 0}}}}

SortBy[#, OddQ[#[[3]]] &] & /@ data

{{{0, 1, 8, 1, 5}, {8, 3, 2, 9, 1}, {9, 1, 0, 1, 3}, {0, 1, 1, 2, 8}, {4, 0 ,
   5, 0, 5}}, {{0, 5, 4, 7, 8}, {0, 6, 1, 0, 5}, {1, 1, 7, 8, 0}, {8, 3, 9, 4,
    7}, {8, 4, 3, 5, 5}}, {{1, 9, 0, 4, 8}, {0, 7, 5, 0, 0}, {7, 9, 9, 5,
   8}, {8, 2, 9, 7, 8}, {8, 4, 3, 0, 3}}}

SplitBy[#, OddQ[#[[3]]] &] & /@ data

{{{{4, 0, 5, 0, 5}, {0, 1, 1, 2, 8}}, {{0, 1, 8, 1, 5}, {8, 3, 2, 9, 1}, {9 ,
    1, 0, 1, 3}}}, {{{8, 4, 3, 5, 5}, {0, 6, 1, 0, 5}, {1, 1, 7, 8, 0}, {8, 3,
     9, 4, 7}}, {{0, 5, 4, 7, 8}}}, {{{1, 9, 0, 4, 8}}, {{8, 2, 9, 7, 8}, { 7,
    9, 9, 5, 8}, {8, 4, 3, 0, 3}, {0, 7, 5, 0, 0}}}}


Bob Hanlon

---- "Szabolcs Horv=C3=A1t" <szhorvat at gmail.com> wrote:

==========================
On 2011.06.01. 13:27, Bob Hanlon wrote:
> GatherBy[myArray, #[[3]]&]
>
> GatherBy[#, #[[3]]&]&  /@ myListOfArrays
>
>

Is this kind of use of nested functions with # guaranteed to be safe
(let's disregard readability concerns for now)?

It appears that # indeed is always the argument of the innermost
function, but I couldn't find this stated in the docs.  Instead, it's
suggested in the docs (page for Slot) to use named arguments with nested
functions (but those aren't without name-conflict / localization issues
either)



  • Prev by Date: Re: Portfolio Optimization
  • Next by Date: Re: Workbench - Recommendation?
  • Previous by thread: Re: Mapping to Create Nested Loops
  • Next by thread: Dt@x@1