Skip to content

Instantly share code, notes, and snippets.

@librasteve
librasteve / iterator.raku
Last active July 5, 2024 15:14
Raku oddNumbers Iterator (following Nim example)
#`[Nim example:
# Thanks to Nim's 'iterator' and 'yield' constructs,
# iterators are as easy to write as ordinary
# functions. They are compiled to inline loops.
iterator oddNumbers[Idx, T](a: array[Idx, T]): T =
for x in a:
if x mod 2 == 1:
yield x
@librasteve
librasteve / ramd.raku
Created June 22, 2024 17:22
Raku Markdown
#!/usr/bin/env raku
use MONKEY-SEE-NO-EVAL;
#| takes markdown file and evals all code lines delimited by '```perl6 xxx ```'
sub MAIN(
$filename!, #= '/pathto/example.md'
) {
my @lines = $filename.IO.lines or die 'no file found';
my $toggle = False;
@librasteve
librasteve / functional.raku
Created May 18, 2024 16:51
How Functional Is Raku?
#viz. https://rosettacode.org/wiki/100_doors
#`[ Elm for comparison
import List exposing (indexedMap, foldl, repeat, range)
import Html exposing (text)
import Debug exposing (toString)
type Door = Open | Closed
toggle d = if d == Open then Closed else Open
@librasteve
librasteve / test.raku
Created February 22, 2024 05:04
How to use sub capture and gather together
sub dims-match( @a ) {
given @a {
when *== 0 { '' }
when *== 1 { .first }
when *>= 2 { .&type-hint }
}
}
gather {
for $.dictionary.type-to-dims.kv -> $key, $value {
@librasteve
librasteve / groupbysign.raku
Created February 2, 2024 11:22
GroupBy Test
say my @input = (1, 4, -3, -6, 5, 6);
sub demerge( $aoa is rw, $value ) {
state $first = 1;
state $newi = 0;
state $last;
my $this = $value.sign;
if $first {
@librasteve
librasteve / gce4.raku
Created January 2, 2024 14:54
GrammarConsumptionExampleOK
use v6.d;
grammar Timestamp {
token TOP { <year><month><day>T<hours><minutes> }
token year { \d**4 }
token month { \d**2 }
token day { \d**2 }
token hours { \d**2 }
token minutes { \d**2 }
}
@librasteve
librasteve / gce3.raku
Created January 1, 2024 22:36
GrammarConsumptionExample3
use Grammar::Tracer;
grammar Timestamp {
token TOP{^<y><m><d>T<hr><mn>$}
token y {\d**4}
token m {\d**2}
token d {\d**2}
token hr {\d**2}
token mn {\d**2}
}
@librasteve
librasteve / gce2.raku
Created January 1, 2024 18:41
GrammarConsumptionExample2
use Grammar::Tracer;
grammar Timestamp {
token TOP{^<y><m><d>T<hr><mn>$}
token y {\d**4}
token m {\d**2}
token d {\d**2}
token hr {\d**2}
token mn {\d**2}
}
@librasteve
librasteve / gce1.raku
Created January 1, 2024 17:18
GrammarConsumptionExample1
use Grammar::Tracer;
grammar Timestamp {
token TOP{^<y><m><d>T<hr><mn>$}
token y {\d**4}
token m {\d**2}
token d {\d**2}
token hr {\d**2}
token mn {\d**2}
}
@librasteve
librasteve / pointsort.raku
Last active December 13, 2023 12:59
Point Sort Example
class Point {
has Int $.x;
has Int $.y;
method distance(Point $other) {
abs(self.y - $other.y) + abs(self.x - $other.x);
}
#`[
method Str {