SourceDrainOps

ox.channels.SourceDrainOps
trait SourceDrainOps[+T]

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
trait Source[T]
class Channel[T]
Self type
Source[T]

Members list

Value members

Concrete methods

def drain(): Unit

Receives all elements from the channel. Blocks until the channel is done.

Receives all elements from the channel. Blocks until the channel is done.

Attributes

Throws
ChannelClosedException.Error

when there is an upstream error.

def drainSafe(): Unit | Error

The "safe" variant of drain.

The "safe" variant of drain.

Attributes

def fold[U](zero: U)(f: (U, T) => U): U

Uses zero as the current value and applies function f on it and a value received from this source. The returned value is used as the next current value and f is applied again with the value received from a source. The operation is repeated until the source is drained.

Uses zero as the current value and applies function f on it and a value received from this source. The returned value is used as the next current value and f is applied again with the value received from a source. The operation is repeated until the source is drained.

Value parameters

f

A binary function (a function that takes two arguments) that is applied to the current value and value received from a source.

zero

An initial value to be used as the first argument to function f call.

Attributes

Returns

Combined value retrieved from running function f on all source elements in a cumulative manner where result of the previous call is used as an input value to the next.

Throws
ChannelClosedException.Error

When receiving an element from this source fails.

exception

When function f throws an exception then it is propagated up to the caller.

Example
 import ox.*
 import ox.channels.Source
 supervised {
   Source.empty[Int].fold(0)((acc, n) => acc + n)       // 0
   Source.fromValues(2, 3).fold(5)((acc, n) => acc - n) // 0
 }
def foldSafe[U](zero: U)(f: (U, T) => U): U | Error

The "safe" variant of fold.

The "safe" variant of fold.

Attributes

def foreach(f: T => Unit): Unit

Invokes the given function for each received element. Blocks until the channel is done.

Invokes the given function for each received element. Blocks until the channel is done.

Attributes

Throws
ChannelClosedException.Error

When there is an upstream error.

def foreachSafe(f: T => Unit): Unit | Error

The "safe" variant of foreach.

The "safe" variant of foreach.

Attributes

def last(): T

Returns the last element from this source or throws NoSuchElementException when this source is empty. In case when receiving an element fails then ChannelClosedException.Error exception is thrown. Note that last is a terminal operation leaving the source in ChannelClosed.Done state.

Returns the last element from this source or throws NoSuchElementException when this source is empty. In case when receiving an element fails then ChannelClosedException.Error exception is thrown. Note that last is a terminal operation leaving the source in ChannelClosed.Done state.

Attributes

Returns

A last element if source is not empty or throws otherwise.

Throws
ChannelClosedException.Error

When receiving an element from this source fails.

NoSuchElementException

When this source is empty.

Example
 import ox.*
 import ox.channels.Source
 supervised {
   Source.empty[Int].last()        // throws NoSuchElementException("cannot obtain last element from an empty source")
   val s = Source.fromValues(1, 2)
   s.last()                        // 2
   s.receive()                     // ChannelClosed.Done
 }
def lastOption(): Option[T]

Returns the last element from this source wrapped in Some or None when this source is empty. Note that lastOption is a terminal operation leaving the source in ChannelClosed.Done state.

Returns the last element from this source wrapped in Some or None when this source is empty. Note that lastOption is a terminal operation leaving the source in ChannelClosed.Done state.

Attributes

Returns

A Some(last element) if source is not empty or None otherwise.

Throws
ChannelClosedException.Error

When receiving an element from this source fails.

Example
 import ox.*
 import ox.channels.Source
 supervised {
   Source.empty[Int].lastOption()  // None
   val s = Source.fromValues(1, 2)
   s.lastOption()                  // Some(2)
   s.receive()                     // ChannelClosed.Done
 }
def lastOptionSafe(): Option[T] | Error

The "safe" variant of lastOption.

The "safe" variant of lastOption.

Attributes

def pipeTo(sink: Sink[T]): Unit

Passes each received element from this channel to the given sink. Blocks until the channel is done. When the channel is done or there's an error, propagates the closed status downstream and returns.

Passes each received element from this channel to the given sink. Blocks until the channel is done. When the channel is done or there's an error, propagates the closed status downstream and returns.

Attributes

def reduce[U >: T](f: (U, U) => U): U

Uses the first and the following (if available) elements from this source and applies function f on them. The returned value is used as the next current value and f is applied again with the value received from this source. The operation is repeated until this source is drained. This is similar operation to fold but it uses the first source element as zero.

Uses the first and the following (if available) elements from this source and applies function f on them. The returned value is used as the next current value and f is applied again with the value received from this source. The operation is repeated until this source is drained. This is similar operation to fold but it uses the first source element as zero.

Value parameters

f

A binary function (a function that takes two arguments) that is applied to the current and next values received from this source.

Attributes

Returns

Combined value retrieved from running function f on all source elements in a cumulative manner where result of the previous call is used as an input value to the next.

Throws
ChannelClosedException.Error

When receiving an element from this source fails.

NoSuchElementException

When this source is empty.

exception

When function f throws an exception then it is propagated up to the caller.

Example
 import ox.*
 import ox.channels.Source
 supervised {
   Source.empty[Int].reduce(_ + _)    // throws NoSuchElementException("cannot reduce an empty source")
   Source.fromValues(1).reduce(_ + _) // 1
   val s = Source.fromValues(1, 2)
   s.reduce(_ + _)                    // 3
   s.receive()                        // ChannelClosed.Done
 }
def takeLast(n: Int): List[T]

Returns the list of up to n last elements from this source. Less than n elements is returned when this source contains less elements than requested. The List.empty is returned when takeLast is called on an empty source.

Returns the list of up to n last elements from this source. Less than n elements is returned when this source contains less elements than requested. The List.empty is returned when takeLast is called on an empty source.

Value parameters

n

Number of elements to be taken from the end of this source. It is expected that n >= 0.

Attributes

Returns

A list of up to n last elements from this source.

Throws
ChannelClosedException.Error

When receiving an element from this source fails.

Example
 import ox.*
 import ox.channels.Source
 supervised {
   Source.empty[Int].takeLast(5)    // List.empty
   Source.fromValues(1).takeLast(0) // List.empty
   Source.fromValues(1).takeLast(2) // List(1)
   val s = Source.fromValues(1, 2, 3, 4)
   s.takeLast(2)                    // List(4, 5)
   s.receive()                      // ChannelClosed.Done
 }
def takeLastSafe(n: Int): List[T] | Error

The "safe" variant of takeLast.

The "safe" variant of takeLast.

Attributes

def toList: List[T]

Accumulates all elements received from the channel into a list. Blocks until the channel is done.

Accumulates all elements received from the channel into a list. Blocks until the channel is done.

Attributes

Throws
ChannelClosedException.Error

When there is an upstream error.

def toListSafe: List[T] | Error

The "safe" variant of toList.

The "safe" variant of toList.

Attributes