MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Converting a mapping into a well-defined function

  • To: mathgroup at
  • Subject: [mg56514] Re: [mg56499] Converting a mapping into a well-defined function
  • From: "David Park" <djmp at>
  • Date: Thu, 28 Apr 2005 02:40:00 -0400 (EDT)
  • Sender: owner-wri-mathgroup at

The only thing I don't understand about the statement of your problem is
that you didn't want to sort the resulting B list, but then you reversed the
order of e and d, which rather implies that you did sort it.

In any case, here is a modification of Carl Woll's remarkable UnsortedUnion
routine, which is explained in the Examples under Union in Mathematica Help.
Carl's routine takes the Union of a set of exact elements without changing
their order (and is very efficient). I modify his routine so the match is
made on only the first element of the 2-tuples.

UnsortedUnion2[x_] :=
  Module[{f}, f[{a_, b_}] := (f[{a, _}] = Sequence[]; {a, b}); f /@ x]

alist = {{a, 5}, {b, 27}, {a, 14}, {c, 4}, {e, 94}, {b, 6}, {d, 9}, {e, 4}};

{{a, 5}, {b, 27}, {c, 4}, {e, 94}, {d, 9}}

David Park
djmp at

From: Gilmar [mailto:gilmar.rodriguez at]
To: mathgroup at

Dear Mathematica User Friends:

I need to build a module that takes a non-empty set A
(whose elements are 2-tuples), and appends (or not) those 2-tuples
to a set B  according to the following criteria:

(1.) if the abscissa of a 2-tuple in A is unique (among the abscissas
of all the other 2-tuples in A), then the module appends that 2-tuple
to the set B.

(2.) if there are n 2-tuples in A that share the same abscissa, then
the module appends the first of those 2-tuples to B ("first" here means
the first of such n 2-tuple encountered, as you read A from left to
right),but does not append the rest to B.

(3.) the module does not attempt to sort the set B, after the appending
process is completed.


If A = {{a,5},{b,27},{a,14},{c,4},{e,94},{b,6},{d,9},{e,4}}



Another way to re-phrase the above example;

if A defines a mapping:

A(a)=5, A(b)=27, A(a)=14, A(c)=4, A(e)=94, A(b)=6, A(d)=9, A(e)=4

them the module converts the mapping A into a well-defined function:

A(a)=5, A(b)=27, A(c)=4, A(d)=9, A(e)=94.

Thank you for your help!

  • Prev by Date: Re: Re: can Mathematica be useful for this?
  • Next by Date: gridlines
  • Previous by thread: Re: Converting a mapping into a well-defined function
  • Next by thread: Question about Compiling, external functions, and For loops