Skip to content

Instantly share code, notes, and snippets.

View jonathanstowe's full-sized avatar

Jonathan Stowe jonathanstowe

View GitHub Profile
#!/usr/bin/env raku
use Cro::WebSocket::Client;
use EventSource::Server;
use JSON::Fast;
use Cro::HTTP::Router;
use Cro::HTTP::Server;
my $client = Cro::WebSocket::Client.new: :json, uri => 'wss://jetstream1.us-east.bsky.network/subscribe';
#!/usr/bin/env raku
use Cro::WebSocket::Client;
use NCurses;
my $win = initscr;
die "Failed to initialize ncurses\n" unless $win.defined;
my $client = Cro::WebSocket::Client.new: :json, uri => 'wss://jetstream1.us-east.bsky.network/subscribe';
@jonathanstowe
jonathanstowe / polly
Created July 19, 2025 19:24
Async HTTP "polling"
#!/usr/bin/env raku
use v6;
use Manifesto;
use Cro::HTTP::Client;
my $manifesto = Manifesto.new;
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = "^\\d+$";
string numbers = "௫๓௫๓";
@jonathanstowe
jonathanstowe / observer-classic.raku
Created December 10, 2021 16:14
Raku version of classic observer pattern example but without a fixed interface.
#!/usr/bin/env raku
role ObserverSignal {
}
multi sub trait_mod:<is> ( Method $m, :$observer-signal! ) {
$m does ObserverSignal;
}
role Observable {
@jonathanstowe
jonathanstowe / loose.raku
Created December 4, 2021 10:49
Returning an Array from a raku subroutine
# Or if you want to be check each element then you can
# create a subset that does so
subset ArrayOfInt of Array where { .all ~~ Int };
sub d(Int $a, Int $b --> ArrayOfInt) {
[$a, $b];
}
say d(1, 2);
use Cro::HTTP::Client;
my $c;
react {
whenever Cro::HTTP::Client.get("http://cannibal.local") -> $v {
whenever $v.body-text -> $m {
$c = $m;
done;
}
}
use Cro::HTTP::Router;
use Cro::HTTP::Server;
use JSON::Class;
use Cro::HTTP::BodySerializerJSONClass;
class Foo does JSON::Class {
has DateTime $.date is marshalled-by('Str') = DateTime.now;
}
my $app = route {
use Cro::HTTP::BodySerializers;
use JSON::Class;
class Foo::HTTP::BodySerializerJSONClass does Cro::HTTP::BodySerializer {
method is-applicable(Cro::HTTP::Message $message, $body --> Bool) {
with $message.content-type {
(.type eq 'application' && .subtype eq 'json' || .suffix eq 'json') && ($body ~~ JSON::Class );
}
else {
False
@jonathanstowe
jonathanstowe / namedtuple
Created April 19, 2021 12:02
Named Tuple in Raku
class Tuple does Positional {
has $.a;
has $.b;
multi method AT-POS(0) {
$!a;
}
multi method AT-POS(1) {
$!b;
}