Skip to content

Commit 4434eb1

Browse files
committed
added description of map over select
1 parent 79137c8 commit 4434eb1

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

README.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Outside we can use asynchronous version:
136136
* `channel.awrite(x)` will write `x` and return to us `Future[Unit]` which will be executed after x will send
137137
* `channel.aread` will return future to the value, which will be read.
138138

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.
140140

141141
Note, closing channels is not mandatory; unreachable channels are garbage-collected regardless of they are closed or not.
142142

@@ -166,7 +166,7 @@ Also, note that you can provide own Input and Output implementations by implemen
166166
'select statement' is somewhat similar to Unix 'select' syscall:
167167
from a set of blocking operations select one who is ready to input/output and run it.
168168

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.
170170

171171
Gopher provides similar functionality:
172172

@@ -202,7 +202,7 @@ go{
202202
~~~
203203

204204

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.
206206

207207
Example:
208208

@@ -223,10 +223,10 @@ val consumer = gopherApi.select.forever{
223223
Await.ready(consumer, 5.second)
224224
~~~
225225

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:
227227

228228
~~~ scala
229-
val consumer = gopherApi.select.afold(0) { (state, selector) =>
229+
val sum = gopherApi.select.afold(0) { (state, selector) =>
230230
selector match {
231231
case i: channel.read =>
232232
val nstate = state + i
@@ -250,6 +250,15 @@ val fib = select.afold((0,1)) { case ((x,y), s) =>
250250
}
251251
~~~
252252

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+
val multiplexed = select amap {
257+
case x:ch1.read => (s1,x)
258+
case y:ch2.read => (s2,y)
259+
}
260+
~~~
261+
253262

254263
For using select operation not enclosed in a loop, scala-gopher provide
255264
*select.once* syntax:
@@ -264,7 +273,6 @@ gopherApi.select.once{
264273

265274
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`.
266275

267-
268276
~~~ scala
269277
go {
270278
.....
@@ -292,6 +300,17 @@ go {
292300
}
293301
~~~
294302

303+
amap - map
304+
305+
~~~ scala
306+
val multiplexed = 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+
295314
## Effected{Input,Output,Channel}
296315

297316
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:
345364

346365
### Select transputer
347366

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
349369
transputer prints `Bingo` on console and output this number to `out`:
350370

351371
~~~ scala

0 commit comments

Comments
 (0)