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 anywayephemient
08/23/2022, 3:59 PMitem in array
is idiomaticArray
in general is notBen Edwards
08/23/2022, 4:13 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
🤔dropLoot
since it mutates the list, and returns a boolean==
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 JVM==
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:01 PMfun dropLoot(name: String): Boolean {
println("$name will be dropped")
return inventory.retainAll { it.name != name }
}
ephemient
08/23/2022, 5:08 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 😄