Skip to content

Commit

Permalink
Rename deepFind to recursiveFind
Browse files Browse the repository at this point in the history
  • Loading branch information
Sternbach-Software committed Aug 23, 2021
1 parent 4538ba1 commit f0933f2
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/main/kotlin/KotlinFunctionLibrary.kt
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ println("workingList2=$workingList2")*/
}

/**
* Recurses through a list of a recursive elements to find the element which matches [predicate] by selecting the next list using [recursiveSelector]
* A version of List.find() for recursive data structures. Recurses through a list of a recursive elements to find the element which matches [predicate] by selecting the next list using [recursiveSelector]
* For example, given class Foo(val a: Char, val b: List<Foo>)
* val list = listOf(
Foo('a',
Expand Down Expand Up @@ -971,18 +971,18 @@ println("workingList2=$workingList2")*/
list.deepFind({ it.a == 'a' }) { it.b } != null == true
list.deepFind({ it.a == 'f' }) { it.b } != null == true
* */
fun <E> List<E>.deepFind(predicate: (E) -> Boolean, recursiveSelector: (E) -> List<E>): E? {
fun <E> List<E>.recursiveFind(predicate: (E) -> Boolean, recursiveSelector: (E) -> List<E>): E? {
var find = find(predicate)//check this layer
if(find != null) return find
for(element in this){
find = recursiveSelector(element).deepFind(predicate, recursiveSelector)//check the next layer
find = recursiveSelector(element).recursiveFind(predicate, recursiveSelector)//check the next layer
if(find != null) return find
}
return null
}

@JvmStatic
fun main(args: Array<String>) {
println("KotlinFunctionLibrary v2.1.0")
println("KotlinFunctionLibrary v3.0.0")
}
}

0 comments on commit f0933f2

Please sign in to comment.