Re: Unsorted Union

*To*: mathgroup at smc.vnet.net*Subject*: [mg61475] Re: Unsorted Union*From*: David Bailey <dave at Remove_Thisdbailey.co.uk>*Date*: Wed, 19 Oct 2005 23:07:24 -0400 (EDT)*References*: <dj4p6t$gpt$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Robert Schoefbeck wrote: > Hello all, > > in the help section i found the following thing: > > UnsortedUnion[x_] := Module[{f}, f[y_] := (f[y] = Sequence[]; y); f/@x] > > It does just what it says: > UnsortedUnion[{a,c,b,d,a,b}]={a,c,b,d} > > (ordinary Union would Sort the result) > > my question is: How does the definition work? I know all the syntax > and i have been using these concepts for for some time now. But i cant > figure out what this definition does. > > Robert > Hello, That definition creates a temporary function f, which it maps across the input list ( /@ is the operator for Map). The first time f[c] (say) is called it adds a definition for f[c] and returns its argument c. However, the second time it is called an explicit definition for f[c] exists which returns Sequence[] - so the second c is replaced by Sequence[] - which eliminates the term altogether. I'm fairly sure there would be more efficient ways to do this if the list were long or the function called frequently. BTW, If you are new to Sequence, it is worth exploring it a bit - it can achieve some surprising things! David Bailey http://www.dbaileyconsultancy.co.uk