From: Eric W. <scr...@gm...> - 2007-03-18 22:17:43
|
# from Sergei Steshenko # on Sunday 18 March 2007 02:15 pm: >funky_array =3D> [1, 2, 3, 5, 8 ...], ># element is the some of previous two, ># and this way 100 times;=20 >So, this is Perl, and there is more than one to do it, and I would do > it this way: > >{ >... >funky_array =3D> do{ >=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0my @tmp_array; =A0# no name clash dange= r, the scope >=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# =A0pr= otects from name contention > >=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0my $number_of_times =3D 100; # I said 1= 00 times, dodn't > I ? > >=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0my $previous_element =3D 0; >=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0my $element =3D 1; > >=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0while($number_of_times-- > 0) >=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0{ >=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$element +=3D $previous_element; >=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0push @tmp_array, $element; >=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$previous_element =3D $element; >=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} > >=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0\@tmp_array; # the array reference to b= e returned >=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} >}; > >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. =46ine. But, you should be able to skim over the code in much less space. = =20 When you want to build something based on a rolling pair of values, why=20 not use a rolling pair of values? ... funky_array =3D> do {my @p =3D (1,2); [1,2,map({push(@p, my $v =3D shift(@p)+$p[0]); $v} 3..100)]}, ... And once you know it is correct, you don't have to read it. Of course,=20 I would put that in a method call and maybe even test that it gave the=20 correct answer. And as far as understanding what it does. With a little examination, it=20 is fairly clear that it doesn't do: ... funky_array =3D> [map({2**$_} 0..99)], ... Thanks for helping to illustrate my point. More code means more bugs. =20 Individual line complexity really doesn't mean much. Bug density has=20 been shown to be roughly a constant proportion of SLOC across multiple=20 languages. =2D-Eric =2D-=20 perl -e 'srand; print join(" ",sort({rand() < 0.5} qw(sometimes it is important to be consistent)));' =2D-------------------------------------------------- http://scratchcomputing.com =2D-------------------------------------------------- |