Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View FCO's full-sized avatar

Fernando Correa de Oliveira FCO

View GitHub Profile
raku -MCro::HTTP::Server -MCro::HTTP::Router -MCro::HTTP::Client -e '
my &where = -> Int $i, Int $actual { say "$actual == $i -> { $actual == $i }"; $actual == $i }
Cro::HTTP::Server.new(
host => "127.0.0.1",
port => 10001,
application => route {
post -> {request-body -> $data? { content "test/plain", "ok!" } }
}
).start;
Promise.in(5).then: { Cro::HTTP::Client.post: "http://127.0.0.1:10001", :headers["Content-Type" => "application/x-www-form-urlencoded"] } # breaks the server
@FCO
FCO / cli
Created February 22, 2024 22:01
$ raku -MCro::HTTP::Server -MCro::HTTP::Router -e '
Cro::HTTP::Server.new(
host => "127.0.0.1",
port => 10001,
application => route {my $CRO-ROUTER-RESPONSE = $*CRO-ROUTER-RESPONSE;
get -> {content "test/plain", "ok!"}
^3 .map: -> $i {
post -> Int $a where { say "$_ == $i -> { $_ == $i }"; $_ == $i } {
CATCH { default { say "ERROR: $_" } }
raku -I. -MRed -MRed::Type -MRed::AST::Value -MJSON::Fast -e '
red-defaults "SQLite";
my $*RED-DEBUG = True;
enum Currency <USD GBP BRL>;
class Money { has Rat() $.value; has Currency() $.currency }
class DBMoney does Red::Type {
method inflator { -> $_ is copy --> Money { given .&from-json { Money.new: :value(.<value>), :currency(Currency::{ .<currency> }) } } }
$ raku -I. -MRed -MRed::Type -MRed::AST::Value -MJSON::Fast -e '
red-defaults "SQLite";
#my $*RED-DEBUG = True;
enum Currency <USD GBP BRL>;
class Money { has Rat() $.value; has Currency() $.currency }
class DBMoney does Red::Type {
method inflator { -> $_ is copy --> Money { given .&from-json { Money.new: :value(.<value>), :currency(Currency::{ .<currency> }) } } }
use Singleton;
say Singleton.new.WHERE
class Agg does Callable {
has Callable @.subs;
has Signature $.signature = Signature.new: :params[|@!subs.map: |*.signature.params], :returns(Parameter.new(:type(Map())));
method CALL-ME(|c) {
%(
|@.subs.map: {
my @names = .signature.params.grep({ .named }).map(|*.named_names);
my %params := (c.hash{@names}:p).Map;
my $interval = .run-at: :5mins-running, :off, -> :$rule, :$time {
$rule.stop;
say "$time.hh-mm-ss(): interval has finished"
}
.run-at: :25mins-running, -> :$rule, :$time {
$rule.stop; say "$time.hh-mm-ss(): Time to relax";
$interval.start
}
use App::RakuCron::Configuration;
use App::RakuCron::Rule;
use Lumberjack;
sub print-example(Str $msg = "Generic message", :$rule, +@params, :$time, :$delta-secs = "None", :$something-else = "") {
$rule.log-warn: "$msg (@params[]) ({ $something-else }): ", $time.hh-mm-ss, " -> { $delta-secs }"
}
Lumberjack.dispatchers = [
Lumberjack::Dispatcher::Console.new: :colour
use App::RakuCron::Configuration;
sub print-example(Str $msg, +@params, :$time, :$delta-secs, :$something-else) {
say "$msg (@params[]) ({ $something-else // "" }): ", $time, " -> { $delta-secs // "None" }"
}
config {
# every Mon/Web/Fri at 10:00:00
.run-at: :10hours, :wday<Mon Wed Fri>, { shell "ls -la" }
@FCO
FCO / quicksort.raku
Created August 9, 2023 13:13
better destructing?
multi quicksort([]) { Empty }
multi quicksort([$pivot, *@rest]) {
my (:Less(@less), :Same(@same), :More(@more)) := @rest.classify(* cmp $pivot);
[ |quicksort(@less), $pivot, |@same, |quicksort(@more) ]
}
say quicksort [7, 2, 1, 8, 1, 9, 3]