@@ -293,6 +293,75 @@ object Tag extends LazyLogging {
293293 }
294294 }
295295
296+ object Async {
297+
298+ import Monad ._
299+
300+ def foldLeft [A , U , T [_]](initial : U , after : Option [A ], stream : swaydb.Stream [A , T ], drop : Int , take : Option [Int ], operation : (U , A ) => U )(implicit monad : Monad [T ]): T [U ] = {
301+ def fold (previous : A , drop : Int , currentSize : Int , previousResult : U ): T [U ] =
302+ if (take.contains(currentSize))
303+ monad.success(previousResult)
304+ else
305+ stream
306+ .next(previous)
307+ .flatMap {
308+ case Some (next : A ) =>
309+ if (drop >= 1 ) {
310+ fold(next, drop - 1 , currentSize, previousResult)
311+ } else {
312+ try {
313+ val newResult = operation(previousResult, next)
314+ fold(next, drop, currentSize + 1 , newResult)
315+ } catch {
316+ case throwable : Throwable =>
317+ monad.failed(throwable)
318+ }
319+ }
320+
321+ case None =>
322+ monad.success(previousResult)
323+ }
324+
325+ if (take.contains(0 ))
326+ monad.success(initial)
327+ else
328+ after
329+ .map(stream.next)
330+ .getOrElse(stream.headOption)
331+ .flatMap {
332+ case Some (first) =>
333+ if (drop >= 1 ) {
334+ fold(first, drop - 1 , 0 , initial)
335+ } else {
336+ try {
337+ val nextResult = operation(initial, first)
338+ fold(first, drop, 1 , nextResult)
339+ } catch {
340+ case throwable : Throwable =>
341+ monad.failed(throwable)
342+ }
343+ }
344+
345+ case None =>
346+ monad.success(initial)
347+ }
348+ }
349+
350+ def collectFirst [A , T [_]](previous : A , stream : swaydb.Stream [A , T ], condition : A => Boolean )(implicit monad : Monad [T ]): T [Option [A ]] =
351+ stream
352+ .next(previous)
353+ .flatMap {
354+ case some @ Some (nextA) =>
355+ if (condition(nextA))
356+ monad.success(some)
357+ else
358+ collectFirst(nextA, stream, condition)
359+
360+ case None =>
361+ monad.success(None )
362+ }
363+ }
364+
296365 implicit val throwableIO : Tag .Sync [IO .ThrowableIO ] =
297366 new Tag .Sync [IO .ThrowableIO ] {
298367 override val unit : IO .ThrowableIO [Unit ] =
@@ -465,64 +534,22 @@ object Tag extends LazyLogging {
465534 def isComplete [A ](a : Future [A ]): Boolean =
466535 a.isCompleted
467536
468- override def foldLeft [A , U ](initial : U , after : Option [A ], stream : swaydb.Stream [A , Future ], drop : Int , take : Option [Int ])(operation : (U , A ) => U ): Future [U ] = {
469- def fold (previous : A , drop : Int , currentSize : Int , previousResult : U ): Future [U ] =
470- if (take.contains(currentSize))
471- Future .successful(previousResult)
472- else
473- stream
474- .next(previous)
475- .flatMap {
476- case Some (next) =>
477- if (drop >= 1 ) {
478- fold(next, drop - 1 , currentSize, previousResult)
479- } else {
480- try {
481- val newResult = operation(previousResult, next)
482- fold(next, drop, currentSize + 1 , newResult)
483- } catch {
484- case throwable : Throwable =>
485- Future .failed(throwable)
486- }
487- }
488-
489- case None =>
490- Future .successful(previousResult)
491- }
492-
493- if (take.contains(0 ))
494- Future .successful(initial)
495- else
496- after.map(stream.next).getOrElse(stream.headOption) flatMap {
497- case Some (first) =>
498- if (drop >= 1 ) {
499- fold(first, drop - 1 , 0 , initial)
500- } else {
501- try {
502- val nextResult = operation(initial, first)
503- fold(first, drop, 1 , nextResult)
504- } catch {
505- case throwable : Throwable =>
506- Future .failed(throwable)
507- }
508- }
509-
510- case None =>
511- Future .successful(initial)
512- }
513- }
537+ override def foldLeft [A , U ](initial : U , after : Option [A ], stream : swaydb.Stream [A , Future ], drop : Int , take : Option [Int ])(operation : (U , A ) => U ): Future [U ] =
538+ swaydb.Tag .Async .foldLeft(
539+ initial = initial,
540+ after = after,
541+ stream = stream,
542+ drop = drop,
543+ take = take,
544+ operation = operation
545+ )
514546
515547 override def collectFirst [A ](previous : A , stream : swaydb.Stream [A , Future ])(condition : A => Boolean ): Future [Option [A ]] =
516- stream.next(previous) flatMap {
517- case some @ Some (nextA) =>
518- if (condition(nextA))
519- Future .successful(some)
520- else
521- collectFirst(nextA, stream)(condition)
522-
523- case None =>
524- Future .successful(None )
525- }
548+ swaydb.Tag .Async .collectFirst(
549+ previous = previous,
550+ stream = stream,
551+ condition = condition
552+ )
526553
527554 override def fromIO [E : IO .ExceptionHandler , A ](a : IO [E , A ]): Future [A ] = a.toFuture
528555 }
0 commit comments