When using general stream function, such as Stream.timer/1
, it returns:
#Stream<[
enum: #Function<61.58486609/2 in Stream.unfold/2>,
funs: [#Function<55.58486609/1 in Stream.take_after_guards/2>]
]>
which is very easy to detect the type with is_struct(stream, Stream)
.
But, when using Stream.resource/3
, the return value is something like #Function<51.58486609/2 in Stream.resource/3>
. For example:
stream = Stream.resource(
fn -> File.open!("sample") end,
fn file ->
case IO.read(file, :line) do
data when is_binary(data) -> {[data], file}
_ -> {:halt, file}
end
end,
fn file -> File.close(file) end
)
# Function<51.58486609/2 in Stream.resource/3>
is_struct(stream, Stream) # false
Is there any way to detect the return value of Stream.resource/3
as a stream?