Thread
#stdlib
    Alpesh Vas

    Alpesh Vas

    2 years ago
    What do you guys think?
    Xavier F. Gouchet

    Xavier F. Gouchet

    2 years ago
    Can you share a sample of what your extension function does?
    karelpeeters

    karelpeeters

    2 years ago
    Or maybe even the function itself?
    Alpesh Vas

    Alpesh Vas

    2 years ago
    Sure
    Consider this code snippet
    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
    /**
     * 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 *
    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.
    Xavier F. Gouchet

    Xavier F. Gouchet

    2 years ago
    Can't you just do
    val customerAfterIntersect = customers.filter { it.id in customerIds }
    Alpesh Vas

    Alpesh Vas

    2 years ago
    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.
    Xavier F. Gouchet

    Xavier F. Gouchet

    2 years ago
    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
    Alpesh Vas

    Alpesh Vas

    2 years ago
    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

    2 years ago
    what about
    val customerAfterIntersect = customers.filter { it.id in customerIds.toSet() }
    gildor

    gildor

    2 years ago
    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
    Alpesh Vas

    Alpesh Vas

    2 years ago
    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.
    gildor

    gildor

    2 years ago
    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
    Derek Peirce

    Derek Peirce

    2 years ago
    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.
    Fudge

    Fudge

    2 years ago
    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
    Alpesh Vas

    Alpesh Vas

    2 years ago
    Yup ^