Skip to content

Commit dc551e4

Browse files
committed
+ sequence for NonEmptyList and some XML docs
1 parent 7346c07 commit dc551e4

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/FSharpPlus/Data/List.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@ open FSharpPlus.Internals.Prelude
99

1010
/// Additional operations on List
1111
module List =
12+
13+
/// <summary>
14+
/// Evaluates each action in the list from left to right and collect the results.
15+
/// </summary>
1216
let inline sequence (ms: list<'``Applicative<'T>``>) : '``Applicative<list<'T>>`` = sequence ms
1317

18+
/// <summary>
19+
/// Maps each element of the list to an action, evaluates these actions from left to right and collect the results.
20+
/// </summary>
1421
let inline traverse (f: 'T->'``Applicative<'U>``) (xs:list<'T>) : '``Applicative<list<'U>>`` = traverse f xs
1522

1623
let inline foldM (f: 'T->'U->'``Monad<'T>``) (a: 'T) (bx:list<'U>) : '``Monad<'T>`` =

src/FSharpPlus/Data/NonEmptyList.fs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,19 @@ module NonEmptyList =
130130
| h::t -> cons s (tails {Head = h; Tail = t})
131131

132132
#if !FABLE_COMPILER || FABLE_COMPILER_3
133-
let inline traverse (f: 'T->'``Functor<'U>``) (s: NonEmptyList<'T>) =
134-
let lst = traverse f (toList s) : '``Functor<'List<'U>>``
133+
134+
/// <summary>
135+
/// Maps each element of the list to an action, evaluates these actions from left to right and collect the results.
136+
/// </summary>
137+
let inline traverse (f: 'T -> '``Functor<'U>``) (source: NonEmptyList<'T>) =
138+
let lst = traverse f (toList source) : '``Functor<'List<'U>>``
135139
(create << List.head |> fun f x -> f x (List.tail x)) <!> lst : '``Functor<NonEmptyList<'U>>``
140+
141+
/// <summary>
142+
/// Evaluates each action in the list from left to right and collect the results.
143+
/// </summary>
144+
let inline sequence (source: NonEmptyList<'``Functor<'T>``>) : '``Functor<NonEmptyList<'T>>`` = traverse id source
145+
136146
#endif
137147

138148
/// <summary>Returns the average of the elements in the list.</summary>
@@ -255,7 +265,10 @@ type NonEmptyList<'t> with
255265
static member ToSeq (s: NonEmptyList<'a>, [<Optional>]_impl: ToSeq ) = NonEmptyList.toList s |> List.toSeq
256266

257267
[<EditorBrowsable(EditorBrowsableState.Never)>]
258-
static member inline Traverse (s: NonEmptyList<'T>, f: 'T->'``Functor<'U>``) : '``Functor<NonEmptyList<'U>>`` = NonEmptyList.traverse f s
268+
static member inline Traverse (s: NonEmptyList<'T>, f: 'T -> '``Functor<'U>``) : '``Functor<NonEmptyList<'U>>`` = NonEmptyList.traverse f s
269+
270+
[<EditorBrowsable(EditorBrowsableState.Never)>]
271+
static member inline Sequence (s: NonEmptyList<'``Functor<'T>``>) : '``Functor<NonEmptyList<'T>>`` = NonEmptyList.sequence s
259272

260273
static member Replace (source: NonEmptyList<'T>, oldValue: NonEmptyList<'T>, newValue: NonEmptyList<'T>, _impl: Replace ) =
261274
let lst = source |> NonEmptyList.toSeq |> Seq.replace oldValue newValue |> Seq.toList

src/FSharpPlus/Data/Seq.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ open FSharpPlus.Internals.Prelude
1010
/// Additional operations on Seq
1111
module Seq =
1212

13+
/// <summary>
14+
/// Evaluates each action in the sequence from left to right and collect the results.
15+
/// </summary>
1316
let inline sequence (ms: seq<'``Applicative<'T>``>) : '``Applicative<seq<'T>>`` = sequence ms
1417

18+
/// <summary>
19+
/// Maps each element of the sequence to an action, evaluates these actions from left to right and collect the results.
20+
/// </summary>
1521
let inline traverse (f: 'T->'``Applicative<'U>``) (xs: seq<'T>) : '``Applicative<seq<'U>>`` = traverse f xs
1622

1723
let inline replicateM count (initial: '``Applicative<'T>``) = sequence (Seq.replicate count initial)

0 commit comments

Comments
 (0)