Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gfldex
Created July 14, 2022 10: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/f9c7ea9d5457a2967e99c8dbcc997140 to your computer and use it in GitHub Desktop.
Save gfldex/f9c7ea9d5457a2967e99c8dbcc997140 to your computer and use it in GitHub Desktop.
Hi Hillel,
you where asking for a very strange programming in "Six Programming
Languages I'd Like to See". We already made that for you. It is called
Raku (was called Perl 6) and can be found at www.raku.org.
The following is valid Raku code:
try {
class :: { # I didn't came up with a good name, so I didn't gave one to this class.
subset PositiveRat of Rat where * ≥ 0;
has PositiveRat() $.balance = 0; # We could do the value check on exit of method withdraw with a trait. But this is bad practice. Just do it for the whole
class.
method withdraw(Int $amnt where { $amnt ≤ self.balance }) {
self.balance -= $amnt;
}
}.new.withdraw(1);
CATCH { default { .&warn } }
}
# So you want to test your tests?
sub window1($n, *@a) {
@a »*» $n # this is a hyperoperator of *, think: vector operations and more
}
sub window2($n, *@a) {
@a.map(* * $n).Array
}
use Test;
my @values = (2; 1, 2, 3, 4), (4; 1, 2, 3, 4);
for @values -> @args {
is-deeply window1(|@args), window2(|@args), „same for @args[]?“ # we like e-mail-adresses in our strings, so we demand a zen-slice for interpolating @-sigiled
symbols
}
# Everything is not a graph. Larry is mad, not insane.
# Sadly, we don't got factorials build in. So you may need to load a module for your calculator.
# $ raku -Mcalculator # REPL with preloading a module
# this goes into calculator.rakumod, and yes, we can define custom operatos with ease
multi sub postfix:<!>(Int $n) { [*] 1..$n }
multi sub postfix:<!>(@ns) { @ns.map: *! }
# this goes into the REPL, you don't _have_ to type 'say' here
say [*] (10,20,30)!; # [*] takes the infix operator * and turns it into a reduction meta-operator
# you like Excel? Excellent!
my $input = 1;
my &out1 = { $input + 1 }; # raku is so functional that blocks are first-class citizen
$input = 2;
say out1;
# out1.replace(+, -) is not possible right now. The compiler frontent is being rewritten right now, so we may be able to serve you next year with then new
macro-system
# you can have as many types as you want, whenever you want them
my $type = Metamodel::ClassHOW.new_type(name => "NewType", ver => v0.0.1, auth => 'github:perl6' );
$type.HOW.add_method($type,"hey", method { say "Hey" });
$type.hey; # OUTPUT: «Hey␤»
$type.HOW.compose($type);
my $instance = $type.new;
$instance.hey; # OUTPUT: «Hey␤»
# you can capture a type of a parameter at runtime and do stuff with it
sub foo(::T \s) {
say „The type of s is {T.^name}.“;
}
foo 1/2; # The type of s is Rat.
foo "42"; # The type of s is Str.
# a container with a value constraint
my $v where (10 < * < 50) = 42;
try {
# this fails when $v tries to cross 50;
$v++ for ^10;
CATCH { when X::TypeCheck::Assignment { say ‚$v foolishly tried to cross 50.‘ } }
}
I hope you find something you like. There is a lot more you could have wished for. How about using all those CPU-cores while finding primes that contain 666?
.say for (^∞).hyper(:batch(1000), :degree(16)).grep({ .is-prime && .contains('666') })
# Output lazily, for all integers up to infinite. Use 16 threads for sub-lists in batches of 1000 elements. Grep the values that are prime and contain 666.
Don't worry, there is no GIL. Also the devil seems to like primes.
I have loads of fun with Raku and hope to see you soon on IRC.
With love from raku.land
Wenzel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment