Re: Pure function in a pure function (again)

*To*: mathgroup at smc.vnet.net*Subject*: [mg69817] Re: Pure function in a pure function (again)*From*: dimmechan at yahoo.com*Date*: Sat, 23 Sep 2006 23:45:17 -0400 (EDT)*References*: <ef2tl4$mn4$1@smc.vnet.net>

Hello. (For clarity I have converted everything to InputForm.) Remove["Global`"] I read enough time your post/question and as far as I have understood your pure function f is icorect. "If Dat is a 2D-table, the following picks out the rows whose first element is 1,2 or 3 and adds up their the second elements". Are you sure that f do that? dat = Array[2*#1 + #2 & , {3, 3}, {0, 1}] {{1, 2, 3}, {3, 4, 5}, {5, 6, 7}} f[x_] := Plus @@ Column[Select[dat, #1[[1]] == x & ], 2] f /@ Range[3] {{{3, 4, 5}}, {}, {{5, 6, 7}}} If I guess right your desired result it should be {{1+2,2+2,3+2},{3+4,4+4,5+4}}--->{{3,4,5},{7,8,9}}. BTW, even if I am wrong about what I have understood, I believe that the empty list in your result {{{3, 4, 5}}, {}, {{5, 6, 7}}} is not welcome (at least for me...). You can see what f does executing the following commands: Trace[f /@ Range[3], Select] {{{{HoldForm[Select[{{1, 2, 3}, {3, 4, 5}, {5, 6, 7}}, #1[[1]] == 1 & ]], HoldForm[{{1, 2, 3}}]}}}, {{{HoldForm[Select[{{1, 2, 3}, {3, 4, 5}, {5, 6, 7}}, #1[[1]] == 2 & ]], HoldForm[{}]}}}, {{{HoldForm[Select[{{1, 2, 3}, {3, 4, 5}, {5, 6, 7}}, #1[[1]] == 3 & ]], HoldForm[{{3, 4, 5}}]}}}} f correctly collect the sublists in which the first element is 1 or 2 or 3. Trace[f /@ Range[3], Plus] {{HoldForm[{{1, 2, 3}} + 2], HoldForm[2 + {{1, 2, 3}}], HoldForm[{2 + {1, 2, 3}}], {HoldForm[2 + {1, 2, 3}], HoldForm[{2 + 1, 2 + 2, 2 + 3}], {HoldForm[2 + 1], HoldForm[3]}, {HoldForm[2 + 2], HoldForm[4]}, {HoldForm[2 + 3], HoldForm[5]}}}, {HoldForm[{} + 2], HoldForm[2 + {}], HoldForm[{}]}, {HoldForm[{{3, 4, 5}} + 2], HoldForm[2 + {{3, 4, 5}}], HoldForm[{2 + {3, 4, 5}}], {HoldForm[2 + {3, 4, 5}], HoldForm[{2 + 3, 2 + 4, 2 + 5}], {HoldForm[2 + 3], HoldForm[5]}, {HoldForm[2 + 4], HoldForm[6]}, {HoldForm[2 + 5], HoldForm[7]}}}} But instead of the second element of EACH sublist, it actually adds up the second element of the FIRST sublist (in particular 2 here). So here is my attempt. It succeds as you can see (supposing I have understood right!!!). Before defining the pure function let me show you step by step the procedure. dat = Array[2*#1 + #2 & , {3, 3}, {0, 1}] {{1, 2, 3}, {3, 4, 5}, {5, 6, 7}} You want an output {{1,2,3},{3,4,5}}. This can be achieve by Cases and Alternatives Alias["|"] Alternatives Cases[dat, {1, ___} | {2, ___} | {3, ___}] {{1, 2, 3}, {3, 4, 5}} Of course it is supposed to do programming and not to type by hands. So Alternatives @@ ({#1, ___} & ) /@ Range[3] {1, ___} | {2, ___} | {3, ___} lst=Cases[dat, Alternatives @@ ({#1, ___} & ) /@ Range[3]] {{1, 2, 3}, {3, 4, 5}} Now we make the difficult step (at least for me...), it comes the easy (#1[[2]] + #1 & ) /@ lst {{3, 4, 5}, {7, 8, 9}} As you see the unwelcome {} is not there but what is more important is that the result is what (I consider) the desired. It is time to collect the pieces. Voila colFiraddSec[(x_)?MatrixQ, (n_)?IntegerQ] := (#1[[2]] + #1 & ) /@ Cases[x, Alternatives @@ ({#1, ___} & ) /@ Range[n]] Examples colFiraddSec[dat, 3] {{3, 4, 5}, {7, 8, 9}} dat2 = Table[Random[Integer, {1, 10}], {10}, {10}] {{5, 9, 6, 10, 3, 8, 9, 4, 8, 4}, {8, 5, 1, 4, 9, 7, 5, 3, 8, 9}, {3, 10, 10, 7, 3, 3, 9, 1, 6, 8}, {6, 1, 1, 3, 3, 10, 1, 4, 9, 9}, {7, 5, 10, 2, 5, 2, 3, 7, 2, 3}, {2, 4, 5, 6, 10, 10, 8, 2, 8, 2}, {6, 2, 5, 9, 1, 1, 7, 4, 3, 10}, {3, 4, 4, 3, 10, 3, 2, 9, 5, 2}, {10, 9, 7, 4, 10, 6, 6, 6, 10, 6}, {7, 9, 5, 4, 6, 7, 5, 1, 9, 2}} colFiraddSec[dat2, 3] {{13, 20, 20, 17, 13, 13, 19, 11, 16, 18}, {6, 8, 9, 10, 14, 14, 12, 6, 12, 6}, {7, 8, 8, 7, 14, 7, 6, 13, 9, 6}} And the game is over. Regards Dimitrios Anagnostou ------------------------------------------ We can walk our road together / If our goals are all the same We can run alone and free / If we pursue a different aim Let the truth of love be lighted / Let the love of truth shine clear Sensibility / Armed with sense and liberty With the Heart and Mind united / In a single perfect sphere RUSH "Cygnus X-1 Book II Hemispheres"