CY's Take on PWC#088

Task 1 Array of Product

sub myproduct {
    my @arr = @_;
   …

CY's Take on PWC#087

If you want to challenge yourself on programming, especially on Perl and/or Raku, go to https://perlweeklychallenge.org, code the latest challenges, submit codes on-time (by GitHub or email).

After the long-haul Sudoku Task, this week we come to meet two tiny tasks.

Task 1 Longest Consecutive Sequence

It seems unavoidable for me that we have to sort the input first:

sub long_consec{
    my…

CY's Take on PWC#086

pascal_userinterface.jpg# (I will write more some hours later. This may not be a good practice. But) #HKT November 15, 2020 3:32 PM

I am excited by the Sudoku task and eager to share.

If you want to challenge yourself on…

CY's Take on PWC#083

If you want to challenge yourself on programming, especially on Perl and/or Raku, go to https://perlweeklychallenge.org, code the latest challenges, submit codes on-time (by GitHub or email).

... before coding

Long time no blogging for The Weekly Challenge!

I found that I use "and/or" quite frequently in writing. I know, (mathematical-)logically we only need "or". It seems to me to be a language tricky part as the use of gender neutral terms.

I made a logical error for the "Lonely X" task in c…

OO linked lists in Perl

After many days, trying to implement linked lists by nested hash (link to Rosetta Code) (link to my code) or Struct::Dumb, I get how to write the (singly) linked list in object-oriented style by Perl. One with bless, another one with Moose. Keep the learning record here.

Updated: See the link in comment section of Tobyink, a showcase of his OO module Zydeco. Thanks Toby!

#reference: “Object Oriented Perl” by Damian Conway 

use strict;
use warnings;

{
package SLL::Node;
use strict;

=pod
sub new {
    my $class = $_[0];
    my $objref = {
        _value => $_[1],
        _nextnode => $_[2],
    };
    bless $objref, $class;
    return $objref;
}

=cut
sub new {
    my ($class) = @_;
    bless {
        _value => $_[1],
        _nextnode => $_[2],
    }, $class;
}


sub value { $_[0]->{_value} }

sub nextnode { $_[0]->{_nextnode} }

}


my $node_a = SLL::Node->new(30undef);
my $node_b = SLL::Node->new(40, \$node_a);
my $node_c = SLL::Node->new(70, \$node_b);

my $node_head = SLL::Node->new(undef, \$node_c);

my $node = $node_head;

do {
    $node = ${$node->nextnode};
    print $node->value ;
while ($node->nextnode);

print \n;

#print 70 40 30


#reference: various resources on Moose

use strict;
use warnings;

package Node {
    use Moose;

    has 'value', is => 'ro', isa => 'Int';
    has 'nextnode', is => 'rw', isa => 'Any';

    sub printvalue { 
        my $self = shift;
        print $self->value , " ";
    }

    sub go_next {
        my $self = shift;
        return $self->nextnode;
    }

}



my $node_a = Node->new(value => 30, nextnode=> undef);
my $node_b = Node->new(value => 40, nextnode => $node_a);
my $node_c = Node->new(value => 70, nextnode => $node_b);

my $node_head = Node->new(value => 0,nextnode=> $node_c);

my $node = $node_head;

while ($node = $node->go_next ) {
     $node->printvalue;
}

print "\n";
#print 70 40 30