Skip to content

Commit 7ec23cc

Browse files
authored
Add ignore to some common type extensions (#658)
1 parent 8d0e534 commit 7ec23cc

5 files changed

Lines changed: 46 additions & 2 deletions

File tree

src/FSharpPlus/Extensions/Observable.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open System
66
open System.Threading
77
open System.Threading.Tasks
88
open System.Runtime.ExceptionServices
9+
open FSharpPlus.Internals.Errors
910
open FSharpPlus.Data
1011

1112
/// Additional operations on Observable<'T>
@@ -33,6 +34,14 @@ module Observable =
3334
member _.OnError e = observer.OnNext (Error (ExceptionDispatchInfo.Capture e)) }}
3435

3536

37+
/// Ignores the values resulting from each OnNext call.
38+
/// Only the side-effects of the observable are preserved.
39+
/// <param name="source">The source observable.</param>
40+
/// <returns>An observable that ignores the values of the source observable.</returns>
41+
let ignore (source: IObservable<'T>) : IObservable<unit> =
42+
let source = nullArgCheck (nameof source) source
43+
44+
Observable.map ignore source
3645

3746

3847
let toAsyncSeq (source: System.IObservable<'T>) : SeqT<Async<bool>, 'T> = monad.plus {

src/FSharpPlus/Extensions/Option.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ module Option =
7171
| (true, x) -> Some x
7272
| (false, _) -> None
7373

74+
/// <summary>Ignores the value inside the option, if any.</summary>
75+
/// <param name="source">The option value.</param>
76+
/// <returns><c>Some ()</c> if the option is <c>Some</c>, <c>None</c> otherwise.</returns>
77+
let ignore (source: option<'T>) =
78+
match source with
79+
| Some _ -> Some ()
80+
| None -> None
81+
7482
/// <summary>
7583
/// Extracts a value from either side of an Option.
7684
/// </summary>

src/FSharpPlus/Extensions/Result.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,11 @@ module Result =
176176
| Error e, Ok _, Ok _ | Ok _, Error e, Ok _ | Ok _, Ok _, Error e -> Error e
177177
| Ok _, Error e1, Error e2 | Error e1, Ok _, Error e2 | Error e1, Error e2, Ok _ -> Error (combiner e1 e2)
178178
| Error e1, Error e2, Error e3 -> Error (combiner (combiner e1 e2) e3)
179+
180+
/// <summary>Ignores the value inside the result, if any.</summary>
181+
/// <param name="source">The result value.</param>
182+
/// <returns><c>Ok ()</c> if the result is <c>Ok</c>, <c>Error e</c> otherwise.</returns>
183+
let ignore (source: Result<'T, 'Error>) =
184+
match source with
185+
| Ok _ -> Ok ()
186+
| Error e -> Error e

src/FSharpPlus/Extensions/Seq.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,12 @@ module Seq =
361361
|> nullArgCheck (nameof source)
362362
|> Seq.indexed
363363
|> Seq.choose (fun (a, b) -> mapping a b)
364+
365+
/// <summary>Ignores the values resulting from each iteration inside the sequence.</summary>
366+
/// <param name="source">The sequence value.</param>
367+
/// <returns> A sequence of unit values.</returns>
368+
/// <remarks>It can be used to convert a non-generic IEnumerable to a unit seq.</remarks>
369+
let ignore (source: Collections.IEnumerable) =
370+
let source = nullArgCheck (nameof source) source
371+
372+
seq { for _ in source do yield () }

src/FSharpPlus/Extensions/ValueOption.fs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,15 @@ module ValueOption =
8181
let ofOption (source: option<'T>) =
8282
match source with
8383
| Some x -> ValueSome x
84-
| None -> ValueNone
84+
| None -> ValueNone
85+
86+
/// <summary>Ignores the value inside the option, if any.</summary>
87+
/// <param name="source">The option value.</param>
88+
/// <returns><c>ValueSome ()</c> if the option is <c>ValueSome</c>, <c>ValueNone</c> otherwise.</returns>
89+
let ignore (source: ValueOption<'T>) =
90+
match source with
91+
| ValueSome _ -> ValueSome ()
92+
| ValueNone -> ValueNone
8593

8694
/// <summary>
8795
/// Extracts a value from either side of a ValueOption.
@@ -90,4 +98,6 @@ module ValueOption =
9098
/// <param name="fNone">The function to apply if the option is ValueNone.</param>
9199
/// <param name="source">The option to extract the value from.</param>
92100
let inline either ([<InlineIfLambda>]fSome: 'T -> 'U) ([<InlineIfLambda>]fNone: unit -> 'U) (source: ValueOption<'T>) : 'U =
93-
match source with ValueSome v -> fSome v | ValueNone -> fNone ()
101+
match source with
102+
| ValueSome v -> fSome v
103+
| ValueNone -> fNone ()

0 commit comments

Comments
 (0)