in reply to Re: [Perl 6] Generalized shortcutting C<||>?
in thread [Perl 6] Generalized shortcutting C<||>?

map { some_condition ? $_ : some_default } some_expression;

Very good argument!

The syntax and operand order is confusingly dissimilar to the exiting ternary operator.

Pointless argument: why should it be similar to it? And how is that confusing? It's nonconfusingly similar to C<||> and C<//>. OTOH, while I cherish your map solution, which probably is actually the best one in 5's realms, if you look at it as a whole, it is confusingly dissimilar from those operators, with the value being inspected on the extreme right and the default buried in the block, not to talk of an "unnecessary" C<? $_ :. The right comparison if I get it right should be between:

map { some_condition ?? $_ !! some_default }, some_expression; # and some_expression ||| { some_condition }, some_default;

Replies are listed 'Best First'.
Re^3: [Perl 6] Generalized shortcutting C<||>?
by Roy Johnson (Monsignor) on Jul 26, 2007 at 17:32 UTC
    In Perl5, this is roughly what you want as a function, in terms of argument ordering and short-circuiting.
    sub cop { local $_ = shift; my ($cond, $default) = @_; $cond->() ? $_ : $default->(); } for my $test (2..4) { print "$test: ", cop($test, sub {$_ == 3}, sub { 'This is a default' + }), "\n"; }
    I suspect Perl6 would make it more appetizing.

    Caution: Contents may have been coded under pressure.