Skip to content

Instantly share code, notes, and snippets.

View ajs's full-sized avatar

Aaron Sherman ajs

View GitHub Profile
# Based on Python's itertools.tee, takes a list and a count and returns
# count number of iterators, each of which will "contain" all elements
# of the input list, but without evaluating/flattening the input list
# until needed.
#
sub tee(@list, :$count = 2) {
my @indices = 0...^$count;
my @inputs = @indices.map({[]});
my $maketee = -> $i {
gather loop {
@ajs
ajs / gist:2966586
Created June 21, 2012 15:50
Actions and Grammars: a proposal for combining them
role EXPRish is Parser {}
grammar EXPR does EXPRish {
rule TOP { <expr> }
rule expr { <num> '+' <num> | <num> }
token num { (\d+) }
}
class EXPR::Actions does EXPRish {
action TOP { make $<expr>.ast }
action expr {
if $<num>.elems == 1 {
@ajs
ajs / broken.p6
Created July 2, 2012 19:04
Perl6 PIR generation problem
# A trivial program that breaks when run like so:
# perl6 --target=PIR --output=broken.pir broken.p6
# parrot broken.pir
use v6;
BEGIN { say "begin" }
START { say "start" }
ENTER { say "enter" }
@ajs
ajs / filename-example.p6
Created October 13, 2016 18:33
Perl 6 MAIN meta-variables
use v6;
class Filename is Str {
# Do some filename type stuff, here...
}
sub MAIN(
Filename(Str) :$foo
#= A filename argument.
) {
use v6;
class Filename is Str {
# Do some filename type stuff, here...
}
sub MAIN(
:$foo is copy where { $foo = Filename.new: :value($foo) }
#= A filename argument.
) {
@ajs
ajs / P6CRE.md
Last active November 1, 2016 20:42
Thoughts on a PCRE-like generic version of Perl 6 regexes

#Toward a Specification for Perl 6-like Regular Expressions

##Notation

"..." will be used throughout this document to indicate some arbitrary content, which should be explained subsequently.

Otherwise, very little is left to the reader to imagine other than when we get to language-specific concepts.

##Unicode

@ajs
ajs / ArgTrie.p6m
Created November 3, 2016 17:38
A Trie in perl 6 that can generate a sequence of pairs representing the shortest unique prefix over a set of input words
class ArgTrie is Hash {
# A simple Trie that builds a tree from input words (command-line arguments)
# and can generate a list of unique prefixes mapped to the full words.
has $.count is rw = 0;
method gist() { "\{ count={self.count.gist} {self.pairs.gist} \}" }
method insert(Str $word) {
my $letter = $word.substr(0,1);
@ajs
ajs / default-bug.p6
Created November 7, 2016 17:27
Example of the default setting bug in rakudo perl6
#!/usr/bin/env perl6
#
# Demonstrate a bug in defualt handling. One failure:
#
# ok 1 - My something isa Something
# ok 2 - My something does Pingable
# ok 3 - Something starts at zero
# ok 4 - Ping the something
# ok 5 - Something incremented to 1
# not ok 6 - Starts at zero
@ajs
ajs / Perl6-BUILD-caller.txt
Created November 9, 2016 19:05
Test program that shows classes that do not call their BUILD when subclassed
$ perl6 -e 'role Thrower { submethod BUILD(|c) { die; } }; use Test; for <Any Hash Set Int List Routine Mu> -> $name { my $type = ::($name); my $thing = $type.^mixin(Thrower); dies-ok {$thing.new }, "Expect BUILD on ({$thing.perl})" }'
ok 1 - Expect BUILD on (Any+{Thrower})
not ok 2 - Expect BUILD on (Hash+{Thrower})
# Failed test 'Expect BUILD on (Hash+{Thrower})'
# at -e line 1
not ok 3 - Expect BUILD on (Set+{Thrower})
# Failed test 'Expect BUILD on (Set+{Thrower})'
# at -e line 1
@ajs
ajs / main.p6
Created December 7, 2018 18:48
Perl 6 quicksort demo expanded
use v6;
# Empty list sorts to the empty list
multi quicksort([]) { () }
# Called on a scalar item (not a list of one item) return it as a list
multi quicksort($item) { $item, }
# Otherwise, extract first item as pivot...
multi quicksort([$pivot, *@rest]) {