Log in

No account? Create an account
Short-circuiting the invocant - brad's life — LiveJournal [entries|archive|friends|userinfo]
Brad Fitzpatrick

[ website | bradfitz.com ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Short-circuiting the invocant [May. 6th, 2006|10:43 pm]
Brad Fitzpatrick

Before it was necessarily refactored, I had the honor of making Artur "Whoa, will that work? Oh, of course it will. Hah." at the following config parsing code:
($plugin || $vhost || $server)->set_config_option($key, $value);
I'm sad it had to go away. :-)

[User Picture]From: scosol
2006-05-07 08:26 am (UTC)
again, i can not read anyone else's perl-
(Reply) (Thread)
[User Picture]From: pfig
2006-05-07 09:37 am (UTC)
why oh why must such beauty be refactored? :)
(Reply) (Thread)
(Deleted comment)
(Deleted comment)
[User Picture]From: jimtbari
2006-05-08 12:41 am (UTC)
First object that's not false, not first that's not undef. (See discussion of // elsewhere). Of course, in this instance it probably doesn't make any difference, though if one were really twisted it could.
(Reply) (Parent) (Thread)
[User Picture]From: gaal
2006-05-07 10:19 am (UTC)
Heh. Semi-related favorites are lvalue ?: and Perl 6 [//] .
(Reply) (Thread)
[User Picture]From: brad
2006-05-07 12:11 pm (UTC)
I need // every day.
(Reply) (Parent) (Thread)
[User Picture]From: gaal
2006-05-07 12:31 pm (UTC)
I think 5.10 is supposed to have it and it's already in bleadperl.
(Reply) (Parent) (Thread)
[User Picture]From: grumpy_sysadmin
2006-05-07 03:53 pm (UTC)
Oh, hey! I'd forgotten ?: could be an lvalue in Perl.
(Reply) (Parent) (Thread)
From: dan_erat
2006-05-07 06:57 pm (UTC)
What does "//" do? (Is that just an empty regexp?)

I have no idea how to convince Google to give me search results for "//". Yahoo and MSN appear to also be useless for queries containing punctuation, which makes me happy even though it shouldn't. :(
(Reply) (Parent) (Thread)
From: (Anonymous)
2006-05-07 10:34 pm (UTC)
It's "defined or", pronounced "err".

$var //= 18;
#is the same as
defined($var) or $var = 18;

A much needed addition, yes.
(Reply) (Parent) (Thread)
[User Picture]From: gaal
2006-05-08 04:58 am (UTC)
// is err, like the other person said. [//] is a reduce version of it, so that
  # hash lookup looks like this now
  $file = [//] %Cmdline<input-file>, %Configfile<input-file>, $DEFAULT_INPUT_FILE;
means the same as
  $file = %Cmdline<input-file> // %Configfile<input-file> // $DEFAULT_INPUT_FILE;
which means the same as
  # ?: is spelled ??!! in Perl 6
  $file = defined %Cmdline<input-file> ?? %Cmdline<input-file> !!
          defined %Configfile<input-file> ?? %Configfile<input-file> !!

[//] isn't hardcoded; [] is a general purpose reduce metaoperator. [+] @list means sum, [*] @list means product, and so on.
(Reply) (Parent) (Thread)
[User Picture]From: osi
2006-05-07 01:25 pm (UTC)
heh, a similar construct works in javascript, its grand.
(Reply) (Thread)
[User Picture]From: avatraxiom
2006-05-07 02:40 pm (UTC)
That's either awesome or frightening. Or awesomely frightening.

(Reply) (Thread)
From: dan_erat
2006-05-07 03:39 pm (UTC)
On a somewhat related topic, I frequently try to do the following in Ruby and am always disappointed:

(a['foo'] ||= 0) += 3

The solution is to just use Hash.new(0) so that values will be initialized to 0, but I think I still prefer assignment to return lvalues, as in Perl.
(Reply) (Thread)
[User Picture]From: gaal
2006-05-07 05:50 pm (UTC)
For this particular case, Perl DWIMs further: $a{foo} += 3 will not emit a warning when that element was not defined.
(Reply) (Parent) (Thread)
[User Picture]From: 2shortplanks
2006-05-07 05:35 pm (UTC)
You just made me thing of using first to do more complicated versions.

use Scalar::Util qw(blessed);
use List::Util qw(first);

(first { blessed($_) && $_->can("wibble") } $thingy, $doofer, $wosit)->wibble();

I'm not sure if this is a good or bad idea.

(Reply) (Thread)
[User Picture]From: flipzagging
2006-05-07 07:41 pm (UTC)
This would get you assassinated by the Design Patterns mafia at my current job.

It's been interesting learning all about their brand of wisdom, but I miss Perl.
(Reply) (Thread)