Skip to content

Instantly share code, notes, and snippets.

View MattOates's full-sized avatar

Matt Oates MattOates

View GitHub Profile
@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";
@MattOates
MattOates / xmluniq
Last active December 19, 2015 23:59
Filter out a set of tags based on their unique text contents
#!/usr/bin/env perl
use strict;
use warnings;
use 5.10.0;
#Let you define the --tag=rule on the command line
use Getopt::Long;
#Depends on the Mojo DOM because its light weight and nice to use
@MattOates
MattOates / GrammarRoles.p6
Created July 22, 2013 23:46
Playing with roles of tokens for grammars in Perl6.
#!/usr/bin/env perl6
use v6;
role PhoneNumber {
proto token phone-num {*}
token phone-num:sym<gb> {
[<[0]>*]\s*[2<[03489]>]\s*[\d**4]\s*[\d**4] # 02x [eight-digit local number]
|
@MattOates
MattOates / sqlite_import.py
Created August 8, 2013 21:01
Playing with forking and pipes to SQLite3 to speed up that data pumping.
#!/usr/bin/env python
import os, sys, tempfile
from subprocess import Popen, PIPE, STDOUT
#Name of the pipe, use tempfile to create some random filename usually in /tmp
data_pipe = tempfile.mktemp()
#Create the actual pipe 'file' where the OS knows you wanted a pipe type thing
os.mkfifo( data_pipe, 0644 )
@MattOates
MattOates / sqlitemagic.py
Last active December 23, 2021 16:40
Magical SQLite dumping directly over a pipe to the sqlite3 command line client, the .import command is roughly 5x faster at dumping data than using the Python SQLite bindings and doing looped inserts.
#!/usr/bin/env python
import os, sys, tempfile, csv
import re, inspect, ast
from subprocess import Popen, PIPE, STDOUT
"""
Class for dumping data into a SQLite database.
You will need the sqlite3 command line client in your PATH,
even if you are on Windows and not UNIX (maybe)
package main
import(
"fmt"
"net/http"
"net/url"
"io/ioutil"
"encoding/json"
//"strings"
)
@MattOates
MattOates / Portfile
Created September 10, 2013 22:14
First attempt at a Portfile for Rakudo*
# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
# $Id$
PortSystem 1.0
name rakudo-star
conflicts parrot
version 2013.08
categories perl
platforms darwin
@MattOates
MattOates / DNASequence.pm
Last active January 1, 2016 08:19
Snippet for translating DNA to amino acid sequences, trying to use the most Perl6ish looking code.
use v6;
class BioInfo::Sequence {
has Str $.seq;
has Str @.residues;
submethod BUILD (Str :$seq, Str :@residues) {
die X::BioInfo::UnknownResidue.new(seq => $seq, residues => @residues);
$!seq := $seq;
@!residues := @residues;