Every story and comment from the Hacker News API, in a layout borrowed from 2001.
A comment on Why it’s hard for programmers to write a program to flatten a list?. Parent comment
sub flatten { my @in = @{$_[0]}; my @out; while (@in) { my $item = shift @in; if (ref $item eq 'ARRAY') { unshift @in, @$item; } else { push @out, $item; } } return \@out; }
The main difference is that I opted to manipulate a copy of the source list in place to expand sublists rather than recurse.
As others have noted, a recursive version using map can be written much more concisely.
There are no comments on this comment yet.
The main difference is that I opted to manipulate a copy of the source list in place to expand sublists rather than recurse.
As others have noted, a recursive version using map can be written much more concisely.