Skip to content

Instantly share code, notes, and snippets.

@milovtim
Created September 21, 2022 12:40
Show Gist options
  • Save milovtim/e18852833cfe5e10934a324fe006ee30 to your computer and use it in GitHub Desktop.
Save milovtim/e18852833cfe5e10934a324fe006ee30 to your computer and use it in GitHub Desktop.
How to represent collection boolean aggregating functions one into another
fun List<Int>.allNonZero() = all { it != 0 }
fun List<Int>.allNonZero1() = none { it == 0 }
fun List<Int>.allNonZero2() = !any { it == 0 }
fun List<Int>.containsZero() = any { it == 0 }
fun List<Int>.containsZero1() = !all { it != 0 }
fun List<Int>.containsZero2() = !none { it == 0 }
fun main(args: Array<String>) {
val list1 = listOf(1, 2, 3)
list1.allNonZero() eq true
list1.allNonZero1() eq true
list1.allNonZero2() eq true
list1.containsZero() eq false
list1.containsZero1() eq false
list1.containsZero2() eq false
val list2 = listOf(0, 1, 2)
list2.allNonZero() eq false
list2.allNonZero1() eq false
list2.allNonZero2() eq false
list2.containsZero() eq true
list2.containsZero1() eq true
list2.containsZero2() eq true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment