TransWikia.com

StringJoin in Thread not working

Mathematica Asked on August 14, 2021

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?

One Answer

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"}

Correct answer by kglr on August 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP