Skip to content

Commit 77466ee

Browse files
committed
updated README with information about doc-channel.
1 parent 53df7df commit 77466ee

File tree

1 file changed

+57
-5
lines changed

1 file changed

+57
-5
lines changed

README.md

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
### Dependences:
66

77
* scala 2.11.7 +
8-
* akka 2.4.1 +
8+
* akka 2.4.2 +
99
* scala-async 0.9.5
1010

1111
#### Download:
1212

1313
libraryDependencies += "com.github.rssh" %% "scala-gopher" % "0.99.6"
1414

15+
(or `0.99.7-SNAPSHOT` for development version).
16+
1517
Scala-gopher is open source (license is Apache2); binaries are available from the maven-central repository.
1618

1719
## Overview
@@ -107,7 +109,9 @@ val s = goScope{
107109

108110
## Channels
109111

110-
Inside go blocks, you can look on the channel as on classic blocked queue with fixed size. Different execution flows can exchange messages via channels.
112+
Channels are used for asynchronous communication between execution flows.
113+
114+
When using channel inside *go* block, you can look at one as on classic blocked queue with fixed size with methods read and write:
111115

112116

113117
val channel = gopherApi.makeChannel[Int];
@@ -136,8 +140,11 @@ Also, channels can be closed. After this attempt to write will cause throwing 'C
136140

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

139-
Also, you can use only 'Input' or 'Output' interfaces, where appropriative read/write operations are defined.
140-
For an input, exists usual collection functions, like `map`, `zip`, `takeN`. Scala Iterable can be represented as `channels.Input` via method `gopherApi.iterableInput`. Also, we can use Scala futures as channels, which produce one value and then closes. For obtaining such input use `gopherApi.futureInput`.
143+
144+
Channels can be buffered and unbuffered. In a unbuffered channel, write return control to the caller after another side actually will start processing; buffered channel force provider to wait only if internal channel buffer is full.
145+
146+
Also, you can use only `Input` or `Output` interfaces, where appropriative read/write operations are defined.
147+
For `Input`, exists usual collection functions, like `map`, `zip`, `takeN`, `fold` ... etc. Scala Iterable can be represented as `channels.Input` via method `gopherApi.iterableInput`. Also, we can use Scala futures as channels, which produce one value and then closes. For obtaining such input use `gopherApi.futureInput`.
141148

142149
`|` (i.e. or) operator used for merged inputs, i.e. `(x|y).read` will read a value from channel x or y when one will be available.
143150

@@ -154,7 +161,7 @@ will return two inputs, where reading from `inReady` will return the same as rea
154161
Also, note that you can provide own Input and Output implementations by implementing callback `cbread` and `cbwrite` methods.
155162

156163

157-
## Select loops
164+
## Select loops and folds
158165

159166
'select statement' is somewhat similar to Unix 'select' syscall:
160167
from a set of blocking operations select one who is ready to input/output and run it.
@@ -216,6 +223,34 @@ val consumer = gopherApi.select.forever{
216223
Await.ready(consumer, 5.second)
217224
~~~
218225

226+
Combination from variable and select loop better modeled with help 'fold over select'>
227+
228+
~~~ scala
229+
val consumer = gopherApi.select.afold(0) { (state, selector) =>
230+
selector match {
231+
case i: channel.read =>
232+
val nstate = state + i
233+
if (i==1000) {
234+
implictily[FlowTermination[Int]].doExit(nstate)
235+
}
236+
nstate
237+
}
238+
}
239+
~~~
240+
241+
242+
More than one variables in state can be modelled with partial function case syntax:
243+
244+
~~~ scala
245+
val fib = select.afold((0,1)) { case ((x,y), s) =>
246+
s match {
247+
case x:channel.write => (y,y+x)
248+
case q:quit.read => CurrentFlowTermination.exit((x,y))
249+
}
250+
}
251+
~~~
252+
253+
219254
For using select operation not enclosed in a loop, scala-gopher provide
220255
*select.once* syntax:
221256

@@ -242,6 +277,23 @@ go {
242277
}
243278
~~~
244279

280+
281+
and afold become fold:
282+
283+
~~~ scala
284+
go {
285+
...
286+
val sum = select.fold(0) { (n,s) =>
287+
s match {
288+
case x: channelA.read => n+x
289+
case q: quit.read => CurrentFlowTermination.exit(n)
290+
}
291+
}
292+
}
293+
~~~
294+
295+
296+
245297
## Transputers
246298

247299
The logic of data transformation between channels can be encapsulated in special `Transputer` concept. (Word 'transputer' was chosen

0 commit comments

Comments
 (0)