Skip to content
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

AnyCallback is a rewrite of an existing Kotlin stdlib function #143

Open
apatrida opened this issue Jul 30, 2023 · 0 comments
Open

AnyCallback is a rewrite of an existing Kotlin stdlib function #143

apatrida opened this issue Jul 30, 2023 · 0 comments

Comments

@apatrida
Copy link

apatrida commented Jul 30, 2023

This question needs redesigned as it enters the realm of "silly" when you can solve this in 1 line of code using first(predicate), none, any, find(predicate), etc. You'd have to list all functions they could not call to make them really want to recurse. If you want a recursive question, then force it with a custom data structure (NodeLinkedList) that doesn't have 10 existing methods that already do the same answer.

If you don't use a custom data structure, the silly factor is that people know they can just call:

return list.any { callback(it) }

Also, the existing Solution1 throws an exception on empty list. Probably not intended but also is not documented?

private object Solution1 {
    private fun anyCallback(list: List<Int>, callback: (Int) -> Boolean): Boolean {
        if (list.size == 1) {
            return callback(list.first())
        }

        // THROWS EXCEPTION ON EMPTY LIST!!!!
        return callback(list.first()) || anyCallback(list.drop(1), callback)
    }
}

the solution could be a little more efficient (don't copy the list, copy a view of the list instead):

internal object Solution2 {
    fun anyCallback(list: List<Int>, callback: (Int) -> Boolean): Boolean {
        if (list.isEmpty()) return false
        return callback(list.first()) || anyCallback(list.subList(1, list.size), callback)
    }
}

Of course that assumes it isn't a LinkedList, but there is always:

internal object Solution3 {
    fun anyCallback(list: List<Int>, callback: (Int) -> Boolean): Boolean {
        fun _randomAccessOptimized(list: List<Int>, callback: (Int) -> Boolean): Boolean {
            if (list.isEmpty()) return false
            return callback(list.first()) || _randomAccessOptimized(list.subList(1, list.size), callback)
        }

        fun _sequentialOptimized(list: List<Int>, callback: (Int) -> Boolean): Boolean {
            if (list.isEmpty()) return false
            return callback(list.first()) || _sequentialOptimized(list.drop(1), callback)
        }


        return if (list is RandomAccess) {
           _randomAccessOptimized(list, callback)
        }
        else {
            _sequentialOptimized(list, callback)
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant