Ben Edwards
08/23/2022, 3:54 PMJoffrey
08/23/2022, 3:59 PMin inside if conditions because I like how it reads. Sometimes contains might feel more natural if it's at the end of a chain of calls. In other cases, I don't mind, I guess it's opinion based anywayJoffrey
08/23/2022, 3:59 PMephemient
08/23/2022, 3:59 PMitem in array is idiomaticephemient
08/23/2022, 4:00 PMArray in general is notBen Edwards
08/23/2022, 4:13 PMBen Edwards
08/23/2022, 4:13 PMBen Edwards
08/23/2022, 4:36 PMfun dropLoot(name: String): Boolean {
println("$name will be dropped")
return inventory.removeIf { it.name = name }
}
cant work out how to do this for mutanleListOfephemient
08/23/2022, 4:39 PMinventory.removeAll { it.name == name }
removeIf comes from JavaAugust Lilleaas
08/23/2022, 4:41 PMremoveIf seems to work on mutableListOf 🤔August Lilleaas
08/23/2022, 4:44 PMdropLoot since it mutates the list, and returns a booleanBen Edwards
08/23/2022, 4:46 PMAugust Lilleaas
08/23/2022, 4:48 PM== there and not =ephemient
08/23/2022, 4:49 PMremoveIf is one of the new default methods in Java 8: https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#removeIf-java.util.function.Predicate-
and as such it is callable on a MutableList as long as you're on JVMephemient
08/23/2022, 4:51 PM== vs = would break regardless of `ArrayList`/`MutableList`…Ben Edwards
08/23/2022, 4:51 PMAugust Lilleaas
08/23/2022, 4:53 PMdata class Thing(
val name: String
)
val inventory = mutableListOf<Thing>(Thing("foo"), Thing("bar"), Thing("baz"))
println(inventory) // [Thing(name=foo), Thing(name=bar), Thing(name=baz)]
inventory.removeIf { it.name == "foo" }
println(inventory) // [Thing(name=bar), Thing(name=baz)]Ben Edwards
08/23/2022, 5:00 PMBen Edwards
08/23/2022, 5:01 PMfun dropLoot(name: String): Boolean {
println("$name will be dropped")
return inventory.retainAll { it.name != name }
}ephemient
08/23/2022, 5:08 PMephemient
08/23/2022, 5:09 PMremoveAll or retainAll.Ben Edwards
08/23/2022, 5:13 PMJoffrey
08/23/2022, 7:48 PMbuild.gradle(.kts) files - or nowhere at all, but I think the default is now 1.8 so if you have no such setting you should also have no such problem 😄