From: Sergei S. <ser...@ya...> - 2007-03-18 23:08:43
|
--- Eric Wilhelm <scr...@gm...> wrote: > # from Sergei Steshenko > # on Sunday 18 March 2007 02:15 pm: > > >funky_array => [1, 2, 3, 5, 8 ...], > ># element is the some of previous two, > ># and this way 100 times; > > >So, this is Perl, and there is more than one to do it, and I would do > > it this way: > > > >{ > >... > >funky_array => do{ > > my @tmp_array; # no name clash danger, the scope > > # protects from name contention > > > > my $number_of_times = 100; # I said 100 times, dodn't > > I ? > > > > my $previous_element = 0; > > my $element = 1; > > > > while($number_of_times-- > 0) > > { > > $element += $previous_element; > > push @tmp_array, $element; > > $previous_element = $element; > > } > > > > \@tmp_array; # the array reference to be returned > > } > >}; > > > >So, the point of this example is that there should be > > canonical/normalized data structure and there can be infinite number > > of ways to produce it. > > Fine. But, you should be able to skim over the code in much less space. > When you want to build something based on a rolling pair of values, why > not use a rolling pair of values? > > ... > funky_array => do {my @p = (1,2); > [1,2,map({push(@p, my $v = shift(@p)+$p[0]); $v} 3..100)]}, > ... Why should I load my brain trying to understand what $p means ? And why should help those claiming Perl is unreadable ? And what is $v if I may ? I prefer readable code, not a short one. I remember a few days ago a colleague of mine was saying "'i' is current and 'j' is previous". And the colleague had a real difficulty debugging her own code. And I wanted to ask her "Why did you give your variables meaningless names in the first place ?". I wouldn't have written such a code in the first place - in my code previous is previous and current is current. Sorry. --Sergei. Applications From Scratch: http://appsfromscratch.berlios.de/ ____________________________________________________________________________________ Need Mail bonding? Go to the Yahoo! Mail Q&A for great tips from Yahoo! Answers users. http://answers.yahoo.com/dir/?link=list&sid=396546091 |