FAQ
My question is: could I write below code in perl6:
my @a = <1 2 3 4>; my @b[2]; for @a ->
@b {;} # 2 loops like for @a -> $b[0],$b[1]{;}
my @a = <1 2 3 4>; my @b; for @a -> @b
{;} # 1 loop
my @a = <1 2 3 4>; my @b[2];
grep(@b){say 'yes' if +@b>3} <== @a; # 2 loops like
grep {$^a+$^b >3} <== @a;
my @a = <1 2 3 4>; grep{say 'yes' if +@_
=10} <== @a; # slurp
Since memory is cheaper and cheaper, bulky feed can maximize to make use of
memory and reduce loops.
Any thoughts?

Search Discussions

  • Yary at Jul 9, 2009 at 4:29 pm

    On Wed, Jul 8, 2009 at 8:45 PM, Xiao Yafeng wrote:

    Any thoughts?
    First let's fix the whitespace in your post so it's easier to read-

    My question is: could I write below code in perl6:

    # 2 loops like for @a -> $b[0],$b[1] {;}
    my @a = <1 2 3 4>; my @b[2]; for @a ->@b {;}

    my @a = <1 2 3 4>; my @b; for @a -> @b{;} # 1 loop

    # 2 loops like grep {$^a+$^b >3} <== @a;
    my @a = <1 2 3 4>; my @b[2];
    grep(@b){say 'yes' if +@b>3} <== @a;

    # slurp
    my @a = <1 2 3 4>; grep{say 'yes' if +@_>=10} <== @a;

    One style point, generally a list of numbers is written as
    my @a = 1..4; # list of numbers, <1 2 3 4> is a list of strings
    my @a = '1'..'4' # equivalent to <1 2 3 4>

    As for your original question, I am not quite sure what you're asking. Do
    you want a simpler way to write
    for 一..四 {say "$^x $^y";}
    一 二
    三 四
    (... which assumes that unicode sequencing is working, I can't test that on
    my machine!)

    Or do you want something like-
    for <yuht! yee sahm say> -> $a {for <eee arr> ->$b {say "$a $b"}}

    Or using more memory and less looping (at least superficially!)
    for 'a'..'d' X <A Z> {say "$^p $^q"};

    In other words, if you can write some code that runs and tell us how you'd
    like to simplify it, I might be able to answer better.
  • Xiao Yafeng at Jul 10, 2009 at 5:49 am
    On Fri, Jul 10, 2009 at 12:29 AM, yary wrote:
    On Wed, Jul 8, 2009 at 8:45 PM, Xiao Yafeng wrote:

    Any thoughts?
    First let's fix the whitespace in your post so it's easier to read-

    My question is: could I write below code in perl6:

    # 2 loops like for @a -> $b[0],$b[1] {;}
    my @a = <1 2 3 4>; my @b[2]; for @a ->@b {;}

    my @a = <1 2 3 4>; my @b; for @a -> @b{;} # 1 loop

    # 2 loops like grep {$^a+$^b >3} <== @a;
    my @a = <1 2 3 4>; my @b[2];
    grep(@b){say 'yes' if +@b>3} <== @a;

    # slurp
    my @a = <1 2 3 4>; grep{say 'yes' if +@_>=10} <== @a;

    One style point, generally a list of numbers is written as
    my @a = 1..4; # list of numbers, <1 2 3 4> is a list of strings
    my @a = '1'..'4' # equivalent to <1 2 3 4>

    As for your original question, I am not quite sure what you're asking. Do
    you want a simpler way to write
    for 一..四 {say "$^x $^y";}
    一 二
    三 四
    (... which assumes that unicode sequencing is working, I can't test that on
    my machine!)

    Or do you want something like-
    for <yuht! yee sahm say> -> $a {for <eee arr> ->$b {say "$a $b"}}

    Or using more memory and less looping (at least superficially!)
    for 'a'..'d' X <A Z> {say "$^p $^q"};

    In other words, if you can write some code that runs and tell us how you'd
    like to simplify it, I might be able to answer better.
    What I thought is reducing loop times.
    In some case, we don't need pop a item from a list one by one. Acoording to
    memory capacity of machine, we can get more than one items from the list.
    for a simple example,
    for 1..1000000 -> $x {
    func1($x) if $pid = fork;
    }
    above code will loop 1000000 times, fork 1000000
    processes and waste most of memory on machine.
    if I can say,
    my @b[10000]; for 1..1000000 -> @b{
    func2(@b) if $pid = fork;
    }
    IMHO, preceding code will be better in term of memory
    utilization.
  • Yary at Jul 10, 2009 at 5:18 pm
    I understand now. Given a large list, you'd like to assign chunks of
    the list to an array, easily, while looping. In other words, you're
    looking for a way to abbreviate this:

    my $chunk_size=10_000;
    my @big='aaaa'..'mnop';
    for ^@big :by $chunk_size {
    my @chunk=@big[$_..($_+$chunk_size,@big.end).min]
    ... # Do stuff with @chunk
    }

    I'm a perl6 novice so there probably is a more elegant way to write
    the above already.

    From your original email, I like this idea-
    for 'aaaa'..'mnop' -> *@chunk[10_000] { ... } # @chunk is 10000 elems or less
    though I think the syntax has to change a bit. I used the "*@" sigil
    because "chunk" is slurpy, but only up to 10,000 items. Wrong on a
    couple levels?

    By the way, you shouldn't predeclare "@chunk" as you did in the
    original example-
    my @a = <1 2 3 4>; my @b[2]; for @a ->@b {;}
    the @b in the pointy block is a new declaration that shadows the
    earlier "my @b[2]" declaration.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupperl6-language @
categoriesperl
postedJul 9, '09 at 3:45a
activeJul 10, '09 at 5:18p
posts4
users2
websiteperl6.org

2 users in discussion

Xiao Yafeng: 2 posts Yary: 2 posts

People

Translate

site design / logo © 2021 Grokbase