What do you guys think?
# stdlib
a
What do you guys think?
x
Can you share a sample of what your extension function does?
k
Or maybe even the function itself?
a
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
Can't you just do
Copy code
val customerAfterIntersect = customers.filter { it.id in customerIds }
a
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
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
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
what about
val customerAfterIntersect = customers.filter { it.id in customerIds.toSet() }
g
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
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
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
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
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
Yup ^