Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I was trying to use strings as tags for Reap/Sow and I've found out the following strange behavior when I also add a function as the third argument of Reap[]:

Reap[Sow[11, "a"]; Sow[21, "b"]; Sow[12, "c"]; Sow[15, "a"], {"a", "b", "c"}]

(* {15, {{{11, 15}}, {{21}}, {{12}}}} *)

Reap[Sow[11, "a"]; Sow[21, "b"]; Sow[12, "c"]; Sow[15, "a"], {"a", "b", "c"}, Max]

(* {15, {{Max[15, "a"]}, {Max[21, "b"]}, {Max[12, "c"]}}} *)

Whereas when I use numeric tags I get the following, expected, result:

Reap[Sow[11, 1]; Sow[21, 2]; Sow[12, 3]; Sow[15, 1], {1, 2, 3}, Max]
(* {15, {{15}, {21}, {12}}} *)

Why do string-tags seem not to work properly as "tags"? And more in general, do you know what kind of structures can be used as tags?

share|improve this question

closed as off-topic by m_goldberg, bobthechemist, Artes, Ajasja, Michael E2 Jan 8 at 18:47

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, bobthechemist, Artes, Ajasja, Michael E2
If this question can be reworded to fit the rules in the help center, please edit the question.

This is explicitly in the documentation... please read the page for Reap: it says that Reap[expr, patt, f] gives you {expr,{f[tag1,{e11, e12, ...}], ...}}. What you're computing is just Max["a", 11, 15] which evaluates to Max[15, "a"]. What's the maximum of a string and a number? The only reason it "works" in your second example is because your tag numbers are small. If you used a larger number, you'll see that you get a different result. Use Max on just the second argument and you're fine. –  rm -rf Jan 8 at 17:30

1 Answer 1

Reap[Sow[11, "a"]; Sow[21, "b"]; Sow[12, "c"]; Sow[15, "a"], {"a", "b", "c"}, Max[#2] &]
(* {15, {{15}, {21}, {12}}} *)
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.