diff --git a/src/FSharpPlus/Data/Seq.fs b/src/FSharpPlus/Data/Seq.fs index 3c950a5df..5a7ea5817 100644 --- a/src/FSharpPlus/Data/Seq.fs +++ b/src/FSharpPlus/Data/Seq.fs @@ -764,6 +764,14 @@ module SeqT_V2 = let inline iteri<'T, .. > (f: int -> 'T -> unit) (source: SeqT<'``Monad``, 'T>) : '``Monad`` = iteriM (fun i x -> result (f i x)) source let inline iter<'T, .. > f (source: SeqT<'``Monad``, 'T>) : '``Monad`` = iterM (f >> result) source + let inline tryHead (source: SeqT<'``Monad``, 'T>) : '``Monad<'T option>`` = + use ie = (source :> IEnumerableM<'``Monad``, 'T>).GetEnumerator () + Bind.Invoke (ie.MoveNext ()) (fun b -> Return.Invoke (if b then Some ie.Current else None)) + + let inline head (source: SeqT<'``Monad``, 'T>) : '``Monad<'T>`` = + use ie = (source :> IEnumerableM<'``Monad``, 'T>).GetEnumerator () + Bind.Invoke (ie.MoveNext ()) (fun b -> Return.Invoke (if b then ie.Current else invalidArg (nameof source) "The input sequence was empty.")) + let inline internal tryPickMAndMap<'T, 'U, .. > (f: 'T -> '``Monad<'U option>``) (source: SeqT<'``Monad``, 'T>) (postMap: 'U option -> 'V) : '``Monad<'V>`` = innerMonad2<_, '``Monad``> () { use ie = (source :> IEnumerableM<'``Monad``, 'T>).GetEnumerator () let! move = ie.MoveNext ()