Skip to content

Commit

Permalink
Update readme with example for partition
Browse files Browse the repository at this point in the history
  • Loading branch information
Mafii committed Oct 1, 2024
1 parent 0f0fd7e commit 93c1c07
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,38 @@ partial record Shape
</details>


### `GeneratePartitionExtension`
Set `GeneratePartitionExtension` to true to auto-generate partition methods for `IEnumerable<T>` of your type.

```cs
using Funcky;
using System.Text.Serialization;

[DiscriminatedUnion(GeneratePartitionExtension = true)]
public abstract partial record Result
{
public sealed partial record Ok() : Result;

public sealed partial record Warning(string Message) : Result;

public sealed partial record Error(string Message) : Result;
}
```

<details>

<summary>Usage</summary>

```cs
var results = new Result[] { new Result.Ok(), /* ... */ }

// N-Tuple extension method:
var (oks, warnings, errors) = results.Partition();

// Extension method with result selector:
var warningAndErrorCount = results.Partition(resultSelector: (_, warnings, errors) => warnings.Count + errors.Count);
```

</details>

[json-polymorphism-docs]: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism

0 comments on commit 93c1c07

Please sign in to comment.