lang.stream
ballerina/lang.stream Ballerina library
Module overview
The lang.stream
module corresponds to the stream
basic type.
Functions
'map
function 'map(stream<any|error, error|()> stm, function(any|error) returns (any|error)
func) returns stream<any|error, error|()>
Applies a function to each member of a stream and returns a stream of the results.
stream<float> ms = [14.5f, 45.5f, 6.8f, 4f].toStream(); stream<float> cms = ms.map(m => m * 100.0); cms.next() ⇒ {"value":1450.0}
Parameters
close
Closes a stream.
This releases any system resources being used by the stream.
Closing a stream that has already been closed has no effect and returns ()
.
stream<int, error?> strm = new; check strm.close();
Return Type
- error|()? - () if the close completed successfully, otherwise an error
filter
function filter(stream<any|error, error|()> stm, function(any|error) returns (boolean)
func) returns stream<any|error, error|()>
Selects the members from a stream for which a function returns true.
stream<int> scores = [45, 60, 75, 30, 90].toStream(); scores.filter(score => score > 50).next() ⇒ {"value":60}
Parameters
forEach
function forEach(stream<any|error, error|()> stm, function(any|error) returns (() )
func) returns error|()
Applies a function to each member of a stream.
The parameter func
is applied to each member of parameter stm
stream in order.
stream<int> scores = [45, 60, 75, 30, 90].toStream(); int total = 0; scores.forEach(function(int score) { total += score; }); total ⇒ 300
Parameters
- func
function(any|error) returns (() )
- a function to apply to each member
Return Type
- error|() - () if the close completed successfully, otherwise an error
iterator
function iterator(stream<any|error, error|()> stm) returns object {
public isolated function next() returns record {|
Type value;
|}|CompletionType;
}
Returns an iterator over a stream.
stream<int> scores = [45, 60, 75, 30, 90].toStream(); object { public isolated function next() returns record {|int value;|}?; } iterator = scores.iterator(); iterator.next() ⇒ {"value":45}
Return Type
- object {
public isolated function next() returns record {|
Type value;
|}|CompletionType;
} - a new iterator object that will iterate over the members of parameter
stm
.
next
Returns the next element in the stream wrapped in a record or () if the stream ends.
stream<int> scores = [45, 60, 75, 30, 90].toStream(); scores.next() ⇒ {"value":45}
reduce
function reduce(stream<any|error, ErrorType?> stm, function(any|error, any|error) returns (any|error)
func, any|error initial) returns any|error|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.
stream<int> scores = [45, 60, 75, 30, 90].toStream(); scores.reduce(isolated function (int total, int score) returns int => total + score, 0) ⇒ 300
Parameters
- initial any|error - initial value for the first argument of combining parameter
func
Return Type
- any|error|ErrorType - result of combining the members of parameter
stm
using parameterfunc
Errors
lang.stream: ErrorType
A type parameter that is a subtype of error
.
Has the special semantic that when used in a declaration
all uses in the declaration must refer to same type.
Union types
lang.stream: Type
Type
A type parameter that is a subtype of any|error
.
Has the special semantic that when used in a declaration
all uses in the declaration must refer to same type.
lang.stream: Type1
Type1
A type parameter that is a subtype of any|error
.
Has the special semantic that when used in a declaration
all uses in the declaration must refer to same type.