use itertools::Itertools;
use vortex_error::VortexResult;
use crate::array::ChunkedArray;
use crate::iter::ArrayIterator;
use crate::stream::{ArrayStream, ArrayStreamAdapter};
use crate::{ArrayData, IntoArrayData};
pub trait ArrayIteratorExt: ArrayIterator {
fn into_stream(self) -> impl ArrayStream
where
Self: Sized,
{
ArrayStreamAdapter::new(self.dtype().clone(), futures_util::stream::iter(self))
}
fn into_array_data(self) -> VortexResult<ArrayData>
where
Self: Sized,
{
let dtype = self.dtype().clone();
let mut chunks: Vec<ArrayData> = self.try_collect()?;
if chunks.len() == 1 {
Ok(chunks.remove(0))
} else {
Ok(ChunkedArray::try_new(chunks, dtype)?.into_array())
}
}
}
impl<I: ArrayIterator> ArrayIteratorExt for I {}