Skip to content

Instantly share code, notes, and snippets.

@grondilu
grondilu / gist:6112731
Created July 30, 2013 13:07
ethiopian exponentiation in Perl 6
From f3c3e42801cc58889a7994d043891d04d16f5ba2 Mon Sep 17 00:00:00 2001
From: Lucien Grondin <[email protected]>
Date: Tue, 30 Jul 2013 14:55:22 +0200
Subject: [PATCH] first attempt of ethiopian exponentiation
---
src/core/Complex.pm | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/src/core/Complex.pm b/src/core/Complex.pm
@grondilu
grondilu / gist:6112872
Created July 30, 2013 13:26
second attempt of ethiopian exponentiation
From 7993316217fc1e137a757ab710077e68cb7bb350 Mon Sep 17 00:00:00 2001
From: Lucien Grondin <[email protected]>
Date: Tue, 30 Jul 2013 15:23:07 +0200
Subject: [PATCH 2/2] second attempt of ethiopian exponentiation
---
src/core/Complex.pm | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/src/core/Complex.pm b/src/core/Complex.pm
@grondilu
grondilu / gist:6112994
Created July 30, 2013 13:39
ethiopian exponantiation
From 94aca2cbcba52f51708ae37904234096ec8781c2 Mon Sep 17 00:00:00 2001
From: Lucien Grondin <[email protected]>
Date: Tue, 30 Jul 2013 15:38:18 +0200
Subject: [PATCH] first commit
---
src/core/Complex.pm | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/src/core/Complex.pm b/src/core/Complex.pm
From cca7ffe335a65cbf1c7c0e95a45017e16e5979fa Mon Sep 17 00:00:00 2001
From: Lucien Grondin <[email protected]>
Date: Tue, 30 Jul 2013 15:59:12 +0200
Subject: [PATCH 2/2] fixing typo in signature
---
src/core/Complex.pm | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/core/Complex.pm b/src/core/Complex.pm
sub ethiop(Complex:D \z, Int:D \n where n > 0) {
my ($p, $z, $n) = 1, z, n;
until $n == 0 {
$p *= $z unless $n %% 2;
$n div= 2;
$z *= $z;
}
return $p;
}
sub ethiopicmult($a, $b) {
my @left = $a, * div 2 ... 1;
my @right := $b, 2 * * ... *;
[+] map *.value, grep *.key % 2, (@left Z=> @right)
}
say ethiopicmult 17, 34;
sub longest_increasing_subsequence(@d) {
my @l = [[].item];
for ^@d -> $i {
@l.push: [ max(:by(*.elems), grep { .[*-1] < @d[$i] }, @l[0..$i]).list, @d[$i] ].item;
}
return max :by(*.elems), @l;
}
say .perl given longest_increasing_subsequence <0 8 4 12 2 10 6 14 1 9 5 13 3 11 7 15>».Int;
role BinaryTree[::T] {
has T $!value;
has BinaryTree[T] ($.left, $.right);
method replace-all(T $value) {
$!value = $value;
$.left.?replace-all($value);
$.right.?replace-all($value);
}
}
@grondilu
grondilu / Lambda.pm
Last active December 21, 2015 19:09
Perl 6 grammar for Lambda-calculus
grammar Lambda {
=for description
I was trying to write a grammar for Lambda-calculus,
and I failed parsing a simple 'λx.x x' string.
rule TOP { ^ <expression> $ }
rule expression {
| \( ~ \) <expression>
@grondilu
grondilu / stree.pl
Created September 17, 2013 12:33
Ukkonen's online algorithm for suffix trees in Perl 6. Translated from: http://marknelson.us/1996/08/01/suffix-trees/
class Suffix {...}
class Edge {...}
class Node { has Int $.suffix_node is rw = -1 }
subset Explicit of Suffix where { .first_char_index > .last_char_index }
subset Implicit of Suffix where { .last_char_index >= .first_char_index }
my @Nodes = Node.new;
my Blob $T;
class Suffix {