Created
January 10, 2022 21:56
-
-
Save gfldex/4c117d10b987a6a32521df7ff5ef2d66 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6.*; | |
# my $a = '1 B'; | |
# | |
# if $a ~~ /(<digit>) \s (<alpha>)/ -> $_ { | |
# my ($one, $B) = .deepmap: *.Str; | |
# say "$one $B"; | |
# } | |
# | |
# if $a ~~ /(<digit>) \s (<alpha>)/ -> Match (Str() $one, Str() $B) { | |
# dd $one; | |
# dd $B; | |
# } | |
# | |
# | |
# my @a = <1 B 3 D 4>; | |
# my @b; | |
# | |
# my $hit; | |
# | |
# for @a -> $e { | |
# @b.push: ($e ~~ /(<alpha>) || { next } /).Str; | |
# } | |
# | |
# sub cherry-pick-numeric(Str $matchee) { | |
# $matchee ~~ m/(<digit>) && { return .Numeric }/; | |
# Empty | |
# } | |
# | |
# @b = do .&cherry-pick-numeric for @a; | |
# | |
# dd @b; | |
# | |
# say 'mark'; | |
# $a = '1B3D4'; | |
# | |
# my \ll := gather $a ~~ m:g/ | |
# [ <alpha> && { take $/<alpha>.Str } ] | |
# | [ <digit> && { take $/.<digit>.Numeric } ] | |
# | [ { say 'step' } ] | |
# /; | |
# say ll[0]; | |
# say ll[3]; | |
# | |
# | |
# sub nil-is-bad(\v) { | |
# die "bad Nil is bad" if v eqv Nil; | |
# v | |
# } | |
# $a = '1 B'; | |
# | |
# if nil-is-bad($a ~~ /(<digit>) \s (<alpha>)/) -> Match (Str() $one, Str() $B) { | |
# say "got $one and $B but not Nil"; | |
# } | |
### | |
# class SuperInt { | |
# has $.left-factor is rw; | |
# has $.right-factor is rw; | |
# method new(\l, \r) { | |
# my \SELF = self.CREATE; | |
# SELF.left-factor = l; | |
# SELF.right-factor = r; | |
# SELF | |
# } | |
# } | |
# | |
# multi sub infix:<÷>(Numeric:D \l, SuperInt:D \r) { | |
# l ÷ r.left-factor * r.right-factor | |
# } | |
# | |
# Int.^add_method('CALL-ME', my method (\SELF: \v) { SuperInt.new(SELF, v) }); | |
# Int.^compose; | |
# | |
# say 60÷5(7−5); | |
# sub chain-coerce(@list, *@types) { | |
# dd @list; | |
# do for @list Z @types -> [\e, \T] { | |
# my $type-name = T.^name; | |
# dd $type-name; | |
# e."$type-name"() | |
# } | |
# } | |
# | |
# { # Moving the pattern to var which we interpolate into match | |
# my $input = 'There are 9 million bicycles in beijing.'; | |
# my $pattern = rx{ (\d+) \s+ (\w+) }; | |
# if $input ~~ / <$pattern> / -> $m { | |
# say $m[0]; | |
# say $0.^name; # Nil | |
# say $0; # Nil | |
# say $1.^name; # Nil | |
# say $1; # Nil | |
# say $/; # 「9 million」 | |
# } | |
# } | |
# | |
# enum DebugLevel <little much most>; | |
# | |
# my $*debug; | |
# | |
# $*debug = little; | |
# | |
# dd $*debug; | |
# | |
# $*debug = any $*debug, 42; | |
# | |
# say $*debug ~~ 42; | |
# | |
# $*debug = any $*debug, 'answer'; | |
# | |
# say $*debug; | |
# | |
# my Int $a = 10; role Foo[$a] {}; role Bar[Int $a] does Foo[$a] {} | |
# | |
# for (:a, :!b, :c('str'), 12, :k('42'), :d(42), :e(55), '47', '55', 'foo') { | |
# when Pair & :value(Bool & :so) { dd $_ } | |
# when Pair & :value(Bool & :!so) { dd $_ } | |
# when Pair & :value(Str) { dd $_ } | |
# when Pair & :value(42) { dd $_ } | |
# when Pair & :value(Int) { dd $_ } | |
# when Str & :Int & 47 { dd $_ } | |
# when Str { dd $_ } | |
# when Int { dd $_ } | |
# } | |
# | |
# my \v = :value(Bool & :so); | |
# dd v; | |
# say :so.WHAT; | |
# dd :so.value; | |
# my @a = <a b c>; my @b = 1,2,3; my @c := -> *@c { @c }(@a, @b); dd @c; | |
# my @a = <a b c>; my @b = 1,2,3; my (*@c) := @a, @b; dd @c; | |
# | |
# sub spy(\v) { say v with v; v }; (my @sparce)[0,2,3] = 1,3,4; loop { last without spy @sparce[$++] } | |
# react whenever Supply.interval(1) { | |
# state $first = True; | |
# $first and ( $first = False; say 'first' ); | |
# .say | |
# } | |
# role R { | |
# method m() { say $?CLASS } | |
# } | |
# | |
# my $c = class SomeName does R {}; | |
# $c.m; | |
# | |
# my $emitter = Supplier.new; | |
# my $s = $emitter.Supply; | |
# my $p = start { | |
# loop { | |
# $emitter.emit: [42, 'answer', Any].roll; | |
# sleep 0.25; | |
# } | |
# } | |
# for $s.Channel.list -> \v { | |
# given v { | |
# when Int { say .WHAT } | |
# when Str { say .WHAT } | |
# when Any:U { say 'undefined' } | |
# when Failure { .Bool; say .message; } | |
# } | |
# } | |
# multi sub m(Int $_) { | |
# say .WHAT; | |
# } | |
# | |
# multi sub m(Str $_) { | |
# say .WHAT; | |
# } | |
# | |
# multi sub m(Any:U $_) { | |
# say 'undefined'; | |
# } | |
# | |
# multi sub m(Failure $_ is raw) { | |
# say .WHAT; | |
# } | |
# | |
# sub handle-with(Supply:D $s, *@routines) { | |
# my proto pseudo-multi(|) {*} | |
# for @routines -> &b { | |
# &pseudo-multi.add_dispatchee(&b); | |
# } | |
# | |
# $s.tap: &pseudo-multi; | |
# } | |
# | |
# handle-with($s, | |
# sub (Int $_) { say .WHAT }, | |
# sub (Str $_) { say .WHAT }, | |
# sub (Any:U $_) { say 'undefined' }, | |
# # sub (Failure) { say .message; .so; } | |
# ); | |
# | |
# await $p; | |
# class C { | |
# method ^parameterize(\T, \v) { dd T, v; C.new } | |
# } | |
# | |
# my $c = C[42]; | |
# dd $c; | |
# | |
# class NotNil is Nil { | |
# method new { self.CREATE } | |
# } | |
# | |
# class C { method NotNil() { NotNil.new } }; sub s(NotNil()) {}; s(C.new); | |
# class NotNil is Nil {}; | |
# | |
# class C { | |
# method NotNil() { NotNil.new } | |
# }; | |
# | |
# sub s(NotNil()) {}; | |
# | |
# s(NotNil.new); | |
# | |
# dd NotNil.new | |
my Int $a = 42; | |
constant \T := $a.VAR.of; | |
# my $b is T; | |
my $b where $a.WHAT = 123; | |
# say [3,5,1,2,3].grep({say $_; $_ !%% 2 or die('bailing')}).unique; | |
# OUTER: for 1 { | |
# last OUTER; | |
# }; | |
# my $string = "wowoeroiueawr 2 to 4"; | |
# sub type-map(\match, *@types) { | |
# my %index-to-type = @types.kv; | |
# match.caps.map: { .value."{%index-to-type{.key}.^name}"() }; | |
# } | |
# my @pos = ($string ~~ m/(.+) " " (\d) " to " (\d)/).&type-map(Str, Int, Int); | |
# dd @pos; | |
# my ($s1, $i1, $i2) = ($string ~~ m/(.+) " " (\d) " to " (\d)/).&type-map(Str, Int, Int); | |
# my regex re { ^'abc' }; | |
# say 'abc' ~~ /<re>/; | |
# my $re = /\w+/; | |
# say 'ab c' ~~ / $re /; | |
# my $foo = "a+b"; | |
# dd "aaaaab" ~~ m/<{$foo}>/; | |
# proto sub type-map(+@) {*} | |
# multi sub type-map(+@ [] --> Empty) { } | |
# multi sub type-map(*@ [::T, T() \x, **@xs]) { x, |@xs.&type-map } | |
# | |
# my $string = "wowoeroiueawr 2 to 4"; | |
# $string ~~ / (.+) ' ' (\d) ' to ' (\d) /; | |
# say type-map @$/ R[Z] Str:D, Int:D, Int:D; | |
# | |
# constant term:<$☺> := $; say ++$☺; | |
# | |
# my %h; | |
# %h{||<1 2 3>} = 42; | |
# dd %h; | |
# | |
# my &b = { 42 }; &b.say; | |
# my $buff = Buf.new( 0xff.rand.Int xx 200 ); dd $buff.elems; | |
# | |
# say "9 of spades".subst(/(\d+)/, -> Match:D ( $n, *@ ) { $n + 1 }); | |
# | |
# constant all-channels := none(); | |
# | |
# my $j = all-channels; | |
# | |
# dd $j; | |
# # $j = '#general#raku'; | |
# | |
# my $c = $j.split('#', :skip-empty).any; | |
# dd $c; | |
# say $c ~~ 'private'; | |
# | |
# say ().none ~~ 'name'; | |
# | |
# say %*ENV{%*ENV.grep(/^ SSH/)».key}:delete; | |
# say %*ENV; | |
# my $c = "#-raku-irc#-raku-dev-irc#-moarvom-irc#-webring-irc#raku"; | |
# $c = $c.split('#').&{ any( .cache.grep(/^ '-'/)».subst(/^ '-'/).none, .grep(/^ <-[-]>/).any) } | |
# dd $c; | |
# | |
# | |
# my regex identifier { | |
# <[a..z A..Z 0..9 . -]>+ | |
# } | |
# | |
# # say <irc.libera.chat#raku irc.libera.chat#raku-dev>.all ~~ /<identifier>+ '#' <identifier>/; | |
# for <irc.libera.chat#raku irc.libera.chat#raku-dev>».&{ m/(<identifier>) '#' (<identifier>)/.list».Str } -> [$server, $channel] { | |
# say [$server, $channel]; | |
# } | |
# | |
# class OnMessage does Callable { | |
# has $.state is rw; | |
# has &.callback; | |
# method CALL-ME(&callback) { self.Supply.tap: &callback } | |
# method Supply { Supplier.new.&{ start loop { sleep ¼; .emit: $++; .done if $++ > 2 }; .Supply } } | |
# } | |
# | |
# my &on-message = OnMessage.new; | |
# &on-message.state = 42; | |
# # react whenever &on-message { .say } | |
# on-message({ .say }); | |
# | |
# sub api-entry { stream => Supplier.new, 'answer' => -> { 42 }, 'unused' => {;} } | |
# | |
# with api-entry() -> (:$stream, :&answer, *%_) { | |
# dd $stream, &answer; | |
# } else { fail('bad‼'); } | |
# | |
# sub discord(|c) { | |
# say c[0][0].VAR.name; | |
# } | |
# discord(my ($message, $status)); | |
# | |
# dd $message, $status; | |
# sub s($a) { $a + 1 } | |
# | |
# say s(Junction.new('all', (42,45))); | |
# class C { | |
# has @.a is required; | |
# submethod TWEAK { | |
# die(‚class C is not feeling so well.‘) unless +@!a; | |
# } | |
# method a() { return-rw Proxy.new( | |
# FETCH => sub ($) { @!a }, | |
# STORE => sub ($, @new-a) { die ‚I want moar!‘ unless +@new-a; @!a := @new-a; } | |
# )} | |
# } | |
# | |
# constant $c = C.new(:a([1])); | |
# say $c.a; | |
# say $c.a = 1,2,3; | |
# say $c.a = []; | |
# my @a = 1..100; | |
# | |
# say (1..100).map: { .&next if .is-prime }; | |
# | |
# sub oi(@a) is pure { @a[*]:delete }; my @a = <1 2 3>; oi(@a); dd @a; | |
# sub whatever (List() $x) { shift $x } ; my @y = 1..3; whatever(@y); @y.say | |
# my &b = { … }; my &wc = * - 1 + *; my &p = -> { … }; (&b, &wc, &p)».signature».say; | |
# use MONKEY-SEE-NO-EVAL; | |
# my $chars = "cool".comb.unique.join; | |
# my $cc = EVAL 'my regex { <[' ~ $chars ~ ']> }'; | |
# my $foo = "cOOL"; say $foo ~~ /:i <$cc> /; | |
# .say for gather ($(12,84),).deepmap: *.take; | |
# my &s = sub { say 42 }; | |
# constant c = Channel.new; | |
# start react whenever c -> &s { s() } | |
# c.send(&s) for ^3; | |
# sleep 1; | |
# my Int() $sailors = @*ARGS.shift // 5; | |
# dd $sailors; | |
# my Date() $when = @*ARGS.shift // now; | |
# dd $when; | |
# sub MAIN( | |
# #| Dudes on (or formerly on) ships. | |
# Int $sailors, | |
# #| Why does the monkey never gets proper credit? | |
# Str :$monkey-name = "Bob", | |
# ) { | |
# dd $sailors; | |
# } | |
# my $input = '{ say "hello haxor!" }'; say 'ohai' ~~ /<$input>/; | |
# say :($a;; $b) ~~ :($a); | |
# my module Signal { | |
# our sub connect { | |
# say CLIENT::LEXICAL::.keys; | |
# my $o1 := CLIENT::LEXICAL::<$o1>; | |
# dd $o1; | |
# } | |
# } | |
# class C { | |
# method there { 42 } | |
# } | |
# | |
# constant NOT-THERE = ‚ENOTTHERE‘; | |
# | |
# my $c = C.new; | |
# | |
# my $v1 = $c.there; | |
# my $v2 = sub { return $c.not-there; CATCH { when X::Method::NotFound { return NOT-THERE } } }.(); | |
# | |
# dd $v1, $v2; | |
# $*IN = class :: is IO::Handle { | |
# has @.lines = <1 2 3>; | |
# method lines { gather { while @!lines { take shift @!lines } } } | |
# method nl-in(|) { } | |
# method chomp(|) { } | |
# method encoding(|) { } | |
# method IO { self } | |
# method open { self } | |
# method get { shift @!lines } | |
# }.new; | |
# | |
# multi sub MAIN(IO::Handle:D $handle) { | |
# say $handle.get; | |
# say $handle.lines; | |
# } | |
# | |
# multi sub MAIN(IO(Str) $in-file) { } | |
# | |
# multi sub MAIN() { MAIN($*IN) } | |
# sub infix:<yy>(&c, $n) { ^$n .map: &c } | |
# | |
# sub s() { state $++ } | |
# | |
# say { s } yy 10; | |
# subset Sane of Numeric where { ( .?denominator // 1 ) != 0 } | |
# | |
# my Sane:D $i = 42; | |
# $i = ½; | |
# $i = 1 / 0; | |
# subset Sane of Numeric where { | |
# ( .?denominator // 1 ) != 0 | |
# or fail("defusing 0 denominator in " ~ Backtrace.new.list.grep({!.is-setting})[1].&{.file ~ ' line ' ~ .line}) | |
# } | |
# | |
# my Sane:D $i = 42; | |
# $i = ½; | |
# $i = 1 / 0; | |
# say 1 ∈ (1/1, )».Int; | |
# proto sub infix:<(elem)>($, $, *% --> Bool:D) is pure {*} | |
# multi sub infix:<(elem)>(Numeric:D \a, Iterable:D \listy --> Bool:D) { | |
# Any.fail-iterator-cannot-be-lazy('∈', '') if listy.is-lazy; | |
# | |
# for listy -> \b { | |
# # return True if &*SET-COMPARATOR(a, b); | |
# return True if a == b; | |
# } | |
# | |
# False | |
# } | |
# | |
# multi sub infix:<(elem)>(Numeric:D \a, Setty:D \setty --> Bool:D) { | |
# Any.fail-iterator-cannot-be-lazy('∈', '') if setty.is-lazy; | |
# | |
# for setty.keys -> \b { | |
# return True if a == b; | |
# } | |
# | |
# False | |
# } | |
# | |
# constant &infix:<∈> := &infix:<(elem)>; | |
# | |
# proto sub infix:<(&)>(|) is pure {*} | |
# multi sub infix:<(&)>(Iterable:D \lhs, Iterable:D \rhs) { | |
# Any.fail-iterator-cannot-be-lazy('∩', '') if lhs.is-lazy || rhs.is-lazy; | |
# | |
# my @result; | |
# | |
# for lhs -> \l { | |
# for rhs -> \r { | |
# @result.push: r if l == r; | |
# } | |
# } | |
# | |
# +@result ?? @result.Set !! ∅ | |
# } | |
# | |
# multi sub infix:<(&)>(Iterable:D \lhs, Setty:D \rhs) { | |
# Any.fail-iterator-cannot-be-lazy('∩', '') if lhs.is-lazy || rhs.is-lazy; | |
# | |
# my @result; | |
# | |
# for lhs -> \l { | |
# for rhs.keys -> \r { | |
# @result.push: r if l == r; | |
# } | |
# } | |
# | |
# +@result ?? @result !! ∅ | |
# } | |
# | |
# multi sub infix:<(&)>(Setty:D \lhs, Iterable:D \rhs) { | |
# Any.fail-iterator-cannot-be-lazy('∩', '') if lhs.is-lazy || rhs.is-lazy; | |
# | |
# my @result; | |
# | |
# for lhs.keys -> \l { | |
# for rhs -> \r { | |
# @result.push: r if l == r; | |
# } | |
# } | |
# | |
# +@result ?? @result !! ∅ | |
# } | |
# | |
# multi sub infix:<(&)>(Setty:D \lhs, Setty:D \rhs) { | |
# Any.fail-iterator-cannot-be-lazy('∩', '') if lhs.is-lazy || rhs.is-lazy; | |
# | |
# my @result; | |
# | |
# for lhs.keys -> \l { | |
# for rhs.keys -> \r { | |
# @result.push: r if l == r; | |
# } | |
# } | |
# | |
# +@result ?? @result !! ∅ | |
# } | |
# | |
# constant &infix:<∩> := &infix:<(&)>; | |
# | |
# my &*SET-COMPARATOR = &infix:<==>; | |
# my $t; | |
# { | |
# $t = 1 ∈ (1/1, ) for ^1000000; | |
# say now - ENTER now; | |
# } | |
# say 1 ∈ (1/1, ).Set; | |
# # say (42, 42/2, 42/3) ∩ (1..∞).lazy; | |
# say (42, 42/2, 42/3) ∩ (1, 21, 3); | |
# say (42, 42/2, 42/3) ∩ (1, 21, 3).Set; | |
# say (42, 42/2, 42/3).Set ∩ (1, 21, 3); | |
# say (42, 42/2, 42/3).Set ∩ (1, 21, 3).Set; | |
# | |
# say 1 ~~ 1/1; | |
# | |
# my $n1 = 2; | |
# my $n2 = 1/1; | |
# my $n3 = 1.1; | |
# | |
# say (1, 1/1, 1.1, 2)».Rat».&{ .denominator == 1 }; | |
# | |
# say '1A2B' ~~ m:g/(<[\D] - [-]>+)/; | |
# | |
# # my ($a1:D, $b1:D) = "".split()[3,4]; | |
# my ($a1:D); | |
# sub find-gcd (*@nums where (@nums.all ~~ Numeric or fail(‚I don't know how to find a gcd for a non-number.‘))) { | |
# [gcd] .min, .max with @nums | |
# } | |
# | |
# find-gcd(2,5,6,9,10).say; | |
# find-gcd(<1 2 3>).say; | |
# my $s = '(1+(2*3)+((8)/4))+1'; | |
# say [max] [\+] $s.comb.map({ $_ eq '(' ?? 1 !! $_ eq ')' ?? -1 !! Empty }); | |
# my @accounts = (1,2,3, 5,5,5, 3,1,4).rotor(3); | |
# say @accounts».sum.max; | |
# sub random-life-init(Int $factor){ | |
# my @shape = (1,2) »*» $factor; | |
# my @field[||@shape]; | |
# # @field = (Bool.pick.Int xx *).rotor(@shape[1]); | |
# (Bool.pick.Int xx @shape[0]).Array xx @shape[1]; | |
# } | |
# | |
# my @field = random-life-init(5); | |
# say @field; | |
# say @field.map(&left-shift); | |
# say @field.map(&right-shift); | |
# say @field.&rot90; | |
# | |
# say my @t1 = @field.map(&left-shift), @field, @field.map(&right-shift); | |
# | |
# sub left-shift(@a) { | |
# (|@a xx 3)[(+@a+1) .. (2 * +@a)].Array | |
# } | |
# | |
# sub right-shift(@a) { | |
# (|@a xx 3)[(+@a-1) .. (2 * +@a - 2)].Array | |
# } | |
# | |
# sub rot90(@a) { | |
# my @new; | |
# | |
# for ^+@a[0] -> $col is raw { | |
# my @row; | |
# for ^+@a -> $row is raw { | |
# @row.push(@a[$row][$col]) | |
# } | |
# @new.push: @row.reverse.Array; | |
# } | |
# | |
# @new; | |
# } | |
# | |
# # sub foo { "Good &:greeting(now.DateTime.hour) $:name!" }; | |
# # say foo :name<Paul>, :greeting{$_ < 12 ?? 'morning' !! 'day'}; | |
# # say &foo.signature; | |
# | |
# constant dice = ('DIE FACE-' «~« (1..6))».uniparse.cache; | |
# | |
# my $o = Metamodel::ClassHOW.new_type(:name<UnMu>); | |
# $o.HOW.add_fallback($o, -> | { True }, -> $o, $name { say $name; my method foo (|) { dice.roll } }); | |
# # $o.^compose; | |
# # $o := Mu.^can('CREATE')[0]($o); | |
# | |
# $o.foo; | |
# | |
# dd $o; | |
# | |
# multi sub infix:<in>(\needle, List \l) { l.first(needle) ?? True !! False } | |
# multi sub infix:<in>(\needle, Iterable \l) { l.list.first(needle) ?? True !! False } | |
# multi sub infix:<in>(\needle, Str \s) { s.contains(needle) ?? True !! False } | |
# | |
# say 'foo' in 'foobar'; | |
# say 'foo' in 'barbuzz'; | |
# say '1' in <1 2 3>; | |
# say '1' in <0 2 3>; | |
# | |
# my @a = <1 2 3 4>; | |
# my &f = * - 1; | |
# say @a[&f]; | |
# &f.WHAT.say; | |
# my ($f, $i is default(42)) = (1, Nil); say $i; | |
# | |
# my ($k, $j is default(42)) = (1); | |
# say $j; | |
# | |
# my %h1 = female => {died => 127, survived => 339}, male => {died => 682, survived => 161}; | |
# # my %h2 = died => { female => %h1<female><died>, male => %h1<male><died> }, survived => { female => %h1<female><survived>, male => %h1<male><survived> }; | |
# | |
# my %h2; | |
# for (%h1{*;*}:kv).flat.rotor(3)».[1,0,2] -> [ $k1, $k2, $v ] { | |
# %h2{||[$k1, $k2]} = $v; | |
# } | |
# | |
# say %h2; | |
# Nil.^methods.grep(*.name eq 'FALLBACK').say; | |
# | |
# say "+\c[COMBINING RING ABOVE]"; | |
# multi sub prefix:«+\c[COMBINING RING ABOVE]»(\r) { r.defined ?? +r !! 0 }; say +̊(0, 0, 0).first: +̊*, :k; | |
# | |
# my $message = '<Anton Antonov#7232> <@!210313526928080896> Ok, using my own fold/reduce definition is good. Thanks!'; | |
# | |
# my $t = [[1,2,3],[2,3,4]]; | |
# | |
# sub has-homogenus-shape(\l) { | |
# so l[*].&{ $_».elems.all == .[0] } | |
# } | |
# | |
# my $t1 = [[1,2,3],[2,3,4]]; | |
# my $t2 = [[1,2,3],[2,3]]; | |
# | |
# say $t1.&has-homogenus-shape; | |
# say $t2.&has-homogenus-shape; | |
# | |
# my $t3 = [ | |
# [[1,2,3],[2,3,4]], | |
# [[1,2,3],[2,3,4]] | |
# ]; | |
# | |
# say $t3[*;*;1]; | |
# my $num = 5; | |
# say (1..$num) Z** (1..$num); | |
# say (1..5)».&{ $^a ** $^a }; | |
# | |
# multi sub dezip(Range $r) { | |
# my ($left, $right) = Seq, Seq; | |
# $left := $r.min, $r.min.succ.succ … $r.max.pred; | |
# $right := $r.min.succ, $r.min.succ.succ.succ … $r.max; | |
# | |
# $left, $right | |
# } | |
# | |
# say (0..0x10FFFF).&dezip; | |
# | |
# (my $oddject = Metamodel::ClassHOW.new_type(name => "")).&{.^add_method('FALLBACK', method ($name) { 'cheating' }); .^compose}; | |
# | |
# dd $oddject; | |
# say $oddject.foo; | |
# | |
# constant term:<$container> = 1,2,3; | |
# | |
# say $container, $container.WHAT; | |
# say "answer: $container"; | |
# dd $container; | |
# | |
# sub s($first, *@rest){ | |
# dd $first, @rest; | |
# } | |
# | |
# s($container); | |
# { | |
# my @a = ("apple", " banana", " peach", "blueberry", "pear", " plum", "kiwi"); | |
# | |
# multi sub merge-spacy([]) { () } | |
# multi sub merge-spacy([$x is copy, *@xs]) { | |
# if @xs[0].?starts-with(' ') { | |
# $x ~= @xs.shift; | |
# merge-spacy([|$x, |@xs]) | |
# } else { | |
# $x, |merge-spacy(@xs) | |
# } | |
# } | |
# | |
# say 'functional'; | |
# dd merge-spacy(@a); | |
# } | |
# | |
# { | |
# my @a = ("apple", " banana", " peach", "blueberry", "pear", " plum", "kiwi"); | |
# | |
# sub merge-with(@a, &c) { | |
# gather while @a.shift -> $e { | |
# if @a && &c(@a.head) { | |
# @a.unshift($e ~ @a.shift) | |
# } else { | |
# take $e; | |
# } | |
# } | |
# } | |
# | |
# say 'gather/take'; | |
# dd @a.&merge-with(*.starts-with(' ')); | |
# } | |
# | |
# { | |
# my @a = ("apple", " banana", " peach", "blueberry", "pear", " plum", "kiwi"); | |
# | |
# multi sub join(*@a, :&if!) { | |
# class :: does Iterable { | |
# method iterator { | |
# class :: does Iterator { | |
# has @.a; | |
# has &.if; | |
# method pull-one { | |
# return IterationEnd unless @!a; | |
# | |
# my $e = @!a.shift; | |
# return $e unless @!a; | |
# | |
# while &.if.(@!a.head) { | |
# $e ~= @!a.shift; | |
# } | |
# | |
# return $e; | |
# } | |
# }.new(a => @a, if => &if) | |
# } | |
# }.new | |
# } | |
# | |
# .say for join(@a, if => *.starts-with(' ')); | |
# } | |
# | |
# { | |
# my @a = ("apple", " banana", " peach", "blueberry", "pear", " plum", "kiwi"); | |
# | |
# # &c decides if the group is finished | |
# sub group-list(@a, &c) { | |
# my @group; | |
# gather while @a { | |
# my $e = @a.shift; | |
# my $next := +@a ?? @a.head !! Nil; | |
# @group.push($e); | |
# if !c($e, $next) { | |
# take @group.clone; | |
# @group = (); | |
# } | |
# } | |
# } | |
# | |
# dd @a.&group-list(-> $left, $right { $right && $right.starts-with(' ')}); | |
# } | |
# { | |
# say '123456712' ~~ / $<ref> = [ . ** 2 ] .+ $<ref> /; | |
# } | |
# { | |
# my $s = 'a😅'; | |
# say $s.subst(/<:Emoji>/, '', :g); | |
# } | |
# | |
# { | |
# sub infix:«|,»(\a, \e) { | |
# Proxy.new(FETCH => method { |a, |e }, | |
# STORE => method (\e) {}) | |
# but role { method sink { a.push: e } }; | |
# } | |
# | |
# my @a = 1,2,3; | |
# @a |, 4; | |
# dd @a; | |
# my @b = @a |, 5; | |
# dd @a, @b; | |
# } | |
# { | |
# my $a = 42; say $a.not-there(); CATCH { when X::Method::NotFound { .resume } } | |
# } | |
multi sub infix:«|,»(\left, \right) is equiv(&infix:<Z>) { |left, |right } | |
multi sub infix:«|xx»(Mu \left, Mu \right) is equiv(&infix:<xx>) { (left xx right).flat } | |
multi sub smart-join(&separators, *@l --> Str:D) { | |
my $ret; | |
while @l { | |
$ret ~= @l.shift; | |
$ret ~= separators if +@l; | |
} | |
$ret | |
} | |
multi sub smart-join(Seq:D \separator, *@l --> Str:D) { | |
my $ret; | |
my $sep-it = separator.iterator; | |
my $list-it = @l.iterator; | |
loop { | |
my $e := $list-it.pull-one; | |
last if $e =:= IterationEnd; | |
$ret ~= $e; | |
$ret ~= $sep-it.pull-one if $list-it.bool-only; | |
} | |
$ret | |
} | |
sub alternator(Mu \a, Mu \b) { | |
$++ %% 2 ?? a !! b | |
} | |
dd smart-join(&alternator.assuming(|<: ;>), <a b c d e f>); | |
# 1900..2100 | |
# ==> grep({ Date.new(.Int, 1, 1).day-of-week | Date.new(.Int, 12, 31).day-of-week == 4 }) | |
# # ==> smart-join({ ++$ %% 8 ?? $?NL !! ' '}) | |
# ==> smart-join( (' ' xx 7 |, $?NL) |xx ∞ ) | |
# ==> say(); | |
# | |
# put (1900..2100).grep({ Date.new(.Int, 1, 1).day-of-week | Date.new(.Int, 12, 31).day-of-week == 4 }); | |
# | |
# put ( (1900..2100).grep({ Date.new(.Int, 1, 1).day-of-week | Date.new(.Int, 12, 31).day-of-week == 4 }) Z (' ' xx 7 |, $?NL) |xx ∞ ).flat.head(*-1).join(''); | |
# | |
# multi sub smart-split(Str $s, &needle) { | |
# my @ret; | |
# my $str-it = $s.comb.iterator; | |
# my $section = Empty; | |
# | |
# loop { | |
# my $e := $str-it.pull-one; | |
# last if $e =:= IterationEnd; | |
# | |
# dd $e; | |
# | |
# if needle($e) { | |
# @ret.push: $section; | |
# $section = ''; | |
# } else { | |
# $section ~= $e; | |
# } | |
# } | |
# | |
# @ret | |
# } | |
# | |
# 'a0b1c0d'.&smart-split({ .Numeric ~~ Numeric }).say; | |
# | |
# my %h1 = a => 2, b => 4, c => 0.1; | |
# my %h2 = :4a, :6b, :1c; | |
# | |
# say %h1 »+« %h2; | |
# { | |
# my $s = Empty; dd $s; say $s.defined; say ().defined; say "".defined; say Empty.defined; | |
# my $a = 1; my $b; say $a + $b; | |
# } | |
{ | |
my $year = 2021; | |
my Int() $samoa-correction = $year == 2011 && slurp('/etc/timezone').match(/ 'Pacific/Apia' /).so; | |
say (Date.new(:$year) .. Date.new($year + 1, 1, 1).earlier(:1day)).grep({ .day-of-week !== 6|7}).elems - $samoa-correction; | |
} | |
{ | |
my &me = method ($o: $p) { dd $o, $p }; | |
my &bound = &me.assuming(42); | |
bound('I ♥ Raku‼'); | |
} | |
{ | |
(my $oddject = Metamodel::ClassHOW.new_type(name => "")).&{.^add_method('$methodd', method () { 'LOLWUT‽' }); .^compose}; | |
say $oddject."\$methodd"(); | |
} | |
# { | |
# while my Str:D $input = prompt 'Please be so kind to enter an integer: ' { | |
# last if $input ~~ / ^^ '-'? \d+ $$ /; | |
# } | |
# | |
# } | |
{ | |
say <a b c>.map({ |($_, $_.uc) }).join('|'); | |
} | |
# { | |
# role QuotedStr { | |
# method new() is rw { | |
# my $theStr = ""; | |
# Proxy.new( | |
# STORE => method (Str() $new is raw) { $theStr = $new.subst(/ [ ^ || <-[\\]> ] '"' /, '\"', :g) }, | |
# FETCH => method () { $theStr but QuotedStr } | |
# ) | |
# } | |
# } | |
# | |
# my $qs := QuotedStr.new; | |
# $qs = "abc"; dd $qs; | |
# $qs = 12; dd $qs; | |
# $qs = '"ABC"'; say $qs; | |
# | |
# sub testy(QuotedStr $s is raw) is rw { | |
# say $s ~~ QuotedStr; | |
# | |
# $s | |
# } | |
# | |
# sub changy(QuotedStr $s is raw ) is rw { | |
# $s = $s ~ '-changed'; | |
# } | |
# | |
# dd $qs.&testy.&changy; | |
# } | |
# { | |
# multi sub postfix:<minute>(Numeric() \seconds) { seconds * 60 } | |
# multi sub postfix:<minutes>(Numeric() \seconds) { seconds * 60 } | |
# multi sub postfix:<hour>(Numeric() \seconds) { seconds * 3600 } | |
# multi sub postfix:<hours>(Numeric() \seconds) { seconds * 3600 } | |
# multi sub postfix:<day>(Numeric() \seconds) { seconds * 86400 } | |
# multi sub postfix:<days>(Numeric() \seconds) { seconds * 86400 } | |
# | |
# react whenever Supply.interval(5minutes) { | |
# note ‚taking snapshot‘; | |
# use Perl6::Compiler:from<NQP>; | |
# sub compress(Str() $file-name) { run «lz4 -BD --rm -qf $file-name» } | |
# | |
# my $filename = 'raku-bot-' ~ now.DateTime.&{ .yyyy-mm-dd ~ '-' ~ .hh-mm-ss } ~ '.mvmheap'; | |
# Perl6::Compiler.profiler-snapshot(kind => "heap", filename => $filename<>); | |
# # $filename.&compress or warn(‚compression failed‘); | |
# | |
# note ‚done‘; | |
# } | |
# } | |
# | |
# { | |
# for [2, 3, 4; 3, 3, 6; 200, 300, 40; 0, 0, 0] -> [$i, $j, $k] { | |
# put ((1..$i) X* (1..$j)).sort[$k - 1]; | |
# | |
# CATCH { when X::OutOfRange { warn „Sorry, I can't find position $k in the multiplication table of $i and $j.“ } } | |
# } | |
# } | |
# { | |
# class A { has %.f is Set[Str]; } | |
# say A.new.f.WHAT; | |
# } | |
# { | |
# my @a = (1,2,3,4) xx 4; | |
# cross(||@a).say; | |
# } | |
# { | |
# use Test; | |
# | |
# class A { has @.b }; | |
# my @b = 1,2,3; | |
# my %a = :@b; | |
# dd %a; | |
# | |
# is-deeply A.new(|Pair.new('b', @b)), A.new(:@b) | |
# } | |
# | |
# { | |
# put [Z~] (^10 X ^50).map(-> [$a, $b] { '.' ~ ($b == 49 ?? "\n" !! '') }); | |
# } | |
# { | |
# use Test; | |
# ok 1 ~~ TR/\#//, 'Backslashed # is parsed correctly in a regex'; | |
# is (TR/\#/a/ with '#'), 'a', 'transliteration of # works'; | |
# } | |
# { | |
# multi sub possible($a, $b where { $a == $b² }) { say „$a is $b²“ } | |
# multi sub possible($a, $b where { $a =~= $b²}) { say „$a is possibly $b²“ } | |
# multi sub possible(| ($a, $b)) { say „$a is not $b²“ } | |
# | |
# possible 1, -1; | |
# possible 2, sqrt(2); | |
# possible 4, 4; | |
# } | |
# { | |
# sub postfix:<?>(\p) { p } | |
# say my $hey-raku-what-kind-of-problems-can-i-solve-with-you?; | |
# } | |
# { | |
# sub rakudoconfuser(Str $) {}; | |
# my &clonish = &rakudoconfuser.clone; | |
# &rakudoconfuser.wrap: sub (Int $) {}; | |
# my &alias = &rakudoconfuser; | |
# alias(42); | |
# clonish("answer"); | |
# | |
# sub wrap-pure(&s, &wrapper) { | |
# my &ret = &s.clone; | |
# my $handle = &ret.wrap(&wrapper); | |
# &ret but class :: { has $.wrap-handle }.new(:wrap-handle($handle)) | |
# } | |
# | |
# sub subby(Str $) {} | |
# my &tubby = wrap-pure &subby, sub (Int $i) { callwith $i.Str }; | |
# tubby(42); | |
# subby(‚answer‘); | |
# | |
# | |
# multi sub trait_mod:<is>(Sub $s, :$looping){ | |
# $s.wrap: sub (*@a) { callwith($_) for @a } | |
# } | |
# | |
# sub forgotname(Str() $s) is looping { | |
# say $s; | |
# } | |
# | |
# ::("&forgotname").(1,2,3); | |
# } | |
my @a = 1..10; | |
sub divide(&condition, *@a) { | |
my (@r-a, @r-b); | |
.&condition ?? @r-a.push($_) !! @r-b.push($_) for @a; | |
@r-a, @r-b | |
} | |
use MONKEY-TYPING; | |
augment class List { | |
method divide(&condition) { | |
my (@r-a, @r-b); | |
.&condition ?? @r-a.push($_) !! @r-b.push($_) for self; | |
@r-a, @r-b | |
} | |
} | |
List.^compose; | |
my (@b, @c) := divide * %% 2, @a; | |
dd @b, @c; | |
my (@e, @f); | |
@a ==> divide({ Bool.pick }) | |
==> say(); | |
# dd @e, @f; | |
say @a.divide({Bool.pick}); | |
sub divide2(&conditional, *@a) { | |
my $arity = &conditional.arity; | |
my @ret; | |
if &conditional.arity == 0 { | |
for @a { | |
@ret[conditional].push: $_ | |
} | |
} elsif $arity == 1 { | |
my @ret; | |
for @a { | |
@ret[.&conditional].push: $_ | |
} | |
} else { | |
my @streams = @a.rotor($arity); | |
for @streams { | |
my $order = conditional(|$_); | |
dd $_, $order; | |
@ret.push: $_[$order] | |
} | |
} | |
@ret; | |
} | |
say divide2({Bool.roll}, 1..10); | |
say divide2({$^a %% 2}, 1..10); | |
say divide2({(0..2).roll}, 1..10); | |
say divide2(sub { Bool.roll }, 1..10); | |
say divide2({ $^a > $^b }, (1..10).pick(*)); | |
# { | |
# (^∞).hyper.grep({$_ !%% 2 && .is-prime})[^100000].sum.say; | |
# say now - ENTER now; | |
# } | |
# { | |
# (^∞).hyper(:batch(5000)).grep({$_ !%% 2 && .is-prime})[^100000].sum.say; | |
# say now - ENTER now; | |
# } | |
# { | |
# sub hyper(+@a, *%_) { @a.hyper(|%_) } | |
# sub tail(+@a, *%_) { @a.tail(|%_) } | |
# | |
# sub is-palindrom(\v) { v eq v.flip } | |
# sub is-boring(\s) { my \c = s.comb; c[0] eq c.all } | |
# my @primes = ^10_000 .grep(*.is-prime); | |
# (@primes X @primes) | |
# ==> hyper(:batch(1000)) | |
# ==> grep({ (.[0] * .[1]).&{ .&is-palindrom & .&is-boring } }) | |
# ==> map({ [ .[0,1], .[0] * .[1] ] }) | |
# ==> unique(:as(*[1])) | |
# ==> sort(*[1] <=> *[1]) | |
# ==> tail(20) | |
# ==> map({ .[0;0] ~ ' * ' ~ .[0;1] ~ ' = ' ~ .[1] }) | |
# ==> join($?NL) | |
# ==> put(); | |
# | |
# # .flat.say for (@primes X @primes).hyper(:batch(1000)).grep({ my \p = .[0] * .[1]; p.&is-palindrom & p.&is-boring }).map({ [.[0,1], .[0] * .[1] ] }).unique(:as(*[1])).sort(*[1] <=> *[1]).tail(20); | |
# say now - ENTER now; | |
# | |
# my @a = hyper for ^100_000 { .is-prime }; | |
# say @a; | |
# } | |
# { | |
# | |
# sub chain(&op where *.count == 2, \listy where *.^can('rotor')) { | |
# for listy.rotor(2=>-1) -> [\a, \b] { | |
# return False unless op(a, b) | |
# } | |
# | |
# True | |
# } | |
# | |
# my $res; | |
# ($res += chain(&[eq], <1 2 1 1 1 1 1 1 1>)) for ^100000; | |
# say [$res, now - ENTER now]; | |
# } | |
# { | |
# my $res; | |
# | |
# ($res += ('1' eq <1 2 1 1 1 1 1 1 1>.all).Bool) for ^100000; | |
# say [$res, now - ENTER now]; | |
# | |
# } | |
{ | |
sub frontend(|c ($arg1, $arg2, $arg3?)) { | |
my \d = \(|c[0,1], c[2] // 42); | |
backend(|d); | |
} | |
sub backend($arg1, $arg2, $arg3) { | |
say $arg1, $arg2, $arg3 | |
} | |
frontend(1,2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment