Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.
I feel sorry for the poor fools who don't have anonymous subroutines available in their language. I have code that has to produce "Hi" and "Lo" bandwidth links, but we won't always have both available. If we only have one, we print that link. If we have both, we separate the links with a pipe. My ugly code was something like this:
if ($hi) {
$li_text.= qq'<a href="$url$hi">Hi</a>';
}
if ($hi && $lo) {
$li_text.= ' | ';
}
if ($lo) {
$li_text.= qq'<a href="$url$lo">Lo</a>';
}
That's just ugly, so I rewrote it:
my $link = sub { $_[0]? qq'<a href="$url$_[0]">$_[1]</a>' : () };
$li_text.= join ' | ' => $link->($hi, 'Hi'), $link->($lo, 'Lo');
Much nicer and easier to maintain.
A good map would have worked there (Score:2)
Re:A good map would have worked there (Score:1)
Re:A good map would have worked there (Score:2)
Re:A good map would have worked there (Score:1)
Okay, but it’s still an anonymous code block being passed to
map. A language which does not support anonymous function is unlikely to support that idiom too.Re:A good map would have worked there (Score:2)
I view the syntax of map as an entity... it has a block that evaluates for its last expression evaluated, and a list of values. You're not "passing anything" to map, unless you view every single function and operator as "passed to".
Re: (Score:1)
To me,
foreachis a control structure, whereasmapis not.Per the lisper’s view,
mapis a function that transforms a list by applying a function to each of its elements.Per the pragmatic view,
mapis documented inperlfunc, whereasforeachis documented inperlsyn.Re:A good map would have worked there (Score:1)
$li_text .= join " | ", map { $_->[1] ? "<a href='$url$_->[0]'>$_->[1]</a>" : () } [Lo => $lo], [Hi => $hi];
Re: (Score:1)
Re: (Score:2)
Yes, you can do it that way, but I think the examples that both merlyn and I posted were cleaner. Ours were more functional in nature and require less maintenance. For example, let's say that the client wante the delimiter changed in to "<->". You now have two places in your code to change instead of one. That final substr you use is an example of "synthetic" code (code that's used to solve a problem in the programming language rather than solving the actual problem.) The more synthetic code in a
Re: (Score:1)
Maybe I didn’t make myself clear about the constant. I meant that it would define the separator in a constant and its length in a derived constant so that you’d only need to change one of them. Then all I need to change is the separator constant and the final string shortening call automatically works as intended.
As far as the functional nature of the idioms you and merlyn wrote is concerned, that’s true, but I didn’t claim otherwise. I did say the code I wrote was C-ish, remember?
Re: (Score:1)
Also, as an excuse to brush up my C, the C-ish Perl rendered in actual C:
Re: (Score:2)
Fair enough about the constants. If one's derived from the other, I can't complain too much :)
Re: (Score:1)
You know, after the monstrosity I had to write to emulate this in C, I think I’d stick with your ugly initial version and avoid duplication simply by putting the format string in a constant. Although the C version would have more duplication than the link building due to all the malloc scaffolding. Ugh. C’s not the language to do heavy string lifting with (unless you really need to go fast fast fast ).
Re: (Score:1)
don't forget join()!! (Score:1)
Re:don't forget join()!! (Score:2)
That reminds me of one VBScript job I had where I was so irritated by its awful array manipulation functions that I wrote push(), pop(), shift(), unshift() and sort(). My coworkers were mystified by my code, but they admitted that it worked well.