Browse files

Playing with Kioku pipes

  • Loading branch information...
1 parent 8f0e911 commit c2ee2a20cb9352a81d8829bc13ca9c1fad4aece4 hakim committed Jul 13, 2010
View
31 scratch/perl/kiokupipe/lib/List.pm
@@ -0,0 +1,31 @@
+package List;
+use KiokuDB::Class;
+
+has 'head' => (
+ is => 'ro',
+ isa => 'Any',
+);
+
+has 'tail' => (
+ traits => ['KiokuDB::Lazy'],
+ is => 'ro',
+ isa => 'Maybe[List]',
+);
+
+sub from_array {
+ my $self = shift;
+ my $class = (ref $self) || $self;
+ if (@_) {
+ my $head = shift;
+ my $list = $class->new({
+ head => $head,
+ tail => scalar $class->from_array(@_),
+ });
+ return $list;
+ }
+ else {
+ return;
+ }
+}
+
+1;
View
38 scratch/perl/kiokupipe/lib/List/Map.pm
@@ -0,0 +1,38 @@
+package List::Map;
+use KiokuDB::Class;
+
+extends 'List';
+
+has 'list' => (
+ is => 'ro',
+ isa => 'List',
+);
+
+has '+head' => (
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ $self->transform( $self->list->head ),
+ },
+);
+
+has '+tail' => (
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ my $class = (ref $self) || $self;
+ if (my $tail = $self->list->tail) {
+ return $class->new( list => $tail );
+ }
+ else {
+ return;
+ }
+ },
+);
+
+sub transform {
+ my ($self, $val) = @_;
+ return $val; # id
+}
+
+1;
View
11 scratch/perl/kiokupipe/lib/List/Map/Increment.pm
@@ -0,0 +1,11 @@
+package List::Map::Increment;
+use KiokuDB::Class;
+
+extends 'List::Map';
+
+sub transform {
+ my ($self, $val) = @_;
+ return $val+1;
+}
+
+1;
View
19 scratch/perl/kiokupipe/test.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/perl
+
+use List;
+use List::Map::Increment;
+use Data::Dumper;
+
+my $list = List->from_array(1..10);
+
+my $map = List::Map::Increment->new( list => $list );
+
+local $Data::Dumper::Indent = 1;
+local $Data::Dumper::Maxdepth = 4;
+warn Dumper($list, $map);
+
+while ($map) {
+ warn $map->head;
+ $map = $map->tail;
+}
+

0 comments on commit c2ee2a2

Please sign in to comment.