Skip to content

Instantly share code, notes, and snippets.

View jonathanstowe's full-sized avatar

Jonathan Stowe jonathanstowe

View GitHub Profile
#!/usr/bin/env perl6
use v6;
use Term::termios;
use NativeCall;
class FILE is repr('CPointer') {
sub fdopen(Int, Str) returns FILE is native { * }
method new(Int $fd) {
fdopen($fd, "r");
}
#!perl6
use Audio::PortMIDI;
sub MAIN(Int :$samplerate = 44100, Int :$frequency = 440, Int :$channel = 9, Int :$device = 3, Int :$bpm = 120, :$part) {
my $event-type = NoteOn;
my $pm = Audio::PortMIDI.new;
my $stream = $pm.open-output($device, 32);
role Bop[::Zub] {
has Zub $.bap;
};
role Foo[::Bap] {
has Bap $.bop;
};
my $f = Str;
my $d = class {} but Bop.^parameterize($f);
use HTTP::Server::Tiny;
my $s = supply {
whenever Supply.interval(1) {
emit("data: { DateTime.now }\r\n\r\n".encode);
}
}
sub app(%env) {
return 200, [ Cache-Control => 'must-revalidate, no-cache',Content-Type => 'text/plain; charset=utf-8, text/event-stream' ], $s;
role Zub[::T] {
proto method CALL-ME(|c) { * }
multi method CALL-ME(Mu:D $self) {
}
multi method CALL-ME(Mu:D $self, T $i) {
}
}
multi sub trait_mod:<is> (Method $m, :$foo!) is export {
$m does Zub[Int];
}
#!perl6
use NativeCall;
my $samplerate = 44100;
my $frequency = 440;
sub gen-sin(Int $sample-rate, Int $frequency) {
gather {
loop {
@jonathanstowe
jonathanstowe / gist:1e3cf6f227322d5db071
Created March 9, 2016 13:37
Have a thing delete itself from a hash on promise
class Foo {
has Promise $.promise = Promise.new;
has Str $.name = ('a' .. 'z').pick(8).join('');
}
class Container {
has Foo %.foos;
method add-foo(Foo $f) {
say "adding { $f.name }";
use NativeCall;
class Timeval is repr('CStruct') {
has int64 $.seconds;
has int64 $.microseconds;
}
sub select(int64, Pointer, Pointer, Pointer, Timeval) is native { * }
my $tv = Timeval.new(seconds => 10, microseconds => 0);
my @dists;
for $*REPO.repo-chain -> $r {
if $r.can('prefix') {
if $r.prefix.child('dist').e {
for $r.prefix.child('dist').dir -> $d {
my $dist-data = from-json($d.slurp);
push @dists, Distribution.new(|$dist-data);
}
}
}
@jonathanstowe
jonathanstowe / dumbserver.pl
Created January 26, 2016 16:03
The really really dumb server
use v6;
my sub _index_buf(Blob $input, Blob $sub) {
my $end-pos = 0;
while $end-pos < $input.bytes {
if $sub eq $input.subbuf($end-pos, $sub.bytes) {
return $end-pos;
}
$end-pos++;
}