Skip to content

Instantly share code, notes, and snippets.

@Ovid

Ovid/primes.p6 Secret

Created October 25, 2015 20:29
Show Gist options
  • Save Ovid/92ba0af93988f2c6e560 to your computer and use it in GitHub Desktop.
Save Ovid/92ba0af93988f2c6e560 to your computer and use it in GitHub Desktop.
Why always in the same order?
my @promises = gather for 2000, 4000, 5, 100 -> $nth {
take start { (1..Inf).grep(*.is-prime)[$nth] };
}
.say for await @promises;
# Takes 15 seconds and always in the same order.
# 17393
# 37831
# 13
# 547
@zengargoyle
Copy link

while @promises {                                                               
  await Promise.anyof(@promises);                                               
  .result.say for @promises.grep(so *);                                         
  @promises.=grep(not *);                                                       
}                                                                               
while @promises {                                                               
  await Promise.anyof(@promises);                                               
  my %buckets = @promises.classify(so *);                                       
  .result.say for %buckets<True>.values;                                        
  @promises = %buckets<False>.values;                                           
}
13
547
17393
37831

@lizmat
Copy link

lizmat commented Oct 26, 2015

my @p = do for 2000,4000,5,100 -> $nth {
start { (1..Inf).grep(*.is-prime)[$nth] }
}
say "{now}: $_" for awaiterator(@p);

Instant:1445818396.611794: 13
Instant:1445818396.721480: 547
Instant:1445818401.285319: 17393
Instant:1445818408.029987: 37831

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment