This API documentation is for Ballerina 1.2.0. View API documentation for the latest release.

Functions - lang.stream

close

Closes a stream.

filter

Selects the members from a stream for which a function returns true.

forEach

Applies a function to each member of a stream.

iterator

Returns an iterator over a stream.

map

Applies a function to each member of a stream and returns a stream of the results.

next

Returns the next element in the stream wrapped in a record or () if the stream ends.

reduce

Combines the members of a stream using a combining function.

close

(stream<Type ,ErrorType> stm)

returns ErrorType?

Closes a stream. This releases any system resources being used by the stream.

Parameters

  • stm stream<Type ,ErrorType>
  • the stream to close

  • Return Type

    (ErrorType?)
  • () if the close completed successfully, otherwise an error

filter

(stream<Type ,ErrorType> stm, function(Type) returns (boolean) func)

returns stream<Type ,ErrorType>

Selects the members from a stream for which a function returns true.

Parameters

  • stm stream<Type ,ErrorType>
  • the stream

  • func function(Type) returns (boolean)
  • a predicate to apply to each member to test whether it should be selected

  • Return Type

    (stream<Type ,ErrorType>)
  • new stream only containing members of stm for which func evaluates to true

forEach

(stream<Type ,ErrorType> stm, function(Type) returns (()) func)

returns ErrorType?

Applies a function to each member of a stream. The Combining function is applied to each member of stream in order.

Parameters

  • stm stream<Type ,ErrorType>
  • the stream

  • func function(Type) returns (())
  • a function to apply to each member

  • Return Type

    (ErrorType?)
  • An error if iterating the stream encounters an error

iterator

(stream<Type ,ErrorType> stm)

returns object { public function next () returns ((record {| (any|error) value; |}|error?)?); }

Returns an iterator over a stream.

Parameters

  • stm stream<Type ,ErrorType>
  • the stream

  • Return Type

    (object { public function next () returns ((record {| (any|error) value; |}|error?)?); })
  • a new iterator object that will iterate over the members of stm.

map

(stream<Type ,ErrorType> stm, function(Type) returns (Type1) func)

returns stream<Type1 ,ErrorType>

Applies a function to each member of a stream and returns a stream of the results.

Parameters

  • stm stream<Type ,ErrorType>
  • the stream

  • func function(Type) returns (Type1)
  • a function to apply to each member

  • Return Type

    (stream<Type1 ,ErrorType>)
  • new stream containing result of applying func to each member of stm in order

Returns the next element in the stream wrapped in a record or () if the stream ends.

Parameters

  • strm stream<Type ,ErrorType>
  • The stream

  • Return Type

    (record {| (any|error) value; |} | ErrorType | ())
  • If the stream has elements, return the element wrapped in a record with single field called value, otherwise returns ()

reduce

(stream<Type ,ErrorType> stm, function(Type1, Type) returns (Type1) func, Type1 initial)

returns Type1 | ErrorType

Combines the members of a stream using a combining function. The combining function takes the combined value so far and a member of the stream, and returns a new combined value.

Parameters

  • stm stream<Type ,ErrorType>
  • the stream

  • func function(Type1, Type) returns (Type1)
  • combining function

  • initial Type1
  • initial value for the first argument of combining function

  • Return Type

    (Type1 | ErrorType)
  • result of combining the members of stm using the combining function