Skip to content

Instantly share code, notes, and snippets.

View MattOates's full-sized avatar

Matt Oates MattOates

View GitHub Profile
my %words;
given slurp("part-of-speech.txt") {
my int $pos = 0;
my int $chars = $_.chars;
while $pos < $chars {
my int $tab = $_.index("\t", $pos);
my int $nl = $_.index("\n", $tab);
%words{$_.substr($pos, $tab - $pos)} =
$_.substr($tab + 1, $nl - $tab - 1);
# https://raw.githubusercontent.com/kevina/wordlist/master/pos/part-of-speech.txt
my $input = slurp('part-of-speech.txt');
my %words;
sub bench($name, &code) {
%words = ();
my $start = now;
code;
my $end = now;
@MattOates
MattOates / HQ9Plus.p6
Last active August 29, 2015 14:25
An hq9+ REPL inspired by https://github.com/dha/hq9plus-in-perl6 uses grammars and actions to handle commands. Uses a 64bit accumulator to handle super wide future proof memory addresses.
#!/usr/bin/env perl6
use v6;
#Handle terminal colour output by installing a module
#panda install Term::ANSIColor
use Term::ANSIColor;
grammar HQ9Plus::Grammar {
#A valid REPL line is just a sequence of commands and nothing else including white space
token TOP {
@MattOates
MattOates / parallel-primes.p6
Last active September 4, 2015 20:47
Do some parallel prime finding in the most naive way.
#!/usr/bin/env perl6
use v6;
use Stats;
sub bench($name, &code) {
my ($start,$end);
my $max = 1000;
my @times;
@MattOates
MattOates / chomp_lines.p6
Created September 10, 2015 16:17
Chomp files code
#!/usr/bin/env perl6
for 'data.tsv'.IO.lines {}
@MattOates
MattOates / gist:e68e4aa37753d9d3239c
Created October 16, 2015 14:17
Install latest Rakudo on MoarVM
git clone https://github.com/tadzik/rakudobrew ~/.rakudobrew
export PATH=~/.rakudobrew/bin:$PATH
echo 'export PATH=~/.rakudobrew/bin:$PATH' >> ~/.bash_profile
rakudobrew build moar
rakudobrew build-panda
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use Bio::SeqIO;
sub counts {
my ($counts, $seq) = @_;
map {$counts{$_}++} split ''. $seq;
@MattOates
MattOates / postfix_factorial.p6
Last active December 19, 2015 08:39
A Perl6 factorial postfix operator using recursive multi dispatch
multi sub postfix:<!> (Int $i where $i < 0) is tighter(&infix:<*>) { fail "Not a Natural Number in Factorial" }
multi sub postfix:<!> (Int $i where 0|1) is tighter(&infix:<*>) { 1 }
multi sub postfix:<!> (Int $i where $i > 1) is tighter(&infix:<*>) { $i * ($i-1)! }
use Test;
isa_ok -1!, Failure, "Factorial for -1 fails";
ok 0! == 1, "Factorial for 0";
ok 1! == 1, "Factorial for 1";
ok 5! == 120, "Factorial for a larger integer";
@MattOates
MattOates / given_factorial.p6
Last active December 19, 2015 08:39
A Perl6 factorial function using a switch (given/when) statement and recursion.
sub factorial (Int $i) {
given $i {
when $i < 0 { fail "Not a Natural Number" }
when 0 { 1 }
when 1 { 1 }
default { $i * factorial $i-1 }
}
}
use Test;
@MattOates
MattOates / meta_factorial.p6
Last active December 19, 2015 08:39
A factorial function in Perl6 using a [*] meta operator to produce the factorial product of a range, additionally the function parameter is given using a placeholder variable $^i
sub factorial {
if $^i < 0 {
fail "Not a Natural Number";
} else {
[*] 1..$i;
}
}
use Test;
isa_ok factorial(-1), Failure, "Factorial for -1 fails";