7
$\begingroup$

Here is what I have tried first.

Thread[f["x", {"1", "2"}]]

{f["x", "1"], f["x", "2"]}

But the weirdest thing happened when I substituted StringJoin for f

Thread[StringJoin["x", {"1", "2"}]]

"x12"

I thought it would be like this below

{"x1","x2"}

Is there anyone who can explain this?

$\endgroup$
1
  • 3
    $\begingroup$ Why use Thread for this? Table["x"<>n, {n, {"1","2"}}] or "x"<>#&/@{"1","2"} seem like more straightforward ways to get this result. $\endgroup$
    – Jason B.
    Commented Aug 11, 2020 at 1:51

1 Answer 1

9
$\begingroup$

Thread >> Possible Issues:

  • "Thread evaluates the whole expression before threading"

Wrap StringJoin with Unevaluated to prevent evaluation of StringJoin before Thread gets to work:

Thread[Unevaluated @ StringJoin["x", {"1", "2"}]]
{"x1", "x2"}

Alternatively, you can use

Thread[foo["x", {"1", "2"}]] /. foo -> StringJoin
{"x1", "x2"}
$\endgroup$

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