Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #2912

Re: Mapping to Create Nested Loops

From Bob Hanlon <hanlonr@cox.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Mapping to Create Nested Loops
Date 2011-06-02 23:11 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <is95bn$613$1@smc.vnet.net> (permalink)

Show all headers | View raw


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@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)

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Mapping to Create Nested Loops Bob Hanlon <hanlonr@cox.net> - 2011-06-02 23:11 +0000

csiph-web