Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stream to array conversion function to standard lib #1316

Open
Shadow-Devil opened this issue Jul 31, 2024 · 2 comments
Open

Add stream to array conversion function to standard lib #1316

Shadow-Devil opened this issue Jul 31, 2024 · 2 comments
Labels
Area/StdLib Relates to Ballerina Standard Library Type/NewFeature

Comments

@Shadow-Devil
Copy link

Description:
This Stackoverflow question says, that there are two ways to convert a stream to an array, but in my opinion both of them seem quite verbose.

It would be great to have a stream:toArray() function, which is the opposite of array:toStream() and would convert a stream to an array.
In the documentation of the function it should be noted, that for infinite streams this will loop forever.

What do you think about this?

Suggested Labels:

Code sample that shows issue:

Related Issues:

@jclark
Copy link
Collaborator

jclark commented Jul 31, 2024

I think that's a good addition.

One question is can we write the type for this.

public isolated function toArray(stream<T,E|()>) returns E|T[] = external;

Would that work @MaryamZi ?

@MaryamZi
Copy link
Member

MaryamZi commented Aug 1, 2024

I also agree that this would be a good addition. Especially with database clients, we often see functions/query expressions introduced to do just this.

One question is can we write the type for this.

public isolated function toArray(stream<T,E|()>) returns E|T[] = external;

Would that work @MaryamZi ?

I believe this should work, similar to stream:reduce, which is defined as

@typeParam
type Type any|error;

@typeParam
type Type1 any|error;

@typeParam
type ErrorType error;

public isolated function reduce(stream<Type,ErrorType?> stm, 
         @isolatedParam function(Type1 accum, Type val) returns Type1 func, Type1 initial)
   returns Type1|ErrorType = external;

(We do seem to have a bug with stream:reduce type checking in the jBallerina implementation when the completion type is a subtype of error - ballerina-platform/ballerina-lang#43209)

@anupama-pathirage anupama-pathirage added Area/StdLib Relates to Ballerina Standard Library Type/NewFeature labels Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/StdLib Relates to Ballerina Standard Library Type/NewFeature
Projects
None yet
Development

No branches or pull requests

4 participants