Skip to content

Provide toSequenceOf<SomeDataClass> similar to toListOf #1045

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

Closed
martinitus opened this issue Jan 31, 2025 · 1 comment · Fixed by #1046
Closed

Provide toSequenceOf<SomeDataClass> similar to toListOf #1045

martinitus opened this issue Jan 31, 2025 · 1 comment · Fixed by #1046
Labels
enhancement New feature or request

Comments

@martinitus
Copy link

In our application, we use the dataframe library to parse CSV and preprocess it before we transform the rows into our internal domain objects. This works really well and feels quite natural (coming from spark, pandas, polars, ...).

However, for large datasets, working with eagerly created collections sometimes seems wasteful, as we sometimes immediately chain other mappings or filters - i.e. we discard the List that gets returned from DataFrame.toListOf.

AFAIK it would be way more memory efficient to use a asSequenceOf in those scenarios.

Looking at the implementation of toListOf():

// org/jetbrains/kotlinx/dataframe/api/toList.kt
public inline fun <reified T> AnyFrame.toListOf(): List<T> = toListImpl(typeOf<T>()) as List<T>

and

// org/jetbrains/kotlinx/dataframe/impl/api/toList.kt
 internal fun AnyFrame.toListImpl(type: KType): List<Any> {
    ....
       return rows().map { row ->
        val parameters = convertedColumns
            .map { row[it] }
            .toTypedArray()
        constructor.call(*parameters)
    }
}

and

// org/jetbrains/kotlinx/dataframe/api/DataFrameGet.kt
public fun <T> DataFrame<T>.rows(): Iterable<DataRow<T>> = ...

It looks like a asSequenceOf() method could trivially be added?

@martinitus
Copy link
Author

I would be willing to contribute this - given my above assessment is correct and such a contribution is welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants