Assocs from linear arrays / assert.pl 
Author Message
 Assocs from linear arrays / assert.pl

Is there a builtin way to "interleave" two regular arrays to create
an associative array? Something like

&assert ( '#xx==$#yy' ) ;
%curve = interleave ( *x , *y ) ;

so $curve{$x[$foo]} = $y[foo] for 0<=$foo<=$#xx

I guess you'd have to pass the arrays by name - even to a builtin?
Oh, and while I'm crying for the moon - anyone got an assert.pl?

All suggestions welcomed,

Brian Davies.



Mon, 28 Apr 1997 17:33:27 GMT  
 Assocs from linear arrays / assert.pl

daviesb> Is there a builtin way to "interleave" two regular arrays to create
daviesb> an associative array? Something like



daviesb> &assert ( '#xx==$#yy' ) ;
daviesb> %curve = interleave ( *x , *y ) ;

daviesb> so $curve{$x[$foo]} = $y[foo] for 0<=$foo<=$#xx

It's a built-in... called a "slice".

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)

--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Phrase: "Welcome to Portland, Oregon ... home of the California Raisins!"



Tue, 29 Apr 1997 00:44:55 GMT  
 Assocs from linear arrays / assert.pl

Quote:
>>>>>> daviesb  writes:
> # Oh, and while I'm crying for the moon - anyone got an assert.pl?
> assert.pl is in the Perl library, but be careful! I've been trying
> to put assertions in my packages in Perl 5 to make them a bit more
> robust, but the following snippet doesn't do what you think:

>   [ ... code deleted to save space ... ]
> Apparently, the string in the assertion gets passwd to the assert()
> subroutine and is eval'd in assert's scope - at that point $self
> looks like an undef'd variable. I just wrote an Assert.pm that
> has one wrapper routine - Assert() - and it checks the logical
> value of its parameter, then calls the normal panic() routine
> in assert.pl:
>   package Assert;

>   require 5.000;
>   require Exporter;

>   # put it in the package
>   require 'assert.pl';



>   sub Assert {
>     panic("ASSERTION FAILED:") unless $_[$[];
>   }

>   1;

Would the following alternative work?

  package Assert;

  require 5.000;
  require Exporter;

  require 'assert.pl';  # ... so that &panic() is accessible



  $debug = 0;

  sub Assert {

     if ($debug) {

        # Get the caller's package via the 'caller' routine.

        my($package) = caller;
        eval "package $package";


     }
  }

  1;

(I took the '&panic' line right out of assert.pl and just prepended
'Assert::' ... this line could be tweaked a bit more ... perhaps by
getting rid of '$[').

Quote:
> One thing I haven't found a good way to do is to "compile out"
> assertions if debug is off. What you really need is a macro
> which I haven't though of a way to do in Perl. I'd like 'Assert(exp);'
> to evaluate to '_Assert(exp) if ($debug);' but I don't want to
> run my scripts through cpp. What I've been doing is just writing

>   Assert(exp) if ($debug);

> right in my code. A bit ugly, but it gets the job done. Any other
> suggestions?

In my example above, the $debug variable is part of the Assert package.
So, in your code, you could set ...

  $Assert::debug = 0;
or
  $Assert::debug = 1;

To control whether or not your assertions are virtual no-ops or not.
You still pay the price of a subroutine invocation for each assertion,
however.

Am I missing something, or will my suggestion work?

--
Lloyd Zusman            01234567 <-- The world famous Indent-o-Meter.

   To get my PGP public key automatically mailed to you, please
   send me email with the following string as the subject:
                    mail-request public-key



Tue, 29 Apr 1997 23:09:35 GMT  
 Assocs from linear arrays / assert.pl

:Would the following alternative work?
:
:  package Assert;
:
:  require 5.000;
:  require Exporter;
:
:  require 'assert.pl'; # ... so that &panic() is accessible
:


:
:  $debug = 0;
:
:  sub Assert {
:
:     if ($debug) {
:
:       # Get the caller's package via the 'caller' routine.
:
:       my($package) = caller;
:       eval "package $package";
:

:     }
:  }
:
:  1;

:Am I missing something, or will my suggestion work?
:

Sorry, but that won't work.  Package refers only to static global
namespaces, and has nothing to do with lexical variables the way tcl's
up(var|stack) do.

--tom
--

    Interestingly enough, since subroutine declarations can come anywhere,
    you wouldn't have to put BEGIN {} at the beginning, nor END {} at the
    end.  Interesting, no?  I wonder if Henry would like it. :-) --lwall



Tue, 29 Apr 1997 23:20:11 GMT  
 Assocs from linear arrays / assert.pl

Quote:
:>>>>> daviesb  writes:

:
:# Oh, and while I'm crying for the moon - anyone got an assert.pl?
:
:assert.pl is in the Perl library, but be careful! I've been trying
:to put assertions in my packages in Perl 5 to make them a bit more
:robust, but the following snippet doesn't do what you think:
:
:  package Foo;
:
:  ...
:
:  sub anySub {
:    my $self = shift;
:
:    assert('ref($self) eq "Foo"');
:
:    ...
:  }
:
:Apparently, the string in the assertion gets passwd to the assert()
:subroutine and is eval'd in assert's scope - at that point $self
:looks like an undef'd variable.

Right.

One approach would be for Larry to fix sub{} to do deep binding
(which he intends to do), so you can use:

    assert sub { ref($self) eq 'Foo' };

For now, you'll have to use
    assert ( eval 'sub { ref($self) eq "Foo" }' );

as that's still deeply bound, although if Larry does the
eval'' --> eval{} optimzation again, it'd have to be:

    assert ( eval "sub { ref(\$self) eq 'Foo' }" );

The CPP can help a little here, although it's a lousy macro preprocessor.

--tom
--

"...we will have peace, when you and all your works have perished--and
 the works of your dark master to whom you would deliver us.  You are a
 liar, Saruman, and a corrupter of men's hearts."  --Larry Wall in perl/taint.c, citing Theoden from LOTR



Tue, 29 Apr 1997 22:30:43 GMT  
 Assocs from linear arrays / assert.pl

Quote:
>>>>> daviesb  writes:

# Oh, and while I'm crying for the moon - anyone got an assert.pl?

assert.pl is in the Perl library, but be careful! I've been trying
to put assertions in my packages in Perl 5 to make them a bit more
robust, but the following snippet doesn't do what you think:

  package Foo;

  ...

  sub anySub {
    my $self = shift;

    assert('ref($self) eq "Foo"');

    ...
  }

Apparently, the string in the assertion gets passwd to the assert()
subroutine and is eval'd in assert's scope - at that point $self
looks like an undef'd variable. I just wrote an Assert.pm that
has one wrapper routine - Assert() - and it checks the logical
value of its parameter, then calls the normal panic() routine
in assert.pl:

  package Assert;

  require 5.000;
  require Exporter;

  # put it in the package
  require 'assert.pl';



  sub Assert {
    panic("ASSERTION FAILED:") unless $_[$[];
  }

  1;

Now I call Assert with an actual expresssion, not a string to be
eval'd:

  ...
  Assert(ref($self) eq "Foo");
  ...

Which is eval'd inside the *caller* and the result is passed to
Assert() which checks the logical value (unless $_[$[]) instead
of eval'ing it. The only thing you lose, is that when as assertion
fails, the expression is not printed - but since panic() gives you
a nice traceback, it's not really a big deal for me.

One thing I haven't found a good way to do is to "compile out"
assertions if debug is off. What you really need is a macro
which I haven't though of a way to do in Perl. I'd like 'Assert(exp);'
to evaluate to '_Assert(exp) if ($debug);' but I don't want to
run my scripts through cpp. What I've been doing is just writing

  Assert(exp) if ($debug);

right in my code. A bit ugly, but it gets the job done. Any other
suggestions?

--
David Avraamides
Goldman, Sachs & Co.
(212) 357-1346



Tue, 29 Apr 1997 21:06:51 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. instructor solution manual for Linear dynamic systems and signals by Zoran Gajic

2. math: solve linear equation

3. Linear Algebra, 'modpow', and Math::BigInt

4. Linear Algebra Routines

5. Iterated linear search

6. Matrix or Linear Algebra Libs?

7. New module: Statistics::LTU - Implements linear threshold units

8. Module proposal: Math::LTU (linear threshold units)

9. Assert/Programming by contract,...

10. assert and panic!

11. ANNOUNCE: Sub::Assert v1.10

12. Parameter asserts??

 

 
Powered by phpBB® Forum Software