MalFunction.Result 1.0.0

dotnet add package MalFunction.Result --version 1.0.0
                    
NuGet\Install-Package MalFunction.Result -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MalFunction.Result" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MalFunction.Result" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="MalFunction.Result" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MalFunction.Result --version 1.0.0
                    
#r "nuget: MalFunction.Result, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MalFunction.Result@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MalFunction.Result&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=MalFunction.Result&version=1.0.0
                    
Install as a Cake Tool

Malfunction.Result

Getting Started

The result type is an interface that contains two records, Pass and Fail

  public interface IResult<TPass, TFail>
  {
      public sealed record Pass(TPass Value) : IResult<TPass, TFail>;

      public sealed record Fail(TFail Value) : IResult<TPass, TFail>;
  }

To define a result type, make a type alias. If using it across mulpiple files, define it globally in a seperate file

global using ValidationResult = MalFunction.Result.IResult<int, string>;

Set the return value of your method to your result and return a new pass or fail state

public ValidationResult IsGreaterThan5(int number)
{
    if (number > 5)
        return new ValidationResult.Pass(number);
    else
        return new ValidationResult.Fail("Number was not greater than 5");
}

To handle a result, make a type check and handle the success and fail states

string message = IsGreaterThan5(6) switch
{
    ValidationResult.Pass pass -> $"Number {pass.Value} was validated",
    ValidationResult.Fail fail -> $"Number failed validation with error: {fail.Value}"
};

Console.Writeline(message);

Manipulating Results

Map

Projects result's value into a new form

IResult<int, string> intResult = new IResult<int, string>.Pass(20);
IResult<string, string> stringResult = intResult.Map(x => x.ToString());

Bind

Projects result's value into a new result

IResult<int, string> intResult = new IResult<int, string>.Pass(20);
IResult<string, string> stringResult = intResult.Map(x => new IResult<string, string>.Pass(x.ToString()));

Traverse

Map a result producing function over a list to get a new result

int[] values = [1, 2, 3];

IResult<int, string> NumberMustBeEven()
{
    if (x % 2 = 0)
        return new IResult<int[], string[]>.Pass(number);
    else
        return new IResult<int[], string[]>.Fail("Number was odd")
}

IResult<int[], string[]> result = values.Traverse(NumberMustBeEven);

//using fail aggregator
IResult<int[], string> result = values.Traverse(NumberMustBeEven, (error1, error2) => error1 + error2);

//using fail aggregator with projection
record Error(string[] Errors)
IResult<int[], Error> result = results.Traverse(
    NumberMustBeEven,
    message => new Error([message]),
    (error, message) => error.Errors.Append(message)
);

Sequence

Converts a collection of results into a single result containing a collection

IResult<int, string>[] results =
[
    new IResult<int, string>.Pass(12),
    new IResult<int, string>.Pass(20),
    new IResult<int, string>.Fail("error")
];

IResult<int[], string[]> result = results.Sequence();

//using fail aggregator
IResult<int[], string> result = results.Sequence((error1, error2) => error1 + error2);

//using fail aggregator with projection
record Error(string[] Errors)
IResult<int[], Error> result = results.Sequence(
    message => new Error([message]),
    (error, message) => error.Errors.Append(message)
);

For a detailed explenation on how to use elevated worlds like this result type i recommend you read: https://fsharpforfunandprofit.com/posts/elevated-world/

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 122 4/28/2026