Skip to content

Commit

Permalink
+ NonEmptySeq.unsafeOfSeq (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
cannorin authored and wallymathieu committed Nov 22, 2020
1 parent 42f5d7c commit 354f637
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/FSharpPlus/Data/NonEmptySeq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,24 @@ type NonEmptySeq<'T> =
type neseq<'t> = NonEmptySeq<'t>

module NonEmptySeq =
let internal unsafeOfSeq (x: _ seq) =
/// <summary>Builds a non empty sequence from the given sequence.</summary>
/// <param name="seq">The input sequence.</param>
/// <returns>Non empty sequence containing the elements of the list.</returns>
/// <remarks>
/// **This function does not check whether the sequence is actually non empty or not.**
///
/// Use this function only if you are sure that the sequence is not empty and
/// you don't want to evaluate the first element of the sequence which would cause a
/// side effect.
///
/// Otherwise, always use `ofSeq`.
/// </remarks>
/// <seealso cref="ofSeq" />
let unsafeOfSeq (seq: _ seq) =
{ new NonEmptySeq<_> with
member _.First = Seq.head x
member _.GetEnumerator() = x.GetEnumerator()
member _.GetEnumerator() = x.GetEnumerator() :> Collections.IEnumerator }
member _.First = Seq.head seq
member _.GetEnumerator() = seq.GetEnumerator()
member _.GetEnumerator() = seq.GetEnumerator() :> Collections.IEnumerator }

let internal unsafeOfArray (x: _[]) =
{ new NonEmptySeq<_> with
Expand Down Expand Up @@ -243,7 +256,13 @@ module NonEmptySeq =
/// <param name="seq">The input sequence.</param>
/// <returns>Non empty sequence containing the elements of the list.</returns>
/// <exception cref="System.ArgumentException">Thrown when the input sequence is empty.</exception>
/// <remarks>Throws exception for empty sequence</remarks>
/// <remarks>
/// Throws exception for empty sequence.
///
/// Evaluates the first element of the sequence and may trigger side effects.
/// If you are sure that the sequence is not empty and want to avoid that, you can use `unsafeOfSeq` instead.
/// </remarks>
/// <seealso cref="unsafeOfSeq" />
let ofSeq (seq: _ seq) =
if isNull seq || Seq.isEmpty seq then invalidArg "seq" "The input sequence was empty."
else unsafeOfSeq seq
Expand Down

0 comments on commit 354f637

Please sign in to comment.