[33023] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 4299 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Oct 20 18:14:17 2014

Date: Mon, 20 Oct 2014 15:14:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 20 Oct 2014     Volume: 11 Number: 4299

Today's topics:
    Re: what's wrong with given when? <gravitalsun@hotmail.foo>
    Re: what's wrong with given when? <rweikusat@mobileactivedefense.com>
    Re: what's wrong with given when? <gravitalsun@hotmail.foo>
    Re: what's wrong with given when? <marc.girod@gmail.com>
    Re: what's wrong with given when? <rweikusat@mobileactivedefense.com>
    Re: what's wrong with given when? <rweikusat@mobileactivedefense.com>
    Re: what's wrong with given when? <gravitalsun@hotmail.foo>
    Re: what's wrong with given when? <gravitalsun@hotmail.foo>
    Re: what's wrong with given when? <rweikusat@mobileactivedefense.com>
    Re: what's wrong with given when? <hjp-usenet3@hjp.at>
    Re: what's wrong with given when? <rweikusat@mobileactivedefense.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Mon, 20 Oct 2014 00:12:52 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: what's wrong with given when?
Message-Id: <m219gp$fg5$1@news.ntua.gr>

>
> That's a non-sequitur. Among other things, I'm using given/when in code
> which manages 'firewall rules source address lists' and makes real-time
> content-filtering decisions for thousands of mobile VPN users
> (in three continents) and it works very nicely 'under stress'.
>

why you did not use a hash ?


------------------------------

Date: Sun, 19 Oct 2014 22:33:46 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: what's wrong with given when?
Message-Id: <87egu3kamd.fsf@doppelsaurus.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:
>> That's a non-sequitur. Among other things, I'm using given/when in code
>> which manages 'firewall rules source address lists' and makes real-time
>> content-filtering decisions for thousands of mobile VPN users
>> (in three continents) and it works very nicely 'under stress'.
>>
>
> why you did not use a hash ?

Because it didn't make sense in this context.


------------------------------

Date: Mon, 20 Oct 2014 10:22:25 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: what's wrong with given when?
Message-Id: <m22d6j$26vd$1@news.ntua.gr>

On 20/10/2014 00:33, Rainer Weikusat wrote:
> George Mpouras <gravitalsun@hotmail.foo> writes:
>>> That's a non-sequitur. Among other things, I'm using given/when in code
>>> which manages 'firewall rules source address lists' and makes real-time
>>> content-filtering decisions for thousands of mobile VPN users
>>> (in three continents) and it works very nicely 'under stress'.
>>>
>>
>> why you did not use a hash ?
>
> Because it didn't make sense in this context.
>


ok of course I do not know the context , but at 95% of the cases the 
if/elsif/else/when can be converted to a much faster hash key retrieval




------------------------------

Date: Mon, 20 Oct 2014 00:43:50 -0700 (PDT)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: what's wrong with given when?
Message-Id: <0c0f79aa-ce94-4281-b395-c77727e441dd@googlegroups.com>

On Sunday, 19 October 2014 20:38:13 UTC+1, Rainer Weikusat  wrote:

> There's nothing which can be expressed with one
> which cannot also be expressed with a sequence of independent or
> semi-dependent tests

Granted.
There comes 'elegance', 'readability'...
Such a common need should not require so much boilerplate.
I appreciate getting rid of the 'elsif'.
I also prefer the indentation given/for ... when allows me, as well as the scoping.

Marc


------------------------------

Date: Mon, 20 Oct 2014 14:32:06 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: what's wrong with given when?
Message-Id: <87mw8qamuh.fsf@doppelsaurus.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:
> On 20/10/2014 00:33, Rainer Weikusat wrote:
>> George Mpouras <gravitalsun@hotmail.foo> writes:
>>>> That's a non-sequitur. Among other things, I'm using given/when in code
>>>> which manages 'firewall rules source address lists' and makes real-time
>>>> content-filtering decisions for thousands of mobile VPN users
>>>> (in three continents) and it works very nicely 'under stress'.
>>>>
>>>
>>> why you did not use a hash ?
>>
>> Because it didn't make sense in this context.
>>
> ok of course I do not know the context , but at 95% of the cases the
> if/elsif/else/when can be converted to a much faster hash key
> retrieval

If so, you're using conditionals 'in strange ways'. They're usually
supposed to enabling executing some code conditionally, not to work out
static 1:1 mappings from one set of values to another dynamically.


------------------------------

Date: Mon, 20 Oct 2014 19:31:35 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: what's wrong with given when?
Message-Id: <87tx2y8uew.fsf@doppelsaurus.mobileactivedefense.com>

Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
> George Mpouras <gravitalsun@hotmail.foo> writes:
>> On 20/10/2014 00:33, Rainer Weikusat wrote:
>>> George Mpouras <gravitalsun@hotmail.foo> writes:
>>>>> That's a non-sequitur. Among other things, I'm using given/when in code
>>>>> which manages 'firewall rules source address lists' and makes real-time
>>>>> content-filtering decisions for thousands of mobile VPN users
>>>>> (in three continents) and it works very nicely 'under stress'.
>>>>>
>>>>
>>>> why you did not use a hash ?
>>>
>>> Because it didn't make sense in this context.
>>>
>> ok of course I do not know the context , but at 95% of the cases the
>> if/elsif/else/when can be converted to a much faster hash key
>> retrieval
>
> If so, you're using conditionals 'in strange ways'. They're usually
> supposed to enabling executing some code conditionally, not to work out
> static 1:1 mappings from one set of values to another dynamically.

Reasonably simple example:

-----------------
sub is_in_set
{
    my ($ips, $ip) = @_;
    my ($f, $l, $m);

    $f = 0;
    $l = @$ips;
    do {
        $m = $f + int(($l - $f) / 2);
        
        given ($ips->[$m]->relation_to($ip)) {
            when (-1) {
                $f = $m + 1;
            }

            when (0) {
                return 1;
            }

            when (1) {
                $l = $m;
            }
        }
    } while ($f < $l);

    return;
}
---------------

This takes a set of IP(v4) addresses and an IP(v4) address object as
arguments. The set is represented as sorted list of address and address
range objects and the subroutine determines if the 2nd argument is in
the set by trying to locate it w/ a binary search[*].

This is IMHO a nice showcase for a multiway conditional because there's
a (n relatively expensive, a method call) operation returning one of
three distinct values which indicate what needs to be done next, ie,
this doesn't naturally map to if - else because it isn't binary and
isn't easily expressed with if - elsif - else because the operation
should only be performed once (hence, an additional variable would be
needed). Another example would be handling the three possible return
values of fork.

[*] A few quick tests I made suggest that

$m = $f + int(($l - $f) / 2)

would be better written as

$m = $f + (($l - $f) >> 1)

which doesn't do floating-point calculations.


------------------------------

Date: Tue, 21 Oct 2014 00:13:31 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: what's wrong with given when?
Message-Id: <m23tu1$2mnu$1@news.ntua.gr>

On 20/10/2014 9:31 μμ, Rainer Weikusat wrote:
> he set is represented as sorted list of address and address
> range objects and the subroutine determines if the 2nd argument is in
> the set by trying to locate it w/ a binary search[*].

very good peace of code.
What I mean is the following syntax alternative




sub is_in_set
{
my ($ips, $ip) = @_;
my ($f, $l, $m);
$f = 0;
$l = scalar @{$ips};

my %decide = (
-1 => sub{ $f = $m + 1 },
0  => sub{ return 1 },
1  => sub{ $l = $m } );

	do
	{
	$m = $f + int(($l - $f) / 2);
         &{$decide{ $ips->[$m]->relation_to($ip) }}
	}
	while ($f < $l);

return
}



------------------------------

Date: Tue, 21 Oct 2014 00:32:43 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: what's wrong with given when?
Message-Id: <m23v22$2pd8$1@news.ntua.gr>

your code reminds me an old idea , I have not the time now to think how 
it fits at your example , but anyway here it is




my $n     = binary_check_array_against_code(\@array, \&SomeCode);



#   SomeCode( $offset_of_the_array );
#   Should return TRUE when we think that everything is ok
#
sub SomeCode ($)
{
my $Pos = shift;
 ... ? 1 : 0
};


#   binary_check_array_against_code( $ARRAY , $CODE );
#   Returns the offset where the CODE is true
#
sub binary_check_array_against_code (\@&)
{
my ($low, $high) = (0, scalar @{$_[0]});

	while ($low < $high)
	{
	my $Pos = int(($low + $high)/2);
	$_[1]->($Pos) ? ($low = $Pos + 1) : ($high = $Pos)
	}
$low
}


------------------------------

Date: Mon, 20 Oct 2014 22:40:05 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: what's wrong with given when?
Message-Id: <878uka8loq.fsf@doppelsaurus.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:
> On 20/10/2014 9:31 μμ, Rainer Weikusat wrote:
>> he set is represented as sorted list of address and address
>> range objects and the subroutine determines if the 2nd argument is in
>> the set by trying to locate it w/ a binary search[*].
>
> very good peace of code.
> What I mean is the following syntax alternative
>
> sub is_in_set
> {
> my ($ips, $ip) = @_;
> my ($f, $l, $m);
> $f = 0;
> $l = scalar @{$ips};
>
> my %decide = (
> -1 => sub{ $f = $m + 1 },
> 0  => sub{ return 1 },
> 1  => sub{ $l = $m } );
>
> 	do
> 	{
> 	$m = $f + int(($l - $f) / 2);
>         &{$decide{ $ips->[$m]->relation_to($ip) }}
> 	}
> 	while ($f < $l);
>
> return
> }

This obviously wouldn't work because the 'return 1' in the anonymous
subroutine wouldn't terminate the outer subroutine. OTOH, I had expected
this to be slower because of additional subroutine call but according to
some benchmark I just code (and hopefully debugged until it actually
works), this isn't really the case.

-----------
use feature 'switch';

use Benchmark;

my @a = 0 .. 99999;

my @loc;
push(@loc, int(rand(100000))) for (0 .. 100);

timethese(-3,
	  {
	   given => sub {
	       my ($f, $l, $m, $v);

	   LOOP:
	       for $v (@loc) {
		   $f = 0;
		   $l = @a;
		   do {
		       $m = $f + (($l - $f) >> 1);
		       given ($v <=> $a[$m]) {
			   when (1) {
			       $f = $m + 1;
			   }

			   when (0) {
			       next LOOP;
			   }

			   when (-1) {
			       $l = $m;
			   }
		       }
		   } while ($f < $l);
	       }
	   },
	   
	   hash => sub {
	       my ($f, $l, $m, $v);

	       my %switch = (
			     1 => sub { $f = $m + 1},
			     0 => sub { next LOOP },
			     -1 => sub { $l = $m });

	   LOOP:
	       for $v (@loc) {
		   $f = 0;
		   $l = @a;
		   do {
		       $m = $f + (($l - $f) >> 1);
		       &{$switch{$v <=> $a[$m]}};
		   } while ($f < $l);
	       }
	   }});


------------------------------

Date: Mon, 20 Oct 2014 23:51:04 +0200
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: what's wrong with given when?
Message-Id: <slrnm4b0u8.tai.hjp-usenet3@hrunkner.hjp.at>

On 2014-10-20 18:31, Rainer Weikusat <rweikusat@mobileactivedefense.com> wrote:
> Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
>> George Mpouras <gravitalsun@hotmail.foo> writes:
>>> On 20/10/2014 00:33, Rainer Weikusat wrote:
>>>> George Mpouras <gravitalsun@hotmail.foo> writes:
>>>>>> That's a non-sequitur. Among other things, I'm using given/when in code
>>>>>> which manages 'firewall rules source address lists' and makes real-time
>>>>>> content-filtering decisions for thousands of mobile VPN users
>>>>>> (in three continents) and it works very nicely 'under stress'.
>>>>>>
>>>>>
>>>>> why you did not use a hash ?
>>>>
>>>> Because it didn't make sense in this context.
>>>>
>>> ok of course I do not know the context , but at 95% of the cases the
>>> if/elsif/else/when can be converted to a much faster hash key
>>> retrieval
>>
>> If so, you're using conditionals 'in strange ways'. They're usually
>> supposed to enabling executing some code conditionally, not to work out
>> static 1:1 mappings from one set of values to another dynamically.
>
> Reasonably simple example:
>
> -----------------
> sub is_in_set
> {
>     my ($ips, $ip) = @_;
>     my ($f, $l, $m);
>
>     $f = 0;
>     $l = @$ips;
>     do {
>         $m = $f + int(($l - $f) / 2);
>         
>         given ($ips->[$m]->relation_to($ip)) {
>             when (-1) {
>                 $f = $m + 1;
>             }
>
>             when (0) {
>                 return 1;
>             }
>
>             when (1) {
>                 $l = $m;
>             }
>         }
>     } while ($f < $l);
>
>     return;
> }

That's a bad example because it is a 1:1 mapping: You have one piece of
code for each of the distinct values of -1, 0, and 1, and other values
either cannot or occur or are not handled.

        hp


-- 
   _  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) |                    | Man feilt solange an seinen Text um, bis
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel


------------------------------

Date: Mon, 20 Oct 2014 22:53:33 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: what's wrong with given when?
Message-Id: <87wq7u76hu.fsf@doppelsaurus.mobileactivedefense.com>

"Peter J. Holzer" <hjp-usenet3@hjp.at> writes:
> On 2014-10-20 18:31, Rainer Weikusat <rweikusat@mobileactivedefense.com> wrote:
>> Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
>>> George Mpouras <gravitalsun@hotmail.foo> writes:
>>>> On 20/10/2014 00:33, Rainer Weikusat wrote:
>>>>> George Mpouras <gravitalsun@hotmail.foo> writes:
>>>>>>> That's a non-sequitur. Among other things, I'm using given/when in code
>>>>>>> which manages 'firewall rules source address lists' and makes real-time
>>>>>>> content-filtering decisions for thousands of mobile VPN users
>>>>>>> (in three continents) and it works very nicely 'under stress'.
>>>>>>>
>>>>>>
>>>>>> why you did not use a hash ?
>>>>>
>>>>> Because it didn't make sense in this context.
>>>>>
>>>> ok of course I do not know the context , but at 95% of the cases the
>>>> if/elsif/else/when can be converted to a much faster hash key
>>>> retrieval
>>>
>>> If so, you're using conditionals 'in strange ways'. They're usually
>>> supposed to enabling executing some code conditionally, not to work out
>>> static 1:1 mappings from one set of values to another dynamically.
>>
>> Reasonably simple example:
>>
>> -----------------
>> sub is_in_set
>> {
>>     my ($ips, $ip) = @_;
>>     my ($f, $l, $m);
>>
>>     $f = 0;
>>     $l = @$ips;
>>     do {
>>         $m = $f + int(($l - $f) / 2);
>>         
>>         given ($ips->[$m]->relation_to($ip)) {
>>             when (-1) {
>>                 $f = $m + 1;
>>             }
>>
>>             when (0) {
>>                 return 1;
>>             }
>>
>>             when (1) {
>>                 $l = $m;
>>             }
>>         }
>>     } while ($f < $l);
>>
>>     return;
>> }
>
> That's a bad example because it is a 1:1 mapping: You have one piece of
> code for each of the distinct values of -1, 0, and 1, and other values
> either cannot or occur or are not handled.

It's not a mapping of values to values. This can be expressed by mapping
the values to subroutines and this is actually faster but that's a
different conversation.


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 4299
***************************************


home help back first fref pref prev next nref lref last post