[Date Index] [Thread Index] [Author Index]
Re: Re: Intersection of lists of lists based on the first term
On Fri, Apr 11, 2008 at 9:46 PM, Stern <nycstern at gmail.com> wrote: > Thank you, but I think I am not understanding the second bit -- what is > (isCommon[#] = True) & /@ timeStampsInCommon supposed to achieve other than > a list of True equal to the length of timeStampsInCommon in every case? > Perhaps I should have written it as Scan[(isCommon[#] = True) &, timeStampsInCommon] to emphasize that the function (isCommon[#] = True) & is only invoked for its side effects, i.e. defining new DownValues for the symbol isCommon. This will set the value of isCommon[ ... ] to True for each of the elements in timeStampsInCommon. Thus, isCommon[elem] is equivalent to MemberQ[timeStampsInCommon, elem], but it is much faster if timeStampsInCommon has many elements (because Mathematica will use a hash table for looking up elements instead of simply iterating through timeStampsInCommon). Note that isCommon needs to be Cleared after each use (a better solution is encapsulating the whole thing into a Module where isCommon is a local symbol).