Read Online
Download PDF
Additional resources
Revision history
Known typos/bugs
Report a bug
License terms
About the author
"node479_1.gif" "node479_2.gif" "node479_3.gif"    Example: unsorted union problem revisited  

One of the past examples for the <MapIndexed> function was to create an unsorted union of elements for some list. In particular, a set of rules for elements to their positions was constructed using MapIndexed. The code for the <unsortedUnion> function looked like:


The same rules can be created using Thread, which we will do now. This will be our test list:



This will create a set of rules similar to the one we have previously created by MapIndexed:



The only difference is that the positions are not wrapped in curly braces (lists), as they were for MapIndexed:



We could Map List on the result in our Thread-based realization, like this:



However, there is a more efficient realization - to keep the list as it is, but use Part instead of Extract (Part has a somewhat different syntax and in particular accepts a simple list of positions like the one generated by Thread). So, this is the final version:





We can now compare performance on some large list:    






The performance is roughly the same.


There are more capabilities of the Thread command. Some of them are discussed in Mathematica Help. We will eventually discuss them as we get to examples where they are useful.

"node479_22.gif" "node479_23.gif" "node479_24.gif"

Created by Wolfram Mathematica 6.0  (05 February 2009) Valid XHTML 1.1!