Skip to content

Commit 2f1cd11

Browse files
gustywallymathieu
authored andcommitted
+ SeqT.head and SeqT.tryHead (#609)
1 parent 4079153 commit 2f1cd11

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/FSharpPlus/Data/Seq.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,14 @@ module SeqT_V2 =
797797
let inline iteri<'T, .. > (f: int -> 'T -> unit) (source: SeqT<'``Monad<bool>``, 'T>) : '``Monad<unit>`` = iteriM (fun i x -> result (f i x)) source
798798
let inline iter<'T, .. > f (source: SeqT<'``Monad<bool>``, 'T>) : '``Monad<unit>`` = iterM (f >> result) source
799799

800+
let inline tryHead (source: SeqT<'``Monad<bool>``, 'T>) : '``Monad<'T option>`` =
801+
use ie = (source :> IEnumerableM<'``Monad<bool>``, 'T>).GetEnumerator ()
802+
Bind.Invoke (ie.MoveNext ()) (fun b -> Return.Invoke (if b then Some ie.Current else None))
803+
804+
let inline head (source: SeqT<'``Monad<bool>``, 'T>) : '``Monad<'T>`` =
805+
use ie = (source :> IEnumerableM<'``Monad<bool>``, 'T>).GetEnumerator ()
806+
Bind.Invoke (ie.MoveNext ()) (fun b -> Return.Invoke (if b then ie.Current else invalidArg (nameof source) "The input sequence was empty."))
807+
800808
let inline internal tryPickMAndMap<'T, 'U, .. > (f: 'T -> '``Monad<'U option>``) (source: SeqT<'``Monad<bool>``, 'T>) (postMap: 'U option -> 'V) : '``Monad<'V>`` = innerMonad2<_, '``Monad<unit>``> () {
801809
use ie = (source :> IEnumerableM<'``Monad<bool>``, 'T>).GetEnumerator ()
802810
let! (move: bool) = ie.MoveNext ()

0 commit comments

Comments
 (0)