Skip to content

Instantly share code, notes, and snippets.

@NickDarvey
Last active August 23, 2019 05:43
Show Gist options
  • Save NickDarvey/7823941071df3897eb62e5f3ff84ad3e to your computer and use it in GitHub Desktop.
Save NickDarvey/7823941071df3897eb62e5f3ff84ad3e to your computer and use it in GitHub Desktop.
public static Verifier Combine(params Verifier[] verifiers) =>
expression =>
{
var current = Init;
foreach (var verify in verifiers)
{
var next = verify(expression);
current = next.Apply(current.Apply(KeepRight));
}
return current;
};
public static Verifier Combine2(params Verifier[] verifiers) =>
expression => from xs in verifiers.TraverseA(v => v(expression))
select Unit.Default;
| Method | VCount | Mean | Ratio | Gen 0 | Allocated |
|--------- |------- |-----------:|------:|-------:|----------:|
| Combine | 2 | 163.5 ns | 1.00 | - | - |
| Combine2 | 2 | 346.0 ns | 2.63 | 0.2828 | 592 B |
| | | | | | |
| Combine | 10 | 1,206.3 ns | 1.00 | - | - |
| Combine2 | 10 | 2,425.0 ns | 2.03 | 1.0452 | 2192 B |
/// <summary>
/// Combines verifiers
/// </summary>
/// <remark>
/// | Method | VCount | Mean | Ratio | Gen 0 | Allocated |
/// |--------------------- |------- |-----------:|------:|-------:|----------:|
/// | ThisCombine | 2 | 163.5 ns | 1.00 | - | - |
/// | CombineWithTraverseA | 2 | 346.0 ns | 2.63 | 0.2828 | 592 B |
/// | | | | | | |
/// | ThisCombine | 10 | 1,206.3 ns | 1.00 | - | - |
/// | CombineWithTraverseA | 10 | 2,425.0 ns | 2.03 | 1.0452 | 2192 B |
///
/// </remark>
/// <param name="verifiers">The verifiers to combine.</param>
public static Verifier Combine(params Verifier[] verifiers) =>
expression =>
{
var current = Init;
foreach (var verify in verifiers)
{
var next = verify(expression);
current = next.Apply(current.Apply(KeepRight));
}
return current;
};
// ... _combineWithTraverseA = CombineWithTraverseA(sampleVerifiers);
/// <summary>
/// The combine method using TraverseA.
/// </summary>
[Benchmark]
public Validation<string, Unit> CombineWithTraverseA()
{
return _combineWithTraverseA(_expression);
}
private static Verifier CombineWithTraverseA(params Verifier[] verifiers) =>
expression => from xs in verifiers.TraverseA(v => v(expression))
select Unit.Default;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment