To quote Scala's documentation:
def span(p: (A) => Boolean): (Seq[A], Seq[A])
Splits this iterable collection into a prefix/suffix pair according to a predicate.
Note: c span p is equivalent to (but possibly more efficient than) (c takeWhile p, c dropWhile p), provided the evaluation of the predicate p does not cause any side-effects.
Note: might return different results for different runs, unless the underlying collection type is ordered.
- p - the test predicate
- returns - a pair consisting of the longest prefix of this iterable collection whose elements all satisfy p, and the rest of this iterable collection.
- Definition Classes - IterableOps → IterableOnceOps
- Note - Reuse: After calling this method, one should discard the iterator it was called on, and use only the iterators that were returned. Using the old iterator is undefined, subject to change, and may result in changes to the new iterators as well.
When looking at the F# documentation for Seq I don't see any equivalent.
groupBy, partition, splitAt, none of them match what I'm trying to do. It's like doing a takeWhile and skipWhile at the same time but instead of needing two iterations you only need one where the function would return a tuple of (takeWhile, skipWhile).
The output should match the following function
module List
let span (predicate: 'a -> bool) (list: 'a list): ('a list * 'a list) =
(list |> List.takeWhile predicate, list |> List.skipWhile predicate)
But only need one iteration since my sequence may be infinite.
[1;2;3;4] |> List.span (fun i -> i % 2 = 1) => ([1], [2;3;4])