Fred Friis
06/16/2020, 7:07 AM//given a list of entries, we need to return Either<Error, Boolean> where the boolean is true only if
//there were no errors calling the downstream for all entries AND only if the response from the downstream
//was true for all entries
kotlin
return entries
.map { callDownstreamForEntry(it) } //now we have a List<Either<IError, Boolean>
.sequence(Either.applicative()) //turn it into Either<FirstError, List<Boolean>>
.fix() //please the compiler, won't be necessary in newer versions of Arrow
.map { it.fix() } //please the compiler, won't be necessary in newer versions of Arrow
//now map on the Either<FirstError, List<Boolean>>
.map { listOfBooleans ->
listOfBooleans
.fold(
true, //start with true
{ sofar, currentItem ->
sofar && currentItem
//this will turn the list of booleans into a single boolean true if all
//booleans in the list are true, or a single boolean false if there is a
//single boolean false in the list
})
}
//the above code does that
//BUT
//if we get an error OR a right<false> from the downstream, rather than continuing to map through all
//entries (the list can be very large) we would like to fail fast
//this is easy to do with a for loop but if possible it would be nice to do it functionally