https://kotlinlang.org logo
#stdlib
Title
# stdlib
a

Alpesh Vas

11/07/2019, 7:06 PM
What do you guys think?
x

Xavier F. Gouchet

11/07/2019, 7:39 PM
Can you share a sample of what your extension function does?
k

karelpeeters

11/07/2019, 8:46 PM
Or maybe even the function itself?
a

Alpesh Vas

11/08/2019, 8:00 AM
Sure
Consider this code snippet
Copy code
data class Customer(
        val id: Int,
        val name: String,
        val phoneNumber: String
)

val customerIds = listOf(1, 2, 3, 4, 5)

val customers = listOf(
        Customer(id = 0, name = "Alpesh", phoneNumber = "019"),
        Customer(id = 2, name = "Vas", phoneNumber = "245"),
        Customer(id = 3, name = "Alp3", phoneNumber = "678"),
        Customer(id = 6, name = "Alp4", phoneNumber = "901"))
Let say, now I want to find intersection of
customers
array and
customerIds
and return list of
Customer
after intersection.
Now, kotlin has this intersect function
Copy code
/**
 * Returns a set containing all elements that are contained by both this set and the specified collection.
 * 
 * The returned set preserves the element iteration order of the original collection.
 */
public infix fun <T> Iterable<T>.intersect(other: Iterable<T>): Set<T> {
    val set = this.toMutableSet()
    set.retainAll(other)
    return set
}
We can't use this function as types of both iterables are different in our case. We need some transform function here.
So I've created this *customized extension function *
Copy code
fun <T, A> Iterable<A>.intersect(other: Iterable<T>, valueTransform: (A) -> T ): List<A> {
    val set = this.map(valueTransform).toMutableSet()
    set.retainAll(other)
    return this.filter {valueTransform(it) in set}
}
And I can achieve what I wanted to achieve by
val customerAfterInterSection = customers.intersect(customerIds){it.id}
So I think we can add this ^^ function in kotlin's stdlib for collections.
Let me know if you need more examples.
x

Xavier F. Gouchet

11/08/2019, 8:24 AM
Can't you just do
Copy code
val customerAfterIntersect = customers.filter { it.id in customerIds }
a

Alpesh Vas

11/08/2019, 8:44 AM
We can but,
it.id in customerIds
is not efficient operation here right?
Because customerIds is list, you'll have to convert it to set separately.
x

Xavier F. Gouchet

11/08/2019, 8:45 AM
I'm not sure how big the difference in performance is between list and set
But you can still just create your set by hand and have a two kinds solution
a

Alpesh Vas

11/08/2019, 8:47 AM
True, but that extension function is doing it for you. You won't need to create auxiliary set every time.
By the way look up in list is
O(n)
whilst set does it in
O(1)
@Xavier F. Gouchet, @karelpeeters any suggestions on this? Is this worthy of to be a part of stdlib functions?
n

nkiesel

11/08/2019, 2:23 PM
what about
val customerAfterIntersect = customers.filter { it.id in customerIds.toSet() }
g

gildor

11/08/2019, 2:47 PM
I personally don't think that it make sense to include it to stdlib, pretty specific use case for me, like what if I want to have different mapping for each collection
But be free to create an issue on Kotlin issue tracker of you think it's common case that may be helpful
a

Alpesh Vas

11/08/2019, 5:56 PM
https://kotlinlang.slack.com/archives/C0B8Q383C/p1573200411032900?thread_ts=1573153561.031000&amp;cid=C0B8Q383C This is just an example, you can always create a new function with two different transform functions as arguments.
g

gildor

11/09/2019, 12:31 AM
Yes, that is my point. If you suggest something for stdlib it should be some complete solution, that covers some range of cases. If function has too narrow use case it just shouldn't be a part stdlib
d

Derek Peirce

11/09/2019, 5:19 AM
I second that this function is too specific for stdlib. Even this implementation is calling
valueTransform
on each element twice, which is fine if it is cheap, but problematic if it is expensive and has side-effects.
f

Fudge

11/11/2019, 12:26 AM
what about
val customerAfterIntersect = customers.filter { it.id in customerIds.toSet() }
toSet needs to go outside the loop or it will spend o(n) time making the set every loop
a

Alpesh Vas

11/11/2019, 5:16 AM
Yup ^
4 Views