The Channel class also defines a method that checks on whether the channel is closed. This method is called closed. my $c = Channel.new; say “open” if !$c.closed; # is openΒ $c.close; say “closed” if $c.closed; # closed Despite the simplicity of using the method, it in fact returns not a simple Boolean value but … Continue reading “π The closed method in Perl 6 channels”
Category: Chapter 5. New Concepts
π Chapter 5. New Concepts in Perl 6
π Promises in Perl 6
Promises are objects aimed to help synchronize parallel processes. The simplest use case involving them is to notify if the earlier given promise is kept or broken or if its status is not yet known.
π The list method in Perl 6 channels
The list method accompanies the previously seen methods and returns everything that is left unread in the channel. my $c = Channel.new;Β $c.send(5); $c.send(6);Β $c.close; say $c.list; # (5 6) The method blocks the programme until the channel is open, thus it is wise to close it before calling the list method.
π Factory methods in Perl 6 promises
There are a few factory methods defined in the Promise class.
π The in and at methods in Perl 6 promises
The other two factory methods, Promise.in and Promise.at, create a promise, which will be kept after a given number of seconds or by a given time. For example: my $p = Promise.in(3); for 1..5 { Β Β Β say $p.status; Β Β Β sleep 1; } The programme prints the following lines. Planned Planned Planned Kept Kept That means … Continue reading “π The in and at methods in Perl 6 promises”
π Read and write in Perl 6 channels
In Perl 6, there is a predefined class Channel, which includes, among the others, the send and the receive methods. Here is the simplest example, where an integer number first is being sent to the channel $c and is then immediately read from it. my $c = Channel.new; $c.send(42); say $c.receive; # 42 A channel … Continue reading “π Read and write in Perl 6 channels”
π Transfer non-scalar objects through Perl 6 channels
Channels may also transfer both arrays and hashes and do it as easily as they work with scalars. Unlike Perl 5, an array will not be unfolded to a list of scalars but will be passed as a single unit. Thus, you may write the following code. my $c = Channel.new; my @a = (2, … Continue reading “π Transfer non-scalar objects through Perl 6 channels”
π Channels in Perl 6
Perl 6 includes a number of solutions for parallel and concurrent calculations. The great thing is that this is already built-in into the language and no external libraries are required. The idea of the channels is simple. You create a channel through which you can read and write. It is a kind of a pipe … Continue reading “π Channels in Perl 6”
π Basics of promises in Perl 6
The Promise.new constructor builds a new promise. The status of it can be read using the status method. Before any other actions are done with the promise, its status remains to be Planned. my $p = Promise.new; say $p.status; # Planned When the promise is kept, call the keep method to update the status to … Continue reading “π Basics of promises in Perl 6”
π The start keyword in Perl 6 promises
The start method creates a promise containing a block of code. There is an alternative way to create a promise by calling Promise.start via the start keyword. my $p = start { Β Β Β 42 } (Note that in Perl 6, a semicolon is assumed after a closing brace at the end of a line.) The … Continue reading “π The start keyword in Perl 6 promises”
π The then method in Perl 6 promises
The then method, when called on an already existing promise, creates another promise, whose code will be called after the βparentβ promise is either kept or broken. my $p = Promise.in(2); my $t = $p.then({say “OK”}); # Prints this in two seconds say “promised”; # Prints immediately sleep 3; say “done”; The code above produces … Continue reading “π The then method in Perl 6 promises”
π The anyof and allof methods in Perl 6 promises
Another pair of factory methods, Promise.anyof and Promise.allof, creates new promises, which will be only kept when at least one of the promises (in the case of anyof) is kept or, in the case of allof, all of the promises listed at the moment of creation are kept. One of the useful examples found in … Continue reading “π The anyof and allof methods in Perl 6 promises”
π An example of using promises: Sleep sort in Perl 6
Finally, a funny example of how promises can be used for implementing the sleep sort algorithm. In sleep sort, every integer number, consumed from the input, creates a delay proportional to its value. As the sleep is over, the number is printed out. Promises are exactly the things that will execute the code and tell … Continue reading “π An example of using promises: Sleep sort in Perl 6”