Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

As an excersise in learning "tie", I knocked up this series generator. I know this is probably not the ideal solution to the problem of generating series, but it was "tie" I was interested in.

I would very much apreciate your comments on my implementation.

Series.pm

package Series; use strict; sub TIEARRAY { my $pkg = shift; my $rsub = shift; # sub for formula my @vals = @_; bless { 'values' => [ @vals ], 'next' => $rsub }, $pkg; } sub FETCHSIZE { # rather meaningless in context, but gets called a lot # presume it's to check that any values at all exist return 1; # for now } sub FETCH { die "can't get subscripted value of series"; } sub SHIFT { my ($obj) = @_; my $ra = $obj->{'values'}; push(@$ra, $obj->{'next'}->(@$ra)); return shift @$ra; } 1;

series.pl

#!/usr/bin/perl -w use strict; use Series; print "Arithmetic Series: 1,4,7, ... N_{i-1}+3 ...\n"; tie my @arith, 'Series', sub { return 3 + shift }, 1; print shift(@arith)." " for (1..15); print "\n\n"; print "Geometric Series: 1,2,4, ... N_{i-1}*2 ...\n"; tie my @geo, 'Series', sub { return 2 * shift }, 1; print shift(@geo)." " for (1..15); print "\n\n"; print "Combination Series: 2,7,17, ... N_{i-1}*2+3 ...\n"; tie my @combo, 'Series', sub { return 3 + 2 * shift }, 2; print shift(@combo)." " for (1..15); print "\n\n"; print "Fibonacci Numbers: 0,1,1,2,3, ... N_{i-1}+N_{i-2} ...\n"; tie my @fibo, 'Series', sub { my @n = @_; return $n[0]+$n[1] }, 0,1; print shift(@fibo)." " for (1..15); print "\n";

And here is the output:

Arithmetic Series: 1,4,7, ... N_{i-1}+3 ... 1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 Geometric Series: 1,2,4, ... N_{i-1}*2 ... 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 Combination Series: 2,7,17, ... N_{i-1}*2+3 ... 2 7 17 37 77 157 317 637 1277 2557 5117 10237 20477 40957 81917 Fibonacci Numbers: 0,1,1,2,3, ... N_{i-1}+N_{i-2} ... 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

--
Brother Marvell

Edit: chipmunk 2001-05-16


In reply to infinite series by marvell

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (16)
    As of 2015-07-28 15:06 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (257 votes), past polls