Skip to content

Instantly share code, notes, and snippets.

@grondilu
grondilu / continued-fraction.p6
Created December 19, 2012 18:34
continued fractions, an other attempt
sub continued-fraction(@a, @b)
{
my &f = { $^x };
gather while True {
take my &g = -> $x { &f(@a.shift + @b.shift / $x) };
&f = &g;
}
}
say continued-fraction((1, 2 xx *).list, (1 xx *).list)[1](Inf);
@grondilu
grondilu / erat.p6
Created March 9, 2013 10:08
recursive Eratosthene sieve in Perl6
multi erat(Int $N) { erat 2 .. $N }
multi erat(@a where @a[0] > sqrt @a[*-1]) { @a }
multi erat(@a) { @a[0], callwith(@a.grep: * % @a[0]) }
 
say erat 100;
@grondilu
grondilu / cstr-grondilu.pl
Last active December 15, 2015 17:29
creating a character table from genetic strings (rosalind problem)
my @dna = lines;
my @c = @dna[0].comb;
for ^@c -> $c {
my $count = 0;
my @line = map { $count += my $x = +so .comb[$c] eq @c[$c]; $x }, @dna;
say @line.join if 1 < $count < @dna-1;
}
@grondilu
grondilu / blosum62
Created May 3, 2013 16:25
Turning a poorly formated matrix into a proper double entry array
How could I turn the following data:
A C D E F G H I K L M N P Q R S T V W Y
A 4 0 -2 -1 -2 0 -2 -1 -1 -1 -1 -2 -1 -1 -1 1 0 0 -3 -2
C 0 9 -3 -4 -2 -3 -3 -1 -3 -1 -1 -3 -3 -3 -3 -1 -1 -1 -2 -2
D -2 -3 6 2 -3 -1 -1 -3 -1 -4 -3 1 -1 0 -2 0 -1 -3 -4 -3
E -1 -4 2 5 -3 -2 0 -3 1 -3 -2 0 -1 2 0 0 -1 -2 -3 -2
F -2 -2 -3 -3 6 -3 -1 0 -3 0 0 -3 -4 -3 -3 -2 -2 -1 1 3
G 0 -3 -1 -2 -3 6 -2 -4 -2 -4 -3 0 -2 -2 -2 0 -2 -3 -2 -3
H -2 -3 -1 0 -1 -2 8 -3 -1 -3 -2 1 -2 0 0 -1 -2 -3 -2 2
constant BLOBSUM62 = q :to 'EOT';
A C D E F G H I K L M N P Q R S T V W Y
A 4 0 -2 -1 -2 0 -2 -1 -1 -1 -1 -2 -1 -1 -1 1 0 0 -3 -2
C 0 9 -3 -4 -2 -3 -3 -1 -3 -1 -1 -3 -3 -3 -3 -1 -1 -1 -2 -2
D -2 -3 6 2 -3 -1 -1 -3 -1 -4 -3 1 -1 0 -2 0 -1 -3 -4 -3
E -1 -4 2 5 -3 -2 0 -3 1 -3 -2 0 -1 2 0 0 -1 -2 -3 -2
F -2 -2 -3 -3 6 -3 -1 0 -3 0 0 -3 -4 -3 -3 -2 -2 -1 1 3
G 0 -3 -1 -2 -3 6 -2 -4 -2 -4 -3 0 -2 -2 -2 0 -2 -3 -2 -3
H -2 -3 -1 0 -1 -2 8 -3 -1 -3 -2 1 -2 0 0 -1 -2 -3 -2 2
I -1 -1 -3 -3 0 -4 -3 4 -3 2 1 -3 -3 -3 -3 -2 -1 3 -3 -1
@grondilu
grondilu / combinations.p6
Last active December 17, 2015 03:39
combinations method for Array
use MONKEY_TYPING;
augment class Array {
method combinations(Int $of) {
return [] if $of == 0;
return () if self.elems == 0;
map({ [ self[0], $_[] ] }, [self[1..*-1]].combinations($of-1)),
[self[1..*-1]].combinations( $of );
}
}
use v6;
my %mass-table = <A 71.03711 C 103.00919 D 115.02694 E 129.04259 F
147.06841 G 57.02146 H 137.05891 I 113.08406 K 128.09496 L 113.08406
M 131.04049 N 114.04293 P 97.05276 Q 128.05858 R 156.10111 S
87.03203 T 101.04768 V 99.06841 W 186.07931 Y 163.06333 >;
my @L = sort <4454.90309546 4525.94020546 4622.99296546 4685.42633528
4809.07227546 4828.61559907 4906.86132096 4937.16723546 5068.20772546
5080.83276633 5196.26630546 5202.17174106 5310.30923546 5411.35691546
5514.36610546 5604.87077433 5627.45016546 5688.51728114 5758.49065546
@grondilu
grondilu / sears-haack integration
Created May 28, 2013 13:56
integration failure in perl6
sub simpsons(&f, $a, $b, $n) {
my $h = ($b - $a) / $n;
my $h2 = $h/2;
my $sum1 = f($a + $h2);
my $sum2 = 0;
for $a+$h, *+$h ... $b-$h {
$sum1 += f($_ + $h2);
$sum2 += f($_);
}
-----BEGIN PGP MESSAGE-----
Version: GnuPG v1.4.12 (GNU/Linux)
owGbwMvMwCS4Ou77+4AdfBmMp0OTGAKX125JK0pNzctPSbVSTsosSc7PzNPNL0m2
SktMNjcxMEmyMDE0ME1LMU80MU00TzY3SDE2t0w1Mzc3MzZNTUlMNTdPNUoyNk41
NjNLSkk1TDTn6nBjYRBkYmBjZQIZzsDFKQCzceZ/hnk2dzr/5+559KBMcvauk1uO
hRxp9eBnWLDGNntzQPK+qPUXJsw3rt/6tXXJxOkA
=RQvK
-----END PGP MESSAGE-----
@grondilu
grondilu / chbp
Created July 11, 2013 19:15
Rosalind: character based phylogeny in Perl 6
use v6;
my @sample = «
'cat dog elephant mouse rabbit rat'
011101
001101
001100
»;
my $species = set my @species = @sample.shift.words; #get.words;
my @split = map {
item set map *.key, grep *.value, hash @species Z=> .comb