- having to do with file/directory handles (the notorious File Object Wars :-)
(-X, binmode, closedir, eof, ...)
- regexps (no compiled regexps saved into scalars)
(m, s, split)
- blocks (blocks can be emulated with code refs (closures) to some extent
but this leads to more complicated code *))
(eval, grep, map, sort)
I tried writing a generalised 'reduce' operation of which e.g. sum,
product, min, max, are just special cases. Sort of a cousin to map
or grep. My idea of the interface is
reduce BLOCK, LIST;
where the block would have $a and $b (a la sort()), $a being the result,
initialised to be the first element of the list, $b iterating down
the list, $a changing to be the result of evaluating the block with
$a and $b inside it. At the moment I can do either:
sub reduce1 {
my $e = shift;
my $a = shift;
my $b;
for (@_) {
$b = $_;
$a = &$e($a, $b);
}
$a;
}
reduce1 sub { my ($a, $b) = @_; $a + $b }, @LIST; # yuk! *)
or
sub reduce2 {
my $e = shift;
my $a = shift;
my $b;
for (@_) {
$b = $_;
$a = eval $e;
}
$a;
}
reduce2 '{ $a + $b }', @LIST; # 'blech'.
to do sums but I cannot really have a block
reduce3 { $a + $b }, @LIST;
as an argument. Well, hoping for the sort() magic of $a and $b may be
too much to hope for... the reduce2 is almost in there, the block is
evaluated in the context where $a and $b mean something, the only ugly
thing is the required quoting.
As usual, I am probably missing something...
++jhi;
(-X, binmode, closedir, eof, ...)
- regexps (no compiled regexps saved into scalars)
(m, s, split)
- blocks (blocks can be emulated with code refs (closures) to some extent
but this leads to more complicated code *))
(eval, grep, map, sort)
I tried writing a generalised 'reduce' operation of which e.g. sum,
product, min, max, are just special cases. Sort of a cousin to map
or grep. My idea of the interface is
reduce BLOCK, LIST;
where the block would have $a and $b (a la sort()), $a being the result,
initialised to be the first element of the list, $b iterating down
the list, $a changing to be the result of evaluating the block with
$a and $b inside it. At the moment I can do either:
sub reduce1 {
my $e = shift;
my $a = shift;
my $b;
for (@_) {
$b = $_;
$a = &$e($a, $b);
}
$a;
}
reduce1 sub { my ($a, $b) = @_; $a + $b }, @LIST; # yuk! *)
or
sub reduce2 {
my $e = shift;
my $a = shift;
my $b;
for (@_) {
$b = $_;
$a = eval $e;
}
$a;
}
reduce2 '{ $a + $b }', @LIST; # 'blech'.
to do sums but I cannot really have a block
reduce3 { $a + $b }, @LIST;
as an argument. Well, hoping for the sort() magic of $a and $b may be
too much to hope for... the reduce2 is almost in there, the block is
evaluated in the context where $a and $b mean something, the only ugly
thing is the required quoting.
As usual, I am probably missing something...
++jhi;