Skip to content

Instantly share code, notes, and snippets.

o o o o o o o o o o o o o x o o o o o o o o o o
o o o o o o o x x x x x x x o o o o o o o o o o
o o o o o o o o o o o o o o o x o o o o o o o o
o o o o o o o o o x x x o o o o o o o o o o x x
o o o o o o o o o x x x o o o o o o o o o o x x
o o o o o o o o o o x o o o o o o o o o o o x x
o o o o o o o o o o o o o o o o x o x o o o o o
o o o o o o o o o o o o o o o o x x o o o o o o
o o o o o o o o o o o o o o o o x x o o o o o o
my $x = "\{\n \"name\" : \"Algorithm::Snail\",\n \"description\" : \"Snail sort (snailshell pattern, spiral order) sequence in grid (two dimensional array)\",\n \"version\" : \"0.2\",\n \"perl\": \"6.*\",\n \"auth\": \"github:mj41\",\n \"authors\" : [ \"Michal Jurosz\" ],\n \"license\": \"Artistic-2.0\",\n \"source-type\" : \"git\",\n \"source-url\" : \"git://github.com/mj41/Algorithm-Snail.git\",\n \"depends\" : [],\n \"build-depends\": [],\n \"provides\" : \{\n \"Algorithm::Snail\" : \"lib/Algorithm/Snail.pm6\",\n },\n \"support\": \{\n \"bugtracker\": \"https://github.com/mj41/Algorithm-Snail/issues\",\n \"license\": \"http://www.perlfoundation.org/artistic_license_2_0\"\n },\n \"tags\": [ \"algorithm\", \"bidimensional\", \"sequence\", \"array\" ],\n \"test-depends\": [\n \"Test\"\n ]\n}"; say $x; say ::("Rakudo::Internals::JSON").from-json($x);
λ ~$ perl6 -e 'use Bench;my @a = qw<abc cd e>; Bench.new.cmpthese(10000, { no-reverse => sub { @a.sort({ $^b.chars <=> $^a.chars }); }, reverse => sub { @a.sort(*.chars).reverse; } },);'
Benchmark:
Timing 10000 iterations of no-reverse, reverse...
no-reverse: 0.418 wallclock secs (0.377 usr 0.049 sys 0.427 cpu) @ 23939.825/s (n=10000)
reverse: 0.483 wallclock secs (0.447 usr 0.049 sys 0.495 cpu) @ 20700.334/s (n=10000)
O------------O---------O------------O---------O
| | Rate | no-reverse | reverse |
O============O=========O============O=========O
| no-reverse | 23940/s | -- | -33% |
| reverse | 20700/s | 49% | -- |
my @ARGV = do { sub eval { chomp &EVAL(@_) }; eval( ("0" and q|@*ARGS| or q|@ARGV|) ) };
for (1..@ARGV[0]) {
my $i = $_;
while ($i != 1) {
last if $i == 0;
if (sprintf("%d", $i / 2) == $i / 2) {
$i /= 2;
next;
}

relevant code:

#composing the types on initialization (from YAML)
$new-model.HOW.add_attribute($new-model, Attribute.new(
  :name<@.columns>, :has_accessor(1), :type(Array), :package($new-model.WHAT),
));

#later in BUILD
multi submethod BUILD (:$!driver, :$!db, :$!quote, :$!dbo, :@columns?) {
my @a = get.comb(/\d+/);
say "{++$_}: {@a=(@a Z- @a.rotate)>>.abs}" until none(@a) || (%){~@tup}++;

~/projects/p6-zefpan $ curl http://localhost:10000/package/candidates 2>/dev/null |json_pp

{
   "results" : [
      {
         "source_url" : "git://github.com/ugexe/zef.git",
         "license" : "Artistic-2.0",
         "depends" : [],
         "version" : "0.2.4",

app.pl6

#!/usr/bin/env perl6

use Cro::HTTP::Router;
use lib 'lib';
use Router;

my $application = build-router;
use QAST:from<NQP>;
use Element;
use nqp;
use Data::Dump;
sub create-element($type, *@data) is export {
my %data = @data.classify: {$_ ~~ Pair ?? "pars" !! "children"};
#say "Element.new: :$type, :pars({%data<pars>}), :children({%data<children> // []})";
Element.new: :$type, :pars(%data<pars>.map({ :data($_<data> ~~ Callable ?? $_<data>.() !! $_<data>) }) // {}), :children(%data<children> // [])
}
tonyo@enoch:~/projects/perl6-http-server-async$ siege -b -t 1m http://127.0.0.1:1666
** SIEGE 4.0.2
** Preparing 25 concurrent users for battle.
The server is now under siege...
Lifting the server siege...
Transactions: 842 hits
Availability: 100.00 %
Elapsed time: 59.61 secs
Data transferred: 0.00 MB
Response time: 1.71 secs