Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: 99 Problems in Perl6

by Util (Priest)
on Dec 17, 2006 at 01:09 UTC ( [id://590247]=note: print w/replies, xml ) Need Help??


in reply to 99 Problems in Perl6

Here are my solutions to Problem 9. The first is concise and looks fairly efficient. The second is less straightforward; I wanted to solve it with one big map, in the hopes of allowing it to work lazily. I don't know how to tell if I succeeded, though.

Code tested in Pugs:

sub p09_group_Util_1 (*@list) { my @ret; for @list -> $val { push @ret, [] if !@ret or $val !~~ @ret[-1][-1]; push @ret[-1], $val; } return @ret; } sub p09_group_Util_2 (*@list) { my @tmp; return map -> $index, $val { my @ret; if !@tmp or $val !~~ @tmp[-1] { @ret = [ @tmp ] if @tmp; undefine @tmp; } push @tmp, $val; push @ret, [ @tmp ] if @tmp and $index == @list.end; @ret; # May contain 0, 1, or 2 arrayrefs. }, @list.kv; } my @data = <a a a a b c c a a d e e e e>; say '9.1 ', p09_group_Util_1(@data).perl; say '9.2 ', p09_group_Util_2(@data).perl; # When dumped via .perl, Util_1 is wrapped in [], and Util_2 in (). # When dumped via .yaml, Util_1 and Util_2 are identical. Bug?
Output:
9.1 [["a", "a", "a", "a"], ["b",], ["c", "c"], ["a", "a"], ["d",], ["e +", "e", "e", "e"]] 9.2 (["a", "a", "a", "a"], ["b",], ["c", "c"], ["a", "a"], ["d",], ["e +", "e", "e", "e"])

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://590247]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-18 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found