Skip to content

Commit b005b2c

Browse files
committed
Merge branch 'master' of github.com:perl6/doc
2 parents 4c6cfae + 293c2ba commit b005b2c

File tree

1 file changed

+62
-60
lines changed

1 file changed

+62
-60
lines changed

lib/Language/concurrency.pod

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,18 @@ A L<Promise> can also be scheduled to be automatically kept at a future time:
9494
my $promise2 = $promise1.then(-> $v { say $v.status; 'Second Result' });
9595
say $promise2.result;
9696
97-
The method C<in> creates a new promise and schedules a new task to call
97+
The L<method in|/type/Promise#method_in> creates a new promise and schedules a new task to call
9898
C<keep> on it no earlier than the supplied number of seconds, returning
99-
the new L<Promise> object. This is almost directly equivalent to calling
100-
C<Scheduler.cue> with a code block that will C<keep> the returned promise.
99+
the new L<Promise> object.
101100
102-
Possibly the most frequent requirement would be to schedule some code to
103-
be executed as soon as possible and obtain a promise that will be kept
104-
(or broken) when the code completes (or throws an exception,) this is
105-
performed by the method C<start> :
101+
A very frequent use of promises is to run a piece of code, and keep the
102+
promise once it returns successfully, or break it when the code dies.
103+
The L<start method|/type/Promise#method_start> provides a shortcut for that:
106104
107-
my $promise = Promise.start({ my $i = 0; for 1 .. 10 { $i += $_ }; $i});
108-
say $promise.result;
105+
my $promise = Promise.start(
106+
{ my $i = 0; for 1 .. 10 { $i += $_ }; $i}
107+
);
108+
say $promise.result; # 55
109109
110110
Here the C<result> of the promise returned is the value returned from
111111
the code. Similarly if the code fails (and the promise is thus broken,)
@@ -116,62 +116,64 @@ then C<cause> will be the L<Exception> object that was thrown:
116116
say $promise.cause;
117117
118118
This is considered to be such a commonly required pattern that it is
119-
provided as subroutines:
120-
121-
my $result = await start {
122-
my $i = 0;
123-
for 1 .. 10 {
124-
$i += $_
125-
}
126-
$i
127-
}
119+
also provided as subroutines:
120+
121+
my $promise = start {
122+
my $i = 0;
123+
for 1 .. 10 {
124+
$i += $_
125+
}
126+
$i
127+
}
128+
my $result = await $promise;
128129
say $result;
129130
130131
C<await> is almost equivalent to calling C<result> on the promise object
131132
returned by C<start> but it will also take a list of promises and return
132133
the result of each:
133134
134-
my $result = await start({
135-
my $i = 0;
136-
for 1 .. 10 {
137-
$i += $_
138-
}
139-
$i
140-
}),
141-
start({
142-
my $i = 0;
143-
for 1 .. 10 {
144-
$i -= $_
145-
}
146-
$i
147-
});
148-
say $result;
149-
150-
In addition to C<await> two methods are provided that will combine a set pf
151-
L<Promise> objects into a new promise: C<allof> which returns a promise that
135+
my $p1 = start {
136+
my $i = 0;
137+
for 1 .. 10 {
138+
$i += $_
139+
}
140+
$i
141+
};
142+
my $p2 = start {
143+
my $i = 0;
144+
for 1 .. 10 {
145+
$i -= $_
146+
}
147+
$i
148+
};
149+
my @result = await $p1, $p2;
150+
say @result; # 55 -55
151+
152+
In addition to C<await>, two class methods combine several
153+
L<Promise> objects into a new promise: C<allof> returns a promise that
152154
is kept when all the original promises are kept or broken when one of them
153155
is broken:
154156
155157
my $promise = Promise.allof(
156-
Promise.in(2),
157-
Promise.in(3)
158-
);
158+
Promise.in(2),
159+
Promise.in(3)
160+
);
159161
160162
await $promise;
161-
say "All done"; # Should be no less than three seconds later
163+
say "All done"; # Should be not much more than three seconds later
162164
163-
And C<anyof> which returns a new promise that will be kept when any of the
165+
And C<anyof> returns a new promise that will be kept when any of the
164166
original promises is kept or broken when any of them is broken:
165167
166168
my $promise = Promise.anyof(
167-
Promise.in(3),
168-
Promise.in(8600)
169-
);
169+
Promise.in(3),
170+
Promise.in(8600)
171+
);
170172
171173
await $promise;
172174
say "All done"; # Should be about 3 seconds later
173175
174-
unlike C<await> however the results of the original kept promises are not
176+
Unlike C<await> however the results of the original kept promises are not
175177
available without referring to the original, so these are more useful
176178
when the completion or otherwise of the tasks is more important to the
177179
consumer than the actual results, or the results have been collected by
@@ -185,12 +187,12 @@ consumed by one or more consumers simultaneously in a manner similar to
185187
"events" in other programming languages and can be seem as enabling
186188
"Event Driven" or reactive designs.
187189
188-
L<Supply> is a role that can be composed into your own types, but will
190+
L<Supply> is a role that can be composed into your own types, but will
189191
frequently be used as a "type punned" class to create standalone objects.
190192
191193
At its simplest a L<Supply> is a message stream that can have multiple
192194
subscribers created with the method C<tap> on to which data items can be
193-
placed with C<emit> :
195+
placed with C<emit>:
194196
195197
my $supply = Supply.new();
196198
@@ -211,25 +213,25 @@ interested in the events:
211213
$supply.emit("Won't trigger the tap");
212214
213215
Calling C<close> on the L<Tap> object (or calling C<close> on the supply
214-
object with the tap object as an argument,) will trigger the callback
216+
object with the tap object as an argument,) triggers the callback
215217
specified by C<closing> if present and stop further events being sent to
216218
the tap callback but otherwise leave the supply intact to receive further
217-
events. Calling C<done> on the supply object will similarly call the
218-
C<done> callback that may be specified for any taps but will not prevent any
219+
events. Calling C<done> on the supply object similarly calls the
220+
C<done> callback that may be specified for any taps, but does not prevent any
219221
further events being emitted to the stream, or taps receiving them.
220222
221-
The method C<interval> will return a new supply which will automatically
222-
emit a new event at the specified interval, the data that is emitted will
223-
be an integer starting at 0 that will be incremented for each event. The
224-
following code will output 0 .. 5 :
223+
The method C<interval> returns a new supply which peridically
224+
emits a new event at the specified interval. The data that is emitted
225+
is an integer starting at 0 that is incremented for each event. The
226+
following code outputs 0 .. 5 :
225227
226228
my $supply = Supply.interval(2);
227229
$supply.tap(-> $v { say $v });
228230
sleep 10;
229231
230-
A second argument can be supplied which will specify a delay in seconds
232+
A second argument can be supplied which specifies a delay in seconds
231233
before the first event is fired. Each tap of a supply created by
232-
C<interval> will have its own sequence starting from 0, as illustrated
234+
C<interval> has its own sequence starting from 0, as illustrated
233235
by the following:
234236
235237
my $supply = Supply.interval(2);
@@ -241,8 +243,8 @@ by the following:
241243
An existing supply object can be filtered or transformed, using the methods
242244
C<grep> and C<map> respectively, to create a new supply in a manner like
243245
the similarly named list methods: C<grep> returns a supply such that only
244-
those events emitted on the source stream for which the expression provided
245-
to the C<grep> will be enitted:
246+
those events emitted on the source stream for which the C<grep> condition
247+
is true is emitted on the second supply:
246248
247249
my $supply = Supply.new;
248250
$supply.tap(-> $v { say "Original : $v" });
@@ -254,9 +256,9 @@ to the C<grep> will be enitted:
254256
$supply.emit($_);
255257
}
256258
257-
C<map> will return a new supply such that for each item emitted to the
259+
C<map> returns a new supply such that for each item emitted to the
258260
original supply a new item which is the result of the expression passed
259-
to the C<map> will be emitted:
261+
to the C<map> is emitted:
260262
261263
my $supply = Supply.new;
262264
$supply.tap(-> $v { say "Original : $v" });

0 commit comments

Comments
 (0)