Sorry for the basic question. Given I have a list ...
# arrow
d
Sorry for the basic question. Given I have a list of identifiers
val ids = listOf(1, 2, 3)
and I have a function that returns a valid thing for a given identifier
fun getThing(id: String) : Either<String, Thing?>
what is the best way to execute the getThing function for all ids in the list until a Thing is found, or an either.left is encountered? Traverse is close to what I need, but it will involve unnecessary database calls; I will like to exit processing as soon as a match is found.
k
You should either do the filtering in the SQL query, or load the result set first then filter (to avoid unnecessary roundtrips to the DB).
f
ids.asSequence().map { getThing(it) }.filter {...}.first()
d
Thanks for the idea of using asSequence(). Seems like I was looking for an Arrow solution unnecessarily 🙂