Single value sequence functions

I’m experimenting with sequence/iterable extension methods (find, etc.) on concrete types with a single value. So far, I only have find, but have found it to be useful:

inline fun <T> T.find(predicate: (T) -> Boolean): T? =
    if (predicate(this)) this else null

for example:

nodeName.find { it.prefix?.data == "xmlns" }?.localName

It would be useful if this and the other relevant sequence/iterable extension methods were built into the standard library.

I’m not doing sequenceOf(nodeName) as that seems wasteful to create a temporary object just to apply the find function, and adds noise to the code.

1 Like

This is takeIf


Great, thanks!