If I have an infinite sequence, I cannot use a normal Seq.tryFind.
However, if the sequence is ordered, that means the search could be cancelled when I detect that no other element in the sequence can ever satisfy my condition.
Is there an elegant way to express such a search?
Here is another possible solution. It is quite similar to what @desco posted, but it is using sequence expressions instead of
Seq.tryPickand it also does not need nested option types:Or even nicer and simpler solution using built-in functions. Just use
Seq.takeWhileto take the beginning of the sequence containing only elements that are smaller or equal than the one you’re looking for and then useSeq.tryFindon this part of the sequence:Or if you fancy the point-free style (which I do not, because it gets harder to read IMHO):