Skip to main content
Remove spare variable from Module, use new syntax for First.
Source Link
Simon Woods
  • 85.4k
  • 8
  • 180
  • 326

I think a slightly modified version of Leonid's tableGenAlt should do what you need.

ClearAll[tableGenAlt];
SetAttributes[tableGenAlt, HoldAll];
tableGenAlt[expr_, test_, iter__List] := 
  Module[{sowTag, val, v = Hold[iter][[All, 1]]}, 
   If[# === {}, #, First@#] &@Last@Reap[First[Last@Reap[
      Do[If[test[#], Sow[{#, {ReleaseHold@v}}, sowTag]] &[expr], iter], sowTag]];
        sowTag], {}]];

tableGenAlt[i + j + k, GreaterThan[6], {i, 3}, {j, 3}, {k, 3}]
(* {{7, {1, 3, 3}}, {7, {2, 2, 3}}, {7, {2, 3, 2}}, {8, {2, 3, 
   3}}, {7, {3, 1, 3}}, {7, {3, 2, 2}}, {8, {3, 2, 3}}, {7, {3, 3, 
   1}}, {8, {3, 3, 2}}, {9, {3, 3, 3}}} *)

I think a slightly modified version of Leonid's tableGenAlt should do what you need.

ClearAll[tableGenAlt];
SetAttributes[tableGenAlt, HoldAll];
tableGenAlt[expr_, test_, iter__List] := 
  Module[{sowTag, val, v = Hold[iter][[All, 1]]}, 
   If[# === {}, #, First@#] &@Last@Reap[
      Do[If[test[#], Sow[{#, {ReleaseHold@v}}, sowTag]] &[expr], iter], sowTag]];

tableGenAlt[i + j + k, GreaterThan[6], {i, 3}, {j, 3}, {k, 3}]
(* {{7, {1, 3, 3}}, {7, {2, 2, 3}}, {7, {2, 3, 2}}, {8, {2, 3, 
   3}}, {7, {3, 1, 3}}, {7, {3, 2, 2}}, {8, {3, 2, 3}}, {7, {3, 3, 
   1}}, {8, {3, 3, 2}}, {9, {3, 3, 3}}} *)

I think a slightly modified version of Leonid's tableGenAlt should do what you need.

ClearAll[tableGenAlt];
SetAttributes[tableGenAlt, HoldAll];
tableGenAlt[expr_, test_, iter__List] := 
  Module[{sowTag, v = Hold[iter][[All, 1]]},
   First[Last@Reap[
      Do[If[test[#], Sow[{#, {ReleaseHold@v}}, sowTag]] &[expr], iter], 
        sowTag], {}]];

tableGenAlt[i + j + k, GreaterThan[6], {i, 3}, {j, 3}, {k, 3}]
(* {{7, {1, 3, 3}}, {7, {2, 2, 3}}, {7, {2, 3, 2}}, {8, {2, 3, 
   3}}, {7, {3, 1, 3}}, {7, {3, 2, 2}}, {8, {3, 2, 3}}, {7, {3, 3, 
   1}}, {8, {3, 3, 2}}, {9, {3, 3, 3}}} *)
Source Link
Simon Woods
  • 85.4k
  • 8
  • 180
  • 326

I think a slightly modified version of Leonid's tableGenAlt should do what you need.

ClearAll[tableGenAlt];
SetAttributes[tableGenAlt, HoldAll];
tableGenAlt[expr_, test_, iter__List] := 
  Module[{sowTag, val, v = Hold[iter][[All, 1]]}, 
   If[# === {}, #, First@#] &@Last@Reap[
      Do[If[test[#], Sow[{#, {ReleaseHold@v}}, sowTag]] &[expr], iter], sowTag]];

tableGenAlt[i + j + k, GreaterThan[6], {i, 3}, {j, 3}, {k, 3}]
(* {{7, {1, 3, 3}}, {7, {2, 2, 3}}, {7, {2, 3, 2}}, {8, {2, 3, 
   3}}, {7, {3, 1, 3}}, {7, {3, 2, 2}}, {8, {3, 2, 3}}, {7, {3, 3, 
   1}}, {8, {3, 3, 2}}, {9, {3, 3, 3}}} *)