-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
51 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (C) Tenacom and contributors. Licensed under the MIT license. | ||
// See LICENSE file in the project root for full license information. | ||
|
||
#nullable enable | ||
|
||
// --------------------------------------------------------------------------------------------- | ||
// Miscellaneous utilities | ||
// --------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Linq; | ||
|
||
/* | ||
* Summary : Filters a sequence of nullable values, taking only those that are not null. | ||
* Type params : T - The type of the elements of this. | ||
* Params : this - An IEnumerable<T> to filter.</param> | ||
* Returns : An IEnumerable<T> that contains elements from the input sequence that are not null. | ||
*/ | ||
static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> @this) | ||
where T : class | ||
{ | ||
return @this.Where(IsNotNull) as IEnumerable<T>; | ||
|
||
static bool IsNotNull(T? x) => x is not null; | ||
} | ||
|
||
/* | ||
* Summary : Filters a sequence of nullable values, taking only those that are not null. | ||
* Type params : T - The type of the elements of this. | ||
* Params : this - An IEnumerable<T> to filter.</param> | ||
* Returns : An IEnumerable<T> that contains elements from the input sequence that are not null. | ||
*/ | ||
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> @this) | ||
where T : struct | ||
{ | ||
return @this.Where(IsNotNull).Select(GetValue); | ||
|
||
static bool IsNotNull(T? x) => x.HasValue; | ||
|
||
static T GetValue(T? x) => x!.Value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters