This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Perl] emulate Python zip() for Perl
- Loading branch information
Showing
3 changed files
with
154 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use AI::MXNet qw(mx); | ||
use AI::MXNet::TestUtils qw(same reldiff GetMNIST_ubyte GetCifar10); | ||
|
||
sub test_builtin_zip() | ||
{ | ||
is_deeply( | ||
[ AI::MXNet::zip([ 0 .. 9 ], [ 10 .. 19 ]) ], | ||
[ map { [ $_, 10 + $_ ] } 0 .. 9 ]); | ||
is_deeply( | ||
[ AI::MXNet::zip([ 0 .. 9 ], [ 10 .. 19 ], [ 20 .. 29 ]) ], | ||
[ map { [ $_, 10 + $_, 20 + $_ ] } 0 .. 9 ]); | ||
my $over = ListOverload->new(10 .. 19); | ||
is_deeply( | ||
[ AI::MXNet::zip([ 0 .. 9 ], \@$over) ], | ||
[ map { [ $_, 10 + $_ ] } 0 .. 9 ]); | ||
my $tied = ListTied->new(10 .. 19); | ||
is_deeply( | ||
[ AI::MXNet::zip([ 0 .. 9 ], \@$tied) ], | ||
[ map { [ $_, 10 + $_ ] } 0 .. 9 ]); | ||
} | ||
|
||
|
||
test_builtin_zip(); | ||
done_testing(); | ||
|
||
package ListTied { | ||
sub new { | ||
my($class, @list) = @_; | ||
my @tied; | ||
tie @tied, $class, @list; | ||
return \@tied; | ||
} | ||
sub TIEARRAY { | ||
my($class, @list) = @_; | ||
return bless { list => \@list }, $class; | ||
} | ||
sub FETCH { | ||
my($self, $index) = @_; | ||
return $self->{list}[$index]; | ||
} | ||
sub STORE { | ||
my($self, $index, $value) = @_; | ||
return $self->{list}[$index] = $value; | ||
} | ||
sub FETCHSIZE { | ||
my($self) = @_; | ||
return scalar @{$self->{list}}; | ||
} | ||
sub STORESIZE { | ||
my($self, $count) = @_; | ||
return $self->{list}[$count - 1] //= undef; | ||
} | ||
sub EXTEND { | ||
my($self, $count) = @_; | ||
return $self->STORESIZE($count); | ||
} | ||
sub EXISTS { | ||
my($self, $key) = @_; | ||
return exists $self->{list}[$key]; | ||
} | ||
sub DELETE { | ||
my($self, $key) = @_; | ||
return delete $self->{list}[$key]; | ||
} | ||
sub CLEAR { | ||
my($self) = @_; | ||
return @{$self->{list}} = (); | ||
} | ||
sub PUSH { | ||
my($self, @list) = @_; | ||
return push @{$self->{list}}, @list; | ||
} | ||
sub POP { | ||
my($self) = @_; | ||
return pop @{$self->{list}}; | ||
} | ||
sub SHIFT { | ||
my($self) = @_; | ||
return shift @{$self->{list}}; | ||
} | ||
sub UNSHIFT { | ||
my($self, @list) = @_; | ||
return unshift @{$self->{list}}, @list; | ||
} | ||
sub SPLICE { | ||
my($self, $offset, $length, @list) = @_; | ||
return splice @{$self->{list}}, $offset, $length, @list; | ||
} | ||
sub UNTIE { | ||
my($self) = @_; | ||
} | ||
sub DESTROY { | ||
my($self) = @_; | ||
} | ||
} | ||
|
||
package ListOverload { | ||
use overload '@{}' => \&as_list; | ||
sub new { | ||
my($class, @list) = @_; | ||
return bless { list => \@list }, $class; | ||
} | ||
sub as_list { return $_[0]{list} } | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters