in reply to An infix fix
Again, the little bit of yin-yang.sub AND { my $last; first {!($last = $_)} @_ or $last; } sub OR { my $last; first {($last = $_)} @_ and $last; }
I note that the short-circuiting you mention isn't quite as good as the short-circuiting you get with the actual operators, since each element of the list will be evaluated prior to calling the function. So you don't have to traverse the entire list, but any function calls and assignments within the list will be executed.
I note also that you're just exchanging the && operator for a comma (plus the function calling semantics). I had thought that overloading the comma operator would be a clever alternative, but it turns out not to be overloadable.
Our functional-programming brethren might prefer the tail-recursive
Update: tail-recursive ones were buggy.sub OR { @_ > 1 ? shift || OR(@_) : shift; } sub AND { @_ > 1 ? shift && AND(@_) : shift; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: An infix fix
by tlm (Prior) on Mar 22, 2005 at 21:56 UTC | |
by Roy Johnson (Monsignor) on Mar 22, 2005 at 22:59 UTC | |
by tlm (Prior) on Mar 22, 2005 at 23:37 UTC | |
by Roy Johnson (Monsignor) on Mar 23, 2005 at 00:13 UTC | |
by tlm (Prior) on Mar 22, 2005 at 23:05 UTC | |
by Roy Johnson (Monsignor) on Mar 22, 2005 at 23:30 UTC |