Skip to content

Commit

Permalink
Rename Sequence overloads to Sequential (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
gusty authored Feb 12, 2024
1 parent d6de56b commit 726b20b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 42 deletions.
24 changes: 12 additions & 12 deletions src/FSharpPlus/Control/Traversable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -188,26 +188,26 @@ type Sequence with
static member inline Sequence (t: seq<'``Applicative<'T>``>, [<Optional>]_output: '``Applicative<seq<'T>>`` , [<Optional>]_impl: Default4) : '``Applicative<seq<'T>>`` =
Sequence.ForInfiniteSequences (t, IsLeftZero.Invoke, List.toSeq)

static member Sequence (t: seq<option<'t>> , [<Optional>]_output: option<seq<'t>> , [<Optional>]_impl: Default3) : option<seq<'t>> = Option.Sequence t
static member Sequence (t: seq<option<'t>> , [<Optional>]_output: option<seq<'t>> , [<Optional>]_impl: Default3) : option<seq<'t>> = Option.Sequential t
#if !FABLE_COMPILER
static member Sequence (t: seq<voption<'t>> , [<Optional>]_output: voption<seq<'t>> , [<Optional>]_impl: Default3) : voption<seq<'t>> = ValueOption.Sequence t
static member Sequence (t: seq<voption<'t>> , [<Optional>]_output: voption<seq<'t>> , [<Optional>]_impl: Default3) : voption<seq<'t>> = ValueOption.Sequential t
#endif
static member Sequence (t: seq<Result<'t,'e>>, [<Optional>]_output: Result<seq<'t>, 'e>, [<Optional>]_impl: Default3) : Result<seq<'t>, 'e> = Result.Sequence t
static member Sequence (t: seq<Choice<'t,'e>>, [<Optional>]_output: Choice<seq<'t>, 'e>, [<Optional>]_impl: Default3) : Choice<seq<'t>, 'e> = Choice.Sequence t
static member Sequence (t: seq<Result<'t,'e>>, [<Optional>]_output: Result<seq<'t>, 'e>, [<Optional>]_impl: Default3) : Result<seq<'t>, 'e> = Result.Sequential t
static member Sequence (t: seq<Choice<'t,'e>>, [<Optional>]_output: Choice<seq<'t>, 'e>, [<Optional>]_impl: Default3) : Choice<seq<'t>, 'e> = Choice.Sequential t
static member Sequence (t: seq<list<'t>> , [<Optional>]_output: list<seq<'t>> , [<Optional>]_impl: Default3) : list<seq<'t>> = Sequence.ForInfiniteSequences (t, List.isEmpty, List.toSeq)
static member Sequence (t: seq<'t []> , [<Optional>]_output: seq<'t> [] , [<Optional>]_impl: Default3) : seq<'t> [] = Sequence.ForInfiniteSequences (t, Array.isEmpty, List.toSeq)

#if !FABLE_COMPILER
static member Sequence (t: seq<Async<'t>> , [<Optional>]_output: Async<seq<'t>> , [<Optional>]_impl: Default3) : Async<seq<'t>> = Async.Sequence t
static member Sequence (t: seq<Async<'t>> , [<Optional>]_output: Async<seq<'t>> , [<Optional>]_impl: Default3) : Async<seq<'t>> = Async.SequentialLazy t
#endif
static member inline Sequence (t: NonEmptySeq<'``Applicative<'T>``>, [<Optional>]_output: '``Applicative<NonEmptySeq<'T>>`` , [<Optional>]_impl: Default4) : '``Applicative<NonEmptySeq<'T>>`` = Sequence.ForInfiniteSequences (t, IsLeftZero.Invoke, NonEmptySeq.ofList)
static member Sequence (t: NonEmptySeq<option<'t>> , [<Optional>]_output: option<NonEmptySeq<'t>> , [<Optional>]_impl: Default3) : option<NonEmptySeq<'t>> = Option.Sequence t |> Option.map NonEmptySeq.unsafeOfSeq
static member Sequence (t: NonEmptySeq<Result<'t,'e>>, [<Optional>]_output: Result<NonEmptySeq<'t>, 'e>, [<Optional>]_impl: Default3) : Result<NonEmptySeq<'t>, 'e> = Result.Sequence t |> Result.map NonEmptySeq.unsafeOfSeq
static member Sequence (t: NonEmptySeq<Choice<'t,'e>>, [<Optional>]_output: Choice<NonEmptySeq<'t>, 'e>, [<Optional>]_impl: Default3) : Choice<NonEmptySeq<'t>, 'e> = Choice.Sequence t |> Choice.map NonEmptySeq.unsafeOfSeq
static member Sequence (t: NonEmptySeq<option<'t>> , [<Optional>]_output: option<NonEmptySeq<'t>> , [<Optional>]_impl: Default3) : option<NonEmptySeq<'t>> = Option.Sequential t |> Option.map NonEmptySeq.unsafeOfSeq
static member Sequence (t: NonEmptySeq<Result<'t,'e>>, [<Optional>]_output: Result<NonEmptySeq<'t>, 'e>, [<Optional>]_impl: Default3) : Result<NonEmptySeq<'t>, 'e> = Result.Sequential t |> Result.map NonEmptySeq.unsafeOfSeq
static member Sequence (t: NonEmptySeq<Choice<'t,'e>>, [<Optional>]_output: Choice<NonEmptySeq<'t>, 'e>, [<Optional>]_impl: Default3) : Choice<NonEmptySeq<'t>, 'e> = Choice.Sequential t |> Choice.map NonEmptySeq.unsafeOfSeq
static member Sequence (t: NonEmptySeq<list<'t>> , [<Optional>]_output: list<NonEmptySeq<'t>> , [<Optional>]_impl: Default3) : list<NonEmptySeq<'t>> = Sequence.ForInfiniteSequences(t, List.isEmpty , NonEmptySeq.ofList)
static member Sequence (t: NonEmptySeq<'t []> , [<Optional>]_output: NonEmptySeq<'t> [] , [<Optional>]_impl: Default3) : NonEmptySeq<'t> [] = Sequence.ForInfiniteSequences(t, Array.isEmpty, NonEmptySeq.ofList)
#if !FABLE_COMPILER
static member Sequence (t: NonEmptySeq<Async<'t>> , [<Optional>]_output: Async<NonEmptySeq<'t>> , [<Optional>]_impl: Default3) = Async.Sequence t |> Async.map NonEmptySeq.unsafeOfSeq : Async<NonEmptySeq<'t>>
static member Sequence (t: NonEmptySeq<Async<'t>> , [<Optional>]_output: Async<NonEmptySeq<'t>> , [<Optional>]_impl: Default3) = Async.SequentialLazy t |> Async.map NonEmptySeq.unsafeOfSeq : Async<NonEmptySeq<'t>>
#endif

static member inline Sequence (t: ^a , [<Optional>]_output: 'R, [<Optional>]_impl: Default2) : 'R = Traverse.InvokeOnInstance id t
Expand Down Expand Up @@ -431,9 +431,9 @@ type Transpose with
static member inline Transpose (t: seq<'``Applicative<'T>``>, [<Optional>]_output: '``Applicative<seq<'T>>`` , [<Optional>]_impl: Default4) : '``Applicative<seq<'T>>`` =
Transpose.ForInfiniteSequences (t, IsZipLeftZero.Invoke, List.toSeq, Pure.Invoke)

static member Transpose (t: seq<option<'t>> , [<Optional>]_output: option<seq<'t>> , [<Optional>]_impl: Default3) : option<seq<'t>> = Option.Sequence t
static member Transpose (t: seq<option<'t>> , [<Optional>]_output: option<seq<'t>> , [<Optional>]_impl: Default3) : option<seq<'t>> = Option.Sequential t
#if !FABLE_COMPILER
static member Transpose (t: seq<voption<'t>> , [<Optional>]_output: voption<seq<'t>> , [<Optional>]_impl: Default3) : voption<seq<'t>> = ValueOption.Sequence t
static member Transpose (t: seq<voption<'t>> , [<Optional>]_output: voption<seq<'t>> , [<Optional>]_impl: Default3) : voption<seq<'t>> = ValueOption.Sequential t
#endif
static member inline Transpose (t: seq<Result<'t,'e>>, [<Optional>]_output: Result<seq<'t>, 'e>, [<Optional>]_impl: Default3) : Result<seq<'t>, 'e> = Result.Parallel ((++), t)
static member inline Transpose (t: seq<Choice<'t,'e>>, [<Optional>]_output: Choice<seq<'t>, 'e>, [<Optional>]_impl: Default3) : Choice<seq<'t>, 'e> = Choice.Parallel ((++), t)
Expand All @@ -443,7 +443,7 @@ type Transpose with
static member Transpose (t: seq<Async<'t>> , [<Optional>]_output: Async<seq<'t>> , [<Optional>]_impl: Default3) : Async<seq<'t>> = Async.Parallel t |> Async.map Array.toSeq
#endif
static member inline Transpose (t: NonEmptySeq<'``Applicative<'T>``>, [<Optional>]_output: '``Applicative<NonEmptySeq<'T>>``, [<Optional>]_impl: Default4) : '``Applicative<NonEmptySeq<'T>>`` = Transpose.ForInfiniteSequences (t, IsZipLeftZero.Invoke, NonEmptySeq.ofList, fun _ -> Unchecked.defaultof<_>)
static member Transpose (t: NonEmptySeq<option<'t>> , [<Optional>]_output: option<NonEmptySeq<'t>> , [<Optional>]_impl: Default3) : option<NonEmptySeq<'t>> = Option.Sequence t |> Option.map NonEmptySeq.unsafeOfSeq
static member Transpose (t: NonEmptySeq<option<'t>> , [<Optional>]_output: option<NonEmptySeq<'t>> , [<Optional>]_impl: Default3) : option<NonEmptySeq<'t>> = Option.Sequential t |> Option.map NonEmptySeq.unsafeOfSeq
static member inline Transpose (t: NonEmptySeq<Result<'t,'e>>, [<Optional>]_output: Result<NonEmptySeq<'t>, 'e>, [<Optional>]_impl: Default3) : Result<NonEmptySeq<'t>, 'e> = Result.Parallel ((++), t) |> Result.map NonEmptySeq.unsafeOfSeq
static member inline Transpose (t: NonEmptySeq<Choice<'t,'e>>, [<Optional>]_output: Choice<NonEmptySeq<'t>, 'e>, [<Optional>]_impl: Default3) : Choice<NonEmptySeq<'t>, 'e> = Choice.Parallel ((++), t) |> Choice.map NonEmptySeq.unsafeOfSeq
static member Transpose (t: NonEmptySeq<list<'t>> , [<Optional>]_output: list<NonEmptySeq<'t>> , [<Optional>]_impl: Default3) : list<NonEmptySeq<'t>> = Transpose.ForInfiniteSequences (t, List.isEmpty , NonEmptySeq.ofList, fun _ -> Unchecked.defaultof<_>)
Expand Down
78 changes: 48 additions & 30 deletions src/FSharpPlus/Extensions/Extensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,16 @@ module Extensions =
|> ignore)



/// Combine all asyncs in one, chaining them in sequence order.
static member Sequence (t: seq<Async<'T>>) : Async<seq<_>> = async {
/// Similar to Async.Sequential but the returned Async contains a sequence, which is lazily evaluated.
static member SequentialLazy (t: seq<Async<'T>>) : Async<seq<_>> = async {
let! ct = Async.CancellationToken
return Seq.map (fun t -> Async.AsTask(t, ct).Result) t }
[<Obsolete("Renamed to Async.Sequential or Async.SequentialLazy")>]static member Sequence (t: seq<Async<_>>) = Async.SequentialLazy t
#endif

/// Combine all asyncs in one, chaining them in sequence order.
static member Sequence (t: list<Async<'T>>) : Async<list<'T>> =
static member Sequential (t: list<Async<'T>>) : Async<list<'T>> =
#if FABLE_COMPILER
let rec loop acc = function
| [] -> async.Return (List.rev acc)
Expand All @@ -195,44 +196,56 @@ module Extensions =
coll.Add v
return coll.Close () }
#endif
[<Obsolete("Renamed to Async.Sequential")>]static member Sequence (t: list<Async<'T>>) = Async<_>.Sequential t


/// Combine all asyncs in one, chaining them in sequence order.
static member Sequence (t: array<Async<_>>) : Async<array<_>> = async {
static member Sequential (t: array<Async<_>>) : Async<array<_>> = async {
let siz = Array.length t
let arr = Array.zeroCreate siz
for i in 0 .. siz-1 do
let! v = t.[i]
arr.[i] <- v
return arr }
[<Obsolete("Renamed to Async.Sequential")>]static member Sequence (t: array<Async<_>>) = Async<_>.Sequential t


/// Creates an async Result from a Result where the Ok case is async.
static member Sequence (t: Result<Async<'T>, 'Error>) : Async<Result<'T,'Error>> =
static member Sequential (t: Result<Async<'T>, 'Error>) : Async<Result<'T,'Error>> =
match t with
| Ok a -> Async.Map Ok a
| Error e -> async.Return (Error e)
[<Obsolete("Renamed to Async.Sequential")>]static member Sequence (t: Result<Async<'T>, 'Error>) = Async<_>.Sequential t


/// Creates an async Choice from a Choice where the Choice1Of2 case is async.
static member Sequence (t: Choice<Async<'T>, 'Choice2Of2>) : Async<Choice<'T,'Choice2Of2>> =
static member Sequential (t: Choice<Async<'T>, 'Choice2Of2>) : Async<Choice<'T,'Choice2Of2>> =
match t with
| Choice1Of2 a -> Async.Map Choice1Of2 a
| Choice2Of2 e -> async.Return (Choice2Of2 e)
[<Obsolete("Renamed to Async.Sequential")>]static member Sequence (t: Choice<Async<'T>, 'Choice2Of2>) = Async<_>.Sequential t


/// Creates an async Result from a Result where both cases are async.
static member Bisequence (t: Result<Async<'T>, Async<'Error>>) : Async<Result<'T,'Error>> =
static member Bisequential (t: Result<Async<'T>, Async<'Error>>) : Async<Result<'T,'Error>> =
match t with
| Ok a -> Async.Map Ok a
| Error e -> Async.Map Error e
[<Obsolete("Renamed to Async.Bisequential")>]static member Bisequence (t: Result<Async<'T>, Async<'Error>>) = Async.Bisequential t


/// Creates an async Choice from a Choice where both cases are async.
static member Bisequence (t: Choice<Async<'T>, Async<'Choice2Of2>>) : Async<Choice<'T,'Choice2Of2>> =
static member Bisequential (t: Choice<Async<'T>, Async<'Choice2Of2>>) : Async<Choice<'T,'Choice2Of2>> =
match t with
| Choice1Of2 a -> Async.Map Choice1Of2 a
| Choice2Of2 e -> Async.Map Choice2Of2 e
[<Obsolete("Renamed to Async.Bisequential")>]static member Bisequence (t: Choice<Async<'T>, Async<'Choice2Of2>>) = Async.Bisequential t


type Option<'t> with

/// Returns None if it contains a None element, otherwise a list of all elements
static member Sequence (t: seq<option<'t>>) =
/// Returns None if it contains a None element, otherwise a list of all elements.
static member Sequential (t: seq<option<'t>>) =
#if FABLE_COMPILER
let mutable ok = true
let res = Seq.toArray (seq {
Expand All @@ -253,14 +266,15 @@ module Extensions =

if noneFound
then None
else
Some (accumulator.Close () |> Array.toSeq)
else accumulator.Close () |> Array.toSeq |> Some
#endif
[<Obsolete("Renamed to Option.Sequential")>]static member Sequence (t: seq<option<'t>>) = Option.Sequential t


type ValueOption<'t> with

/// Returns None if it contains a None element, otherwise a list of all elements
static member Sequence (t: seq<voption<'t>>) =
/// Returns None if it contains a None element, otherwise a list of all elements.
static member Sequential (t: seq<voption<'t>>) =
#if FABLE_COMPILER
let mutable ok = true
let res = Seq.toArray (seq {
Expand All @@ -281,14 +295,15 @@ module Extensions =

if noneFound
then ValueNone
else
ValueSome (accumulator.Close () |> Array.toSeq)
else accumulator.Close () |> Array.toSeq |> ValueSome
#endif
[<Obsolete("Renamed to ValueOption.Sequential")>]static member Sequence (t: seq<voption<'t>>) = ValueOption.Sequential t


type Choice<'t, 'error> with
type Choice<'T1, 'T2> with

/// Returns the first Error if it contains an Error element, otherwise a list of all elements
static member Sequence (t: seq<Choice<_, _>>) =
/// Returns the first Choice2Of2 if it contains a Choice2Of2 element, otherwise a list of all Choice1Of2 elements.
static member Sequential (t: seq<Choice<'T1, 'T2>>) =
#if FABLE_COMPILER
let mutable error = ValueNone
let res = Seq.toArray (seq {
Expand All @@ -302,7 +317,7 @@ module Extensions =
| ValueNone -> Choice1Of2 (Array.toSeq res)
| ValueSome e -> Choice2Of2 e
#else
let mutable accumulator = ArrayCollector<'t> ()
let mutable accumulator = ArrayCollector<'T1> ()
let mutable error = ValueNone
use e = t.GetEnumerator ()
while e.MoveNext () && error.IsNone do
Expand All @@ -313,28 +328,30 @@ module Extensions =
| ValueNone -> Choice1Of2 (accumulator.Close () |> Array.toSeq)
| ValueSome x -> Choice2Of2 x
#endif
[<Obsolete("Renamed to Choice.Sequential")>]static member Sequence (t: seq<Choice<_, _>>) = Choice<_, _>.Sequential t

/// Returns all Errors combined, otherwise a sequence of all elements.
static member Parallel (combiner, t: seq<Choice<_, _>>) =

/// Returns all Choice2Of2's combined, otherwise a sequence of all Choice1Of2 elements.
static member Parallel (choice2Combiner, t: seq<Choice<'T1, 'T2>>) =
let mutable error = ValueNone
let res = Seq.toArray (seq {
use e = t.GetEnumerator ()
while e.MoveNext () do
match e.Current, error with
| Choice1Of2 v, ValueNone -> yield v
| Choice2Of2 e, ValueNone -> error <- ValueSome e
| Choice2Of2 e, ValueSome x -> error <- ValueSome (combiner x e)
| Choice2Of2 e, ValueSome x -> error <- ValueSome (choice2Combiner x e)
| _ -> () })

match error with
| ValueNone -> Choice1Of2 (Array.toSeq res)
| ValueSome e -> Choice2Of2 e


type Result<'t, 'error> with
type Result<'T, 'Error> with

/// Returns the first Error if it contains an Error element, otherwise a list of all elements
static member Sequence (t: seq<Result<_, _>>) =
/// Returns the first Error if it contains an Error element, otherwise a sequence of all elements.
static member Sequential (t: seq<Result<'T, 'Error>>) =
#if FABLE_COMPILER
let mutable error = ValueNone
let res = Seq.toArray (seq {
Expand All @@ -348,7 +365,7 @@ module Extensions =
| ValueNone -> Ok (Array.toSeq res)
| ValueSome e -> Error e
#else
let mutable accumulator = ArrayCollector<'t> ()
let mutable accumulator = ArrayCollector<'T> ()
let mutable error = ValueNone
use e = t.GetEnumerator ()
while e.MoveNext () && error.IsNone do
Expand All @@ -359,18 +376,19 @@ module Extensions =
| ValueNone -> Ok (accumulator.Close () |> Array.toSeq)
| ValueSome x -> Error x
#endif
[<Obsolete("Renamed to Result.Sequential")>]static member Sequence (t: seq<Result<_, _>>) = Result.Sequential t

/// Returns all Errors combined, otherwise a sequence of all elements.
static member Parallel (combiner, t: seq<Result<_, _>>) =
static member Parallel (errorCombiner, t: seq<Result<'T, 'Error>>) =
let mutable error = ValueNone
let res = Seq.toArray (seq {
use e = t.GetEnumerator ()
while e.MoveNext () do
match e.Current, error with
| Ok v , ValueNone -> yield v
| Error e, ValueNone -> error <- ValueSome e
| Error e, ValueSome x -> error <- ValueSome (combiner x e)
| _ -> () })
| Error e, ValueSome x -> error <- ValueSome (errorCombiner x e)
| _ -> () })

match error with
| ValueNone -> Ok (Array.toSeq res)
Expand Down

0 comments on commit 726b20b

Please sign in to comment.