Skip to content

Instantly share code, notes, and snippets.

View jonathanstowe's full-sized avatar

Jonathan Stowe jonathanstowe

View GitHub Profile
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;
}
use Cro::MQTT;
my $subscriptions = subscriptions {
subscribe 'hello-world', {
consume -> $m { say "GOT HELLO WORLD ", $m.message.decode }
}
subscribe 'another-world', {
consume -> $m { say "GOT ANOTHER WORLD ", $m.message.decode }
}
}
@jonathanstowe
jonathanstowe / gist:a7e5401c74d0b3c4e7b862f14af2e769
Created January 29, 2021 11:16
make profile.d for installed rakudo
PROFILE_SCRIPT=/etc/profile.d/$PKG_NAME.sh
/usr/bin/cat - <<EOSCR >$PROFILE_SCRIPT
RAKUDO_PATHS=$INSTALL_PREFIX/bin
for TOP in raku perl6
do
if [ -d $INSTALL_PREFIX/share/\$TOP ]
then
for REP in site vendor core
# Inspired by https://dev.to/harleypadua/recursion-recursion-recursion-1501
multi shoot-gun(Str $sound, Int $num where * > 0 --> Str ) {
return $sound ~ ' ' ~ shoot-gun($sound, $num - 1);
}
multi shoot-gun($, 0 --> '') {
}
say shoot-gun('bang', 5);