Skip to content

Commit f0c4e8a

Browse files
committed
added tests for fold
1 parent e8dd3a7 commit f0c4e8a

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ val s = goScope{
107107

108108
## Channels
109109

110-
You can look on the channel as on classic blocked queue with fixed size. Different execution flows can exchange messages via channels.
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.
111111

112112

113113
val channel = gopherApi.makeChannel[Int];

notes/0.99.7.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
be readed at https://github.com/rssh/notes/blob/master/2016_03_05_see-ma-no-vars.md )
88
- akka 2.4.2, scala-2.11.8
99
- channels by defaults now unbuffered. (as in Go)
10+
- implemented fold over input
1011

1112

src/main/scala/gopher/channels/Input.scala

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,26 @@ trait Input[A]
318318

319319
}
320320

321+
/**
322+
* async incarnation of fold. Fold return future, which successed when channel is closed.
323+
*Operations withing fold applyed on result on each other, starting with s0.
324+
*```
325+
* val fsum = ch.afold(0){ (s, n) => s+n }
326+
*```
327+
* Here in fsum will be future with value: sum of all elements in channel until one has been closed.
328+
**/
329+
def afold[S,B](s0:S)(f:(S,A)=>S): Future[S] = macro InputMacro.afoldImpl[A,S]
321330

331+
/**
332+
* fold opeations, available inside async bloc.
333+
*```
334+
* go {
335+
* val sum = ch.fold(0){ (s,n) => s+n }
336+
* }
337+
*```
338+
*/
322339
def fold[S,B](s0:S)(f:(S,A)=>S): S = macro InputMacro.foldImpl[A,S]
323340

324-
def afold[S,B](s0:S)(f:(S,A)=>S): Future[S] = macro InputMacro.afoldImpl[A,S]
325341

326342

327343
def afoldSync[S,B](s0:S)(f:(S,A)=>S): Future[S] =

src/test/scala/gopher/channels/InputOpsSuite.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,15 @@ class InputOpsSuite extends FunSuite with AsyncAssertions {
221221
w.await(timeout(10 seconds), dismissals(2))
222222
}
223223

224+
test("Input afold on stream with 'N' elements inside ") {
225+
val ch = gopherApi.makeChannel[Int]()
226+
val f = ch.afold(0)((s,e)=>s+1)
227+
val ar = ch.awriteAll(1 to 10)
228+
ar.onComplete{ case _ => ch.close() }
229+
val r = Await.result(f,10 seconds)
230+
assert(r==10)
231+
}
232+
224233
test("forech with mapped closed stream") {
225234
def one(i:Int) = {
226235
val w = new Waiter
@@ -257,6 +266,29 @@ class InputOpsSuite extends FunSuite with AsyncAssertions {
257266
w.await(timeout(10 seconds), dismissals(2))
258267
}
259268

269+
/*
270+
test("channel fold with async operation inside") {
271+
val ch1 = gopherApi.makeChannel[Int](10)
272+
val ch2 = gopherApi.makeChannel[Int](10)
273+
val fs = go {
274+
val sum = ch1.fold(0){ (s,n) =>
275+
val n1 = ch2.read
276+
//s+(n1+n2) -- stack overflow in 2.11.8 compiler. TODO: submit bug
277+
s+(n+n1)
278+
}
279+
sum
280+
}
281+
go {
282+
ch1.writeAll(1 to 10)
283+
ch2.writeAll(1 to 10)
284+
ch1.close()
285+
}
286+
val r = Await.result(fs, 10 seconds)
287+
assert(r==110)
288+
}
289+
*/
290+
291+
260292
test("append for finite stream") {
261293
val w = new Waiter
262294
val ch1 = gopherApi.makeChannel[Int](10)

0 commit comments

Comments
 (0)