Skip to content

Instantly share code, notes, and snippets.

View BenGoldberg1's full-sized avatar

Ben Goldberg BenGoldberg1

View GitHub Profile
@BenGoldberg1
BenGoldberg1 / Alias::Shift.pm
Last active December 27, 2015 06:49
Defines an alternative boilerplate for class methods.
#!perl
package Alias::Shift;
require Devel::LexAlias;
use strict;
use warnings;
my $skip;
sub shifter {
my ( $pack, $ref, @attr ) = @_;
#!perl
package CPPish;
use Devel::LexAlias;
use Attribute::Handlers;
use strict;
use warnings;
my $deparser = CPPish::D->new(-1);
our $processing;
package Method::LexicalFields;
use fields ();
use strict;
use warnings;
use Devel::LexAlias;
use Attribute::Handlers;
use Scalar::Util::Hash qw(lock_value);
my $vname_re = qr[[%$@][[:alpha:]]\w*]i;
my $field_re = qr[_?$vname_re];
+"(*+*)*(*+* +*/*) **** ** * * *+*+*/* **** ** * *
*+*-*/* **** ** * * *+* ****-* **** ** * * *-(*-*/
*) ** (*+*/*) **** ** * * *-* **** ** * * *-*+*/*
**** ** * * *+* **** ** * * *-* * * ***((* - */*)
** * * *-*/*) * * **** ** * * *+*/* ****-* ***(*-*
/*)**(*+*/*) * * ***((*-*/*)** * * *-*/*) * * ****
** * * *+*/* **** ** * * *+*+*/*-* * *".split(<***>
).map: {.(|(<* * *>xx.count)).chr.print given EVAL
"(* ** *+*+*-*/*+$_)" }
use v6;
sub primes-list(Bool $use-wheel) {
my @start = $use-wheel ?? (2, 3) !! (2);
my @sieve;
my $i = @start-1;
my $p = @start[$i];
my $q = $p*$p;
my $n = $p;
my \incr = $use-wheel ?? 2 !! 1;
@start, -> *@primes {
@BenGoldberg1
BenGoldberg1 / ThreadSort.pl6
Last active November 20, 2015 03:41
Bad multithreaded sort function.
#!/usr/bin/env perl6
use v6;
sub sort-promise ($c) {
my @same;
earliest $c {
more $c { push @same, $c.receive }
done $c { return }
}
my @chans = Channel.new xx 2;
@BenGoldberg1
BenGoldberg1 / TwoChannels.pl6
Last active September 27, 2015 22:32
One-Pass Sorting Algorithm
#!/usr/bin/env perl6
use v6;
sub sort-promise ($in, $out) {
start {
my @same;
earliest $in {
more * { push @same, $in.receive }
done * { return }
}
my ($less_c, $more_c) = Channel.new xx 2;
@BenGoldberg1
BenGoldberg1 / SupplySort.pl6
Last active October 17, 2015 18:16
Sort Using Supplies
#!/usr/bin/env perl6
use v6;
sub mysort( Supply $s ) {
my (@same, @supplies, @promises);
my $p = Promise.new;
$s.tap( -> $val {
if @same {
given $val <=> @same[0] {
when Same { push @same, $val }
when Less { @supplies[0].emit( $val ) }
@BenGoldberg1
BenGoldberg1 / LRU.pm6
Created November 3, 2015 04:12
Cache::LRU
use v6;
class Cache::LRU {
has Hash[Node] %.entries;
has Node $.lru;
class Node {
has $value is required;
has $prev is rw;
has $next is rw;
method splice() {
@BenGoldberg1
BenGoldberg1 / OnceMoreWithFeeling.pl6
Last active April 16, 2016 02:41
Primes Iterator
use v6;
constant DEBUGGING = False;
my class P does Iterator {
has @!outbuf = (2, 3);
has @!primes;
has @!factors;
has $!a-prime = 3;
has $!a-squared-prime = $!a-prime ** 2;
has $!maybe-prime = 3;
method !mark-next-multiple( \factor ) {