Infix Function?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Infix Function?

Pete-110
Hi-

I've defined an XOR function as:


CREATE OR REPLACE FUNCTION xor(bool, bool)
  RETURNS bool AS
'SELECT ($1 OR $2) AND NOT ($1 AND $2);'
  LANGUAGE 'sql' IMMUTABLE STRICT;

Is there anyway to create an infix version of this?  I'd really like be
able to write (where a..d are some boolean conditions):

a XOR b XOR c XOR d

instead of:

xor(a, xor(b, xor(c, d)))

which gets a little hard to understand as it gets long, esp. when mixed
with AND & OR.

I know one can do this with user-defined operators, but adding a #
operator for booleans seems ugly.  I thought I saw a way to do this, but
now I can't find it in the manual... TIA.

--Pete

--
Peter Fein                 [hidden email]                 773-575-0694

Basically, if you're not a utopianist, you're a schmuck. -J. Feldman

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
       subscribe-nomail command to [hidden email] so that your
       message can get through to the mailing list cleanly
Reply | Threaded
Open this post in threaded view
|

Re: Infix Function?

Martijn van Oosterhout
On Sun, Jun 26, 2005 at 03:42:25PM -0500, Peter Fein wrote:

> Hi-
>
> I've defined an XOR function as:
>
>
> CREATE OR REPLACE FUNCTION xor(bool, bool)
>   RETURNS bool AS
> 'SELECT ($1 OR $2) AND NOT ($1 AND $2);'
>   LANGUAGE 'sql' IMMUTABLE STRICT;
>
> Is there anyway to create an infix version of this?  I'd really like be
> able to write (where a..d are some boolean conditions):
Judging by:

http://www.postgresql.org/docs/7.4/interactive/sql-createoperator.html

operators can't be normal text strings...

Hope this helps,
--
Martijn van Oosterhout   <[hidden email]>   http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

attachment0 (240 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Infix Function?

Bruno Wolff III
In reply to this post by Pete-110
On Sun, Jun 26, 2005 at 15:42:25 -0500,
  Peter Fein <[hidden email]> wrote:
>
> Is there anyway to create an infix version of this?  I'd really like be
> able to write (where a..d are some boolean conditions):

Use <>:
area=> select true <> false <> true;
 ?column?
----------
 f
(1 row)

area=> select false <> true <> false;
 ?column?
----------
 t
(1 row)

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
       subscribe-nomail command to [hidden email] so that your
       message can get through to the mailing list cleanly