Skip to content

Instantly share code, notes, and snippets.

@gfldex
Created January 26, 2022 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfldex/647eb0925ec65bdba11107381557d974 to your computer and use it in GitHub Desktop.
Save gfldex/647eb0925ec65bdba11107381557d974 to your computer and use it in GitHub Desktop.
use v6.*;
{
multi sub hyperize(&code, '«') {
sub (@little, @large) {
((@little xx *).flat Z @large).flat.map(&code)
}
}
my &hyper-assume = hyperize({&^a.assuming($^b)}, '«');
sub sc($_) {
.words».&{ hyper-assume((&lc, &uc), .comb)».().join }
}
for ^10 {
# say sc "sarcasmcase FOR YOU";
}
say now - ENTER now;
}
{
sub simple-assume(&routine, |tail) {
sub (|c) {
my \real-args = \(|tail, |c);
routine(|real-args)
}
}
# sub f1($a, $b, $c) {
# dd $a, $b, $c;
# }
# my &f2 = simple-assume(&f1, 42);
# f2(1,2);
multi sub hyperize(&code, '«') {
sub (@little, @large) {
((@little xx *).flat Z @large).flat.map(&code)
}
}
my &hyper-assume = hyperize({simple-assume(&^a, $^b)}, '«');
sub sc($_) {
.words».&{ hyper-assume((&lc, &uc), .comb)».().join }
}
for ^9000 {
sc "sarcasmcase FOR YOU";
}
say now - ENTER now;
}
{
class Assumed is Callable {
has &!routine;
has @!static-args;
has %!static-named;
has @!mapping;
submethod BUILD(:&routine, :@static-args, :%static-named) {
&!routine = &routine;
@!static-args = @static-args;
%!static-named = %static-named;
}
submethod TWEAK {
my $index = 0;
for @!static-args {
@!mapping[$++] = $index if $_ ~~ Whatever;
$index++;
}
}
method CALL-ME(|sparse-args) {
my @applied-args = @!static-args;
@applied-args[@!mapping] = sparse-args;
&!routine.(|@applied-args, |%!static-named)
}
method name { &!routine.name }
method Signature {
my @params = @!static-args.map: { Parameter.new(:name(.Str)) };
Signature.new(:@params, :returns(&!routine.Signature.returns)); # this could be cached
}
}
multi sub assuming(&routine, |static-args) {
Assumed.new(:&routine, :static-args(|static-args), :static-named(static-args.hash))
}
# sub f1($a, $b, $c, $d, *%_) {
# dd $a, $b, $c, $d, %_;
# }
# my &f2 = assuming(&f1, *, *, 42, *, :9000named);
# f2(1,2,3);
multi sub hyperize(&code, '«') {
sub (@little, @large) {
((@little xx *).flat Z @large).flat.map(&code)
}
}
my &hyper-assume = hyperize({assuming(&^a, $^b)}, '«');
sub sc($_) {
.words».&{ hyper-assume((&lc, &uc), .comb)».().join }
}
for ^9000 {
sc "sarcasmcase FOR YOU";
}
say now - ENTER now;
}
{
sub assuming(&routine, |static-args) {
if static-args.list > 1 { # complex case?
my @mapping;
my $index = 0;
for static-args {
@mapping[$++] = $index if $_ ~~ Whatever;
$index++;
}
sub (|sparse-args) {
my @applied-args = |static-args;
@applied-args[@mapping] = sparse-args;
&routine.(|@applied-args, |static-args.hash)
}
} else { # simple case!
sub (|sparse-args) {
my \applied-args = \(|static-args, |sparse-args, |static-args.hash);
&routine(|applied-args)
}
}
}
# sub f1($a, $b, $c, $d, *%_) {
# dd $a, $b, $c, $d, %_;
# }
# my &f2 = assuming(&f1, *, *, 42, *, :9000named);
# f2(1,2,3);
multi sub hyperize(&code, '«') {
sub (@little, @large) {
((@little xx *).flat Z @large).flat.map(&code)
}
}
my &hyper-assume = hyperize({assuming(&^a, $^b)}, '«');
sub sc($_) {
.words».&{ hyper-assume((&lc, &uc), .comb)».().join }
}
for ^9000 {
sc "sarcasmcase FOR YOU";
}
say now - ENTER now;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment