Skip to content

Thread ​

f applied elementwise across every list-headed argument, other arguments broadcast.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageThread✓
Thread(f(a1, …, an))f threaded over every List-headed operand
Thread(f(a1, …, an), h)like Thread(f(...)), threading only over operands headed by h instead of List
Typed signatures
TypePackageApplies
(any, symbol?) -> anycombinatoricsalways

Domain: Collections

Details
  • Operands not headed by h are broadcast unchanged to every threaded call.
  • The operands are evaluated first, and a lazy ordered collection such as Tuples(…)Tuples(…) threads as the List it evaluates to. The threaded calls themselves are left as built, not evaluated.
  • Left unevaluated when the h-headed operands don't all share one length.
  • Many arithmetic heads already thread over lists automatically; Thread's value is threading a head that doesn't, such as Equal or a plain function.

Examples

Basic #
live ↗
InThread([1,2,3]=[1,5,3])\mathrm{Thread}(\bigl\lbrack1, 2, 3\bigr\rbrack=\bigl\lbrack1, 5, 3\bigr\rbrack)Out[True,False,True]\bigl\lbrack\mathrm{True}, \mathrm{False}, \mathrm{True}\bigr\rbrack

Turns an equation between lists into elementwise equations

live ↗
InThread([a,b,c]=[x,y,z])\mathrm{Thread}(\bigl\lbrack a, b, c\bigr\rbrack=\bigl\lbrack x, y, z\bigr\rbrack)Out{a=xb=yc=z\begin{cases}a=x\\b=y\\c=z\end{cases}

Threading over Equal pushes the function inside both sides

live ↗
InThread(ln⁡(x=y),Equal)\mathrm{Thread}(\ln(x=y), \mathrm{Equal})Outln⁡(x)=ln⁡(y)\ln(x)=\ln(y)
Scope #

A non-list operand is broadcast to every threaded call

live ↗
InThread([1,2,3]=1)\mathrm{Thread}(\bigl\lbrack1, 2, 3\bigr\rbrack=1)Out[True,False,False]\bigl\lbrack\mathrm{True}, \mathrm{False}, \mathrm{False}\bigr\rbrack

A second argument threads over a head other than List

live ↗
InThread(f(g(1,2),g(3,4)),g)\mathrm{Thread}(f(g(1, 2), g(3, 4)), g)Outg(f(1,3),f(2,4))g(f(1, 3), f(2, 4))
Applications #

A scalar is paired with each element of the list

live ↗
InThread([[a,b,c],0])\mathrm{Thread}(\bigl\lbrack\bigl\lbrack a, b, c\bigr\rbrack, 0\bigr\rbrack)Out[[a,0],[b,0],[c,0]]\bigl\lbrack\bigl\lbrack a, 0\bigr\rbrack, \bigl\lbrack b, 0\bigr\rbrack, \bigl\lbrack c, 0\bigr\rbrack\bigr\rbrack

Rules between lists become a list of pairs

live ↗
InThread(Rule([a,b,c],[1,2,3]))\mathrm{Thread}(\mathrm{Rule}(\bigl\lbrack a, b, c\bigr\rbrack, \bigl\lbrack1, 2, 3\bigr\rbrack))Out[Rule(a,1),Rule(b,2),Rule(c,3)]\bigl\lbrack\mathrm{Rule}(a, 1), \mathrm{Rule}(b, 2), \mathrm{Rule}(c, 3)\bigr\rbrack
Properties #

Lists of equal length pair up elementwise, like a transpose

live ↗
InThread([[a,b,c],[x,y,z]])\mathrm{Thread}(\bigl\lbrack\bigl\lbrack a, b, c\bigr\rbrack, \bigl\lbrack x, y, z\bigr\rbrack\bigr\rbrack)Out[[a,x],[b,y],[c,z]]\bigl\lbrack\bigl\lbrack a, x\bigr\rbrack, \bigl\lbrack b, y\bigr\rbrack, \bigl\lbrack c, z\bigr\rbrack\bigr\rbrack
Possible issues #
live ↗
InThread(D⁡([x,xy,xz],[x,y,z]))\mathrm{Thread}(\operatorname{D}(\bigl\lbrack x, xy, xz\bigr\rbrack, \bigl\lbrack x, y, z\bigr\rbrack))Out[1,x,x]\bigl\lbrack1, x, x\bigr\rbrack
live ↗
InThread(Unevaluated(D⁡([x,xy,xz],[x,y,z])))\mathrm{Thread}(\mathrm{Unevaluated}(\operatorname{D}(\bigl\lbrack x, xy, xz\bigr\rbrack, \bigl\lbrack x, y, z\bigr\rbrack)))Out[1,x,x]\bigl\lbrack1, x, x\bigr\rbrack
About
Examples10
Checked againstWolfram Language 10 agree
CrosswalkWolfram Language

See also: MovingMap, Array