mirror of git://git.qorg11.net/kill9.git
haha no
This commit is contained in:
parent
cefcc7ba28
commit
c6bfb6c148
|
@ -11,14 +11,14 @@ promises, lazy evaluation, and grammars.
|
|||
|
||||
Sigils in Raku make sense unlike in Perl, for example:
|
||||
|
||||
<code class="prettyprint">
|
||||
<pre class="prettyprint">
|
||||
# Perl:
|
||||
my %hash = (key1 => "value", key2 => "value2");
|
||||
$hash{key1} # value;
|
||||
# Raku:
|
||||
my %hash = (key1 => "value", key2 => "value2");
|
||||
%hash{"key1"} # Value
|
||||
</code>
|
||||
</pre>
|
||||
|
||||
If a hash is a hash, it is a hash, it won't be converted to a scalar
|
||||
when you want to retrieve data from that hash. Unlike in Perl.
|
||||
|
@ -28,10 +28,10 @@ variables you declare are of the type "Any", which means that variable
|
|||
can have any time and can be converted to other type. Nevertheless you
|
||||
can specify which type you want the variable to be:
|
||||
|
||||
<code class="prettyprint">
|
||||
<pre class="prettyprint">
|
||||
my Int $n = 3;
|
||||
my Str $s = "hello world";
|
||||
</code>
|
||||
</pre>
|
||||
|
||||
There is a type hierarchy, which means types are based on other
|
||||
types. All types are based on the type `Mu`[^1].
|
||||
|
@ -42,7 +42,7 @@ from `Mu`. All roads lead to Rome and all types lead to `Mu`.
|
|||
|
||||
Raku also has a decent Object Oriented interface:
|
||||
|
||||
<code class="prettyprint">
|
||||
<pre class="prettyprint">
|
||||
Class Socket {
|
||||
has Int $.sockfd;
|
||||
|
||||
|
@ -59,7 +59,7 @@ Class Socket {
|
|||
|
||||
my $sock = Socket.new("path/to/socket");
|
||||
$sock.write("hello from raku!");
|
||||
</code>
|
||||
</pre>
|
||||
|
||||
Calling C functions from Raku is stupidly easy, to do this only use
|
||||
the [NativeCall](https://docs.raku.org/language/nativecall) module
|
||||
|
@ -67,7 +67,7 @@ which is included in the standard library, consider the following:
|
|||
|
||||
file.c:
|
||||
|
||||
<code class="prettyprint">
|
||||
<pre class="prettyprint">
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
|
@ -76,7 +76,7 @@ print_string(const char *s)
|
|||
puts(s);
|
||||
return 0;
|
||||
}
|
||||
</code>
|
||||
</pre>
|
||||
|
||||
You have to compile it so it's a shared library:
|
||||
|
||||
|
@ -84,7 +84,7 @@ You have to compile it so it's a shared library:
|
|||
|
||||
file.raku:
|
||||
|
||||
<code class="prettyprint">
|
||||
<pre class="prettyprint">
|
||||
use NativeCall;
|
||||
|
||||
# Libpath, raku will add the lib and the .so automatically so you don't
|
||||
|
@ -97,7 +97,7 @@ sub print_string(Str --> int32) is native(LIBPATH) { * }
|
|||
|
||||
print_string("C function running in raku");
|
||||
|
||||
</code>
|
||||
</pre>
|
||||
|
||||
It will output:
|
||||
|
||||
|
@ -116,13 +116,13 @@ really easily, but if you *really* want to call `fork()` you can call
|
|||
Async functions in raku are something very easy. The [language
|
||||
website](https://raku.org) give us this example:
|
||||
|
||||
<code class="prettyprint">
|
||||
<pre class="prettyprint">
|
||||
start { sleep 1.5; print "hi" }
|
||||
await Supply.from-list(<A B C D E F>).throttle: 2, {
|
||||
await Supply.from-list(<A B C D E F>).throttle: 2, {
|
||||
sleep 0.5;
|
||||
.print
|
||||
}
|
||||
</code>
|
||||
</pre>
|
||||
|
||||
|
||||
Output is "ABCDhiEF"
|
||||
|
@ -134,7 +134,7 @@ print the "hi" form the `start {}` block.
|
|||
|
||||
Raku also has promises:
|
||||
|
||||
<code class="prettyprint">
|
||||
<pre class="prettyprint">
|
||||
|
||||
sub counter(Int $n) {
|
||||
for 0..$n -> $i {
|
||||
|
@ -153,7 +153,7 @@ $promise.then({say .result}); # Will print "There" after finishing.
|
|||
say "I'm doing other stuff";
|
||||
say "Blah, blah";
|
||||
sleep(1000); # Simulate stuff-doing
|
||||
</code>
|
||||
</pre>
|
||||
|
||||
the `.then` method takes as parameter a block of code, which will be
|
||||
executed after finishing the process. In that code I use `sleep(1000)`
|
||||
|
|
Loading…
Reference in New Issue