Raku
Raku (formerly Perl6) is a a really cool language I've been using rakubrew
Here are some examples of features the actual source code can be found bellow in an order other than that which I expound them:
here is a hello world program that pauses for input at the end.
#!/usr/bin/env raku
use v6;
my $msg = "Howdy";
say "$msg Cowboy";
say $*IN.get;
say $*IN.lines(1);
This will actually pause for two lines of input and write both of them out. I did this to show the two ways I've found to get input form stdin now called $*IN. the program actually says "Howdy Cowboy" rather than "Hello World" but since when have I been traditional. and here is another example:
<pre>
unit module Fact;
multi sub postfix:<!>(Int $n) is export {
[*] 1..$n;
}
sub prefix:<√>(Num $n) is export {
return sqrt($n);
}
sub infix:<.oO>($name, $thought) is export {
say "$name thinks $thought"
}
sub infix:<+->($a, $b) is export { ($a - $b) .. ($a + $b) }
</pre>
Note the parameters are typed also note I'm defining new operators including a postfix ! for factorial, here is an example of them in use.
#!/usr/bin/env raku
use v6;
use Fact;
say 0!;
say 1!;
say 6!;
say 10!;
say √ 64.Num;
say √√√256.Num;
"grizzly" .oO "Perl 6 is cool";
say 2.78 ~~ 10 +- 3 ?? 't' !! 'f'; # f
say 7.5 ~~ 10 +- 3 ?? 't' !! 'f'; # t
say 13 ~~ 10 +- 3 ?? 't' !! 'f'; # t
say 13.1 ~~ 10 +- 3 ?? 't' !! 'f'; # f
Note the cast to turn Ints into Nums I shouldn't have to do this but Rakudo cannot seam to work out that Ints and Rats are Nums I'm using the current Trunk version from github to get (git clone git://github.com/rakudo/rakudo.git), Niecza cannot handle the postfix ! it lets you define this, but when you use it it thinks a prefix not ! is being abused.
here are some more examples:
#!/usr/bin/env raku
use v6;
for @*ARGS -> $arg {
my Num $target = $arg.Num;
my Num $guess = $target;
while (abs( $guess**2 - $target ) > 0.000000000000000005) {
$guess += ( $target - $guess**2 ) / ( 2 * $guess );
say $guess;
}
say '=' x 80;
}
This shows how you can type variables it also shows the new @*ARGS array which replaces @ARGV as ARGV is really quite cryptic sorry C guys.
here is cat using the Shell::Command module from the File::Tools library, (see the Libraries page).
#!/usr/bin/env raku
use v6;
use Shell::Command;
cat(|@*ARGS);
#`«
for @*ARGS -> $arg {
given open($arg) {
for .lines -> $line {
say $line;
}
.close;
}
}
»
Note | is the flatten operator now bitwise or is +| for numeric and ~| for string bitwise or also note +& is numeric bitwise and and ~& for string bitwise and.
Also note the #`« … » multi-line comment around the alternate implementation, you can use #`( … ) or substitute any number of [ or { followed by the same number of closing brackets or braces or parenthesis or use as many French quotes as you like « and athe same number of closing French quotes » so #`(((( some stuff
on multiple
lines ))))
some more stuff can
is a valid comment.
here are some links for further reading