You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+27-7Lines changed: 27 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -136,7 +136,7 @@ Outside we can use asynchronous version:
136
136
*`channel.awrite(x)` will write `x` and return to us `Future[Unit]` which will be executed after x will send
137
137
*`channel.aread` will return future to the value, which will be read.
138
138
139
-
Also, channels can be closed. After this attempt to write will cause throwing 'ClosedChannelException'. Reading will be still possible up to 'last written value', after this attempt to read will cause the same exception.
139
+
Also, channels can be closed. After this attempt to write will cause throwing 'ClosedChannelException.' Reading will be still possible up to 'last written value', after this attempt to read will cause the same exception.
140
140
141
141
Note, closing channels is not mandatory; unreachable channels are garbage-collected regardless of they are closed or not.
142
142
@@ -166,7 +166,7 @@ Also, note that you can provide own Input and Output implementations by implemen
166
166
'select statement' is somewhat similar to Unix 'select' syscall:
167
167
from a set of blocking operations select one who is ready to input/output and run it.
168
168
169
-
The typical pattern of channel processing in go language is to wrap select operation into an endless loop.
169
+
The usual pattern of channel processing in go language is to wrap select operation into an endless loop.
170
170
171
171
Gopher provides similar functionality:
172
172
@@ -202,7 +202,7 @@ go{
202
202
~~~
203
203
204
204
205
-
Inside case actions, we can use blocking read/writes and await operations. Call of doExit in the implicit instance of `FlowTermination[T]` (for a forever loop this is `FlowTermination[Unit]`) can be used for exiting from the loop. `select.exit` and `selecet.shutdown` macroses can be used as shortcats.
205
+
Inside case actions, we can use blocking read/writes and await operations. Call of doExit in the implicit instance of `FlowTermination[T]` (for a forever loop this is `FlowTermination[Unit]`) can be used for exiting from the loop. `select.exit` and `select.shutdown` macroses can be used as shortcuts.
206
206
207
207
Example:
208
208
@@ -223,10 +223,10 @@ val consumer = gopherApi.select.forever{
223
223
Await.ready(consumer, 5.second)
224
224
~~~
225
225
226
-
Combination from variable and select loop better modeled with help 'fold over select':
226
+
Combination of variable and select loop better modeled with help 'fold over select' construction:
@@ -250,6 +250,15 @@ val fib = select.afold((0,1)) { case ((x,y), s) =>
250
250
}
251
251
~~~
252
252
253
+
Also, we can use 'map over select' to represent results of handling of different events as input side of channel:
254
+
255
+
~~~scala
256
+
valmultiplexed= select amap {
257
+
case x:ch1.read => (s1,x)
258
+
case y:ch2.read => (s2,y)
259
+
}
260
+
~~~
261
+
253
262
254
263
For using select operation not enclosed in a loop, scala-gopher provide
255
264
*select.once* syntax:
@@ -264,7 +273,6 @@ gopherApi.select.once{
264
273
265
274
Such form can be called from any environment and will return `Future[String]`. Inside `go` you can wrap this in await of use 'for' syntax as with `forever`.
266
275
267
-
268
276
~~~scala
269
277
go {
270
278
.....
@@ -292,6 +300,17 @@ go {
292
300
}
293
301
~~~
294
302
303
+
amap - map
304
+
305
+
~~~scala
306
+
valmultiplexed=for(s <- select) yield
307
+
s match {
308
+
case x:ch1.read => (s1,x)
309
+
case y:ch2.read => (s2,y)
310
+
}
311
+
~~~
312
+
313
+
295
314
## Effected{Input,Output,Channel}
296
315
297
316
One useful programming pattern, often used in CSP-style programming: have a channel from wich we read (or to where we write) as a part of a state. In Go language, this is usually modelled as a mutable variable, changed inside the same select statement, where one is read/written.
@@ -345,7 +364,8 @@ Transformers are build hierarchically with help of 3 operations:
345
364
346
365
### Select transputer
347
366
348
-
Let's look at a simple example: transputer with two input ports and one output. When same number has come from `inA` and `inB`, then
367
+
Let's look at a simple example: transputer with two input ports and one output.
368
+
When the same number has come from `inA` and `inB`, then
349
369
transputer prints `Bingo` on console and output this number to `out`:
0 commit comments