Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

A mini-language for sequences (part 1)

by tmoertel (Chaplain)
on Nov 05, 2004 at 18:15 UTC ( [id://405570]=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
        #!/usr/bin/perl
    
        use warnings;
        use strict;
    
  2. or download this
        package Sequences;
    
    ...
        sub seqsub(&) {
            Sequences->new(@_);
        }
    
  3. or download this
        sub seq {
            my ($i, $elems) = (0, \@_);
    ...
                    : do { $i = 0; () };
            }
        }
    
  4. or download this
        my $abcees = seq("a", "b", "c");
    
    ...
        $abcees->();  # ("c")
        $abcees->();  # (   )
        # ... the cycle repeats ...
    
  5. or download this
        use Data::Dumper;
    
    ...
            }
            $seq;
        }
    
  6. or download this
        enumerate( $abcees );
    
        #  0 => a
        #  1 => b
        #  2 => c
    
  7. or download this
        $abcees->enumerate;
    
        #  0 => a
        #  1 => b
        #  2 => c
    
  8. or download this
        sub seq_prod2 {
            my ($s, $t) = @_;
    ...
                ( @sval, @tval );
            }
        };
    
  9. or download this
        my $one_two_threes = seq( 1 .. 3 );
    
    ...
        #  6 => c 1
        #  7 => c 2
        #  8 => c 3
    
  10. or download this
        use List::Util qw( reduce );
    
        sub seq_prod {
            reduce { seq_prod2($a,$b) } @_ ;
        }
    
  11. or download this
        my $you_and_mees = seq( "you", "me" );
    
    ...
        # 15 => c 2 me
        # 16 => c 3 you
        # 17 => c 3 me
    
  12. or download this
        my (@alist, @blist, @clist);
        # ... initialize arrays with values ...
    ...
                }
            }
        }
    
  13. or download this
        my $combined_sequence =
            seq_prod( seq(@alist), seq(@blist), seq(@clist) );
    ...
        while ( my ($a, $b, $c) = $combined_sequence->() ) {
            # do something with ($a, $b, $c)
        }
    
  14. or download this
        sub seqs {
            map seq(@$_), @_;
    ...
        sub seq_from_spec {
            seq_prod( seqs(@_) );
        }
    
  15. or download this
        sub nary_digits {
            my ($base, $digits) = @_;
    ...
        #  5 => 1 0 1
        #  6 => 1 1 0
        #  7 => 1 1 1
    
  16. or download this
        # seq_from_spec([1..3])       === seq(1..3)
        # seq_from_spec([1..3],[4,5]) === seq(1..3) x seq(4,5)
        # seq_from_spec(\(@a,@b,...)) === seq(@a) x seq(@b) x ...
    
  17. or download this
        sub seq_foreach {
            my ($seq, $fn) = @_;
    ...
            }
            $seq;
        }
    
  18. or download this
        $abcees->seq_foreach( sub { print "@_\n"; } );
        # a
        # b
        # c
    
  19. or download this
        sub seq_foreach_from_spec {
            my ($spec, $fn) = @_;
            seq_foreach( seq_from_spec( @$spec ), $fn );
        }
    
  20. or download this
        seq_foreach_from_spec( [\(@alist, @blist, @clist)], sub {
            my ($a, $b, $c) = @_;
            # do something with $a, $b, $c, ...
        });
    
  21. or download this
        sub seq_filter {
            my ($seq, $filter_fn) = @_;
    ...
                return @val;
            }
        }
    
  22. or download this
        sub odds_up_to {
            my $maximum = shift;
    ...
        #  2 => 5
        #  3 => 7
        #  4 => 9
    
  23. or download this
        sub seq_map {
            my ($seq, $fn) = @_;
    ...
                @val ? $fn->(@val) : ();
            }
        }
    
  24. or download this
        sub evens_up_to {
            odds_up_to( $_[0] + 1 )
    ...
        #  3 => 6
        #  4 => 8
        #  5 => 10
    
  25. or download this
        sub min_length_combinations {
            my ($min_length, @inputs) = @_;
    ...
                ->seq_map( sub { [ map @$_, @_ ] } )
                ->seq_filter( sub { @{$_[0]} >= $min_length } )
        }
    
  26. or download this
        min_length_combinations(
            4, map [split//], qw( abc de fgh i jk l m )
    ...
        # 863 => ['c','e','h','i','k','m']
        # 864 => ['c','e','h','i','k','l']
        # 865 => ['c','e','h','i','k','l','m']
    
  27. or download this
        sub seq_series {
            my $seqs = seq( @_ );  # seq of seqs (!)
    ...
        #  3 => 1
        #  4 => 2
        #  5 => 3
    
  28. or download this
        sub seq_reset {
            my $seq = shift;
    ...
                return @outvals;
            }
        }
    
  29. or download this
        seq_zip( $abcees, $one_two_threes )->enumerate;
    
    ...
    
        #  0 => a 1 you
        #  1 => b 2 me
    
  30. or download this
        sub seq_zip_with {
            my $zipper_fn = shift;
            seq_map( seq_zip(@_), $zipper_fn );
        }
    
  31. or download this
        # some math helpers
    
    ...
        #  2 => 24
        #  3 => 60
        #  4 => 120
    
  32. or download this
        my @site1 = qw( AATKKM aatkkm );
        my @site2 = qw( GGGGGG gggggg );
    ...
        #   'A g' => 2, 'M G' => 1, 'k g' => 2, 'k G' => 2,
        #   'T G' => 1, 'a G' => 2, 'm G' => 1, 't G' => 1,
        #   'K g' => 2, 'M g' => 1, 't g' => 1, 'T g' => 1 }
    
  33. or download this
        sub seq_values {
            my $seq = shift;
    ...
            seq_foreach( $seq, sub { push @values, @_ } );
            return @values;
        }
    
  34. or download this
        print Dumper( [ seq(1..3)->seq_values ] ), "\n";
        # [[1],[2],[3]]
    
        print Dumper( [ seq(1..3)->seq_values_scalar ] ), "\n";
        # [1,2,3]
    
  35. or download this
        sub matrix_transpose {
            my $rows  = shift;
    ...
        print Dumper( matrix_transpose( $matrix ) ), "\n";
        # [ [0,2,4]
        # , [1,3,5] ]
    
  36. or download this
        sub seq_fold {
            my ($seq, $fn) = @_;
    ...
            }
            wantarray ? @accum : $accum[0];
        }
    
  37. or download this
        sub dot_product {
            seq_zip_with( \&product, seqs(@_) )
    ...
    
        print dot_product( [1,1,1], [1,2,3] ), "\n";
        # 6
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://405570]
Approved by Limbic~Region
Front-paged by Limbic~Region
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-19 11:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found