Skip to content

Instantly share code, notes, and snippets.

@0racle
0racle / DateTime::Strings.rakumod
Created Jun 18, 2021
Raku Native strftime/strptime
View DateTime::Strings.rakumod
unit module DateTime::Strings;
use NativeCall;
constant MAX = 256;
my class tm is repr('CStruct') {
has int32 $.second;
has int32 $.minute;
has int32 $.hour;
@0racle
0racle / mngrepper.md
Last active Jan 28, 2021
M×N Grepper
View mngrepper.md
sub mngrepper($n, $m=$n) {
    sub (@xs, &f) {
        (^$n X ^$m).grep(-> ($y, $x) { f(@xs[$x;$y]) })
    }
}

my @puzzle = [<a b>], [<c d>];
my @visited = (True, False), ((Any), False);
@0racle
0racle / dyalog.md
Last active Jan 20, 2021
Dyalog in Raku
View dyalog.md
sub infix:<ρ>($shape, @values) {
    Array.new(:$shape, ([] $shape.skip.map: { *.rotor($_) })(@values));
}
sub prefix:<ι>(UInt $x)     { ^$x }
sub infix:<ι>(@xs, $x)      { @xs.first(:k, $x) }
sub infix:<⁄>($bs, $xs)     { ([Z] $xs, $bs).map({ .[0] if .[1] }) }
sub infix:<↑>(Int $x, @xs)  { $x0 ?? @xs.head($x) !! @xs.tail($x.abs) }
sub infix:<↓>(Int $x, @xs)  { $x0 ?? @xs.tail(* - $x) !! @xs.head(* + $x) }
View split-on.md
multi split-on(&f, @xs, :$A!) {
    (-1, |@xs.grep(:k, &f), |@xs.elems)
      .rotor(2 => -1)
      .map(-> ($a, $b) { @xs[$a ^..^ $b] });
}

multi split-on(&f, @xs, :$B!) {
    my $taken = 0;
    gather {
View mixin-role.raku
use v6.e.PREVIEW;
say 0 but True;
@0racle
0racle / rsp.md
Last active Oct 8, 2020
Rock Scissor Paper
View rsp.md

An implementation of "Rock Scissor Paper" in Raku.

The "robot" will make a weighted random choice based on your previous moves.

use Term::ReadKey;
use Terminal::ANSIColor;

enum Choice  « :Rock(1) :Paper(2) :Scissors(3) »;
enum Outcome « :WIN(1)  :DRAW(0)  :LOSE(-1)    »;
@0racle
0racle / io-socket-ssl.p6
Last active Oct 28, 2019
IO::Socket::SSL
View io-socket-ssl.p6
use IO::Socket::SSL;
my $ssl = IO::Socket::SSL.new(:host<google.com>, :port(443));
$ssl.print(qq:to<END>);
GET / HTTP/1.1
Host:www.google.com
Connection:close
END
View error-margin.p6
role ErrorMargin[$e] {
method ACCEPTS($lval) { $lval ~~ self - $e .. self + $e }
}
say 2.2 ~~ 2 but ErrorMargin[0.5]; # True
say 2.7 ~~ 2 but ErrorMargin[0.5]; # False
@0racle
0racle / About.md
Created Sep 19, 2019
Slang::ForElse
View About.md
View Nested.md

I was playing around with nesting data in hashes, and came up with... something.
Some kinda tree structure with JSON-Path like access.
Not sure what this might be useful for.

Raku and Perl implementations below...

Raku

use Data::Dump;