From b953a0e0c5b6990b0e81f2135794a4e6ac81a2b6 Mon Sep 17 00:00:00 2001 From: gusty <1261319+gusty@users.noreply.github.com> Date: Tue, 8 Oct 2024 06:46:16 +0200 Subject: [PATCH] + SeqT.head and SeqT.tryHead --- src/FSharpPlus/Data/Seq.fs | 8 ++++++++ 1 file changed, 8 insertions(+) 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 ()