Skip to content
David Arno edited this page Jun 23, 2015 · 34 revisions

Succinc<T>

Discriminated unions, pattern matching and partial applications for C#


Current release: v1.2.0

Release Notes

v1.2.0

  1. Breaking change: Previously, the partial application functionality was incorrectly referred to as functional composition. The code and docs have been updated to use the correct term. This has involved a change of namespace and method name for partial applications. Whilst Succinc<T> aims to adopt semantic versioning, those rules have been broken here. The user base is still small as is assumed to be composed of early adopters who are prepared to fix code broken by this change. To fix affected code:

    a. Change SuccincT.FunctionalComposition namespace imports to SuccincT.PartialApplications. b. Change all xx.Compose(...) occurances to xx.Apply(...) c. Change all xx.TailCompose(...) occurances to xx.TailApply(...)

  2. Tail-application functionality has been expanded to support methods with optional tail parameters (which do not match the Action delegates).

v1.1.0

  1. Improved behaviour for Do() and Else() in patterns. Previously, these had to be lambdas, even if the value wasn't required. Now, simple expressions are supported too.
  2. The functional composition aspects of Succinc<T> have been expanded to support tail composition. This whole area needs proper documentation, which will appear soon.

v1.0.0 This was the initial release.

Features

Discriminated Unions

Succinc<T> provides a set of union types (Union<T1,T2> through to Union<T1,T2,T3,T4>) where an instance will hold exactly one value of one of the specified types. In addition, it provides the likes of Option<T> that can have the value Some<T> or None.

Succinc<T> uses Option<T> to provide replacements for the .NET basic types' TryParse() methods and Enum.Parse(). In all cases, these are extension methods to string and they return Some<T> on a successful parse and None when the string is not a valid value for that type. No more out parameters!

Pattern Matching

Succinc<T> can pattern match values, unions etc in a way similar to F#'s pattern matching features. It uses a fluent syntax to achieve this. Some examples of its abilities:

    public static void PrintColorName(Color color)
    {
        color.Match()
             .With(Color.Red).Do(x => Console.WriteLine("Red"))
             .With(Color.Green).Do(x => Console.WriteLine("Green"))
             .With(Color.Blue).Do(x => Console.WriteLine("Blue"))
             .Exec();
    }

    public static string SinglePositiveOddDigitReporter(Option<int> data)
    {
        return data.Match<string>()
                   .Some().Of(0).Do(x => "0 isn't positive or negative")
                   .Some().Where(x => x == 1 || x == 3 || x == 5 || x == 7 || x == 9).Do(x => x.ToString())
                   .Some().Where(x => x > 9).Do(x => string.Format("{0} isn't 1 digit", x))
                   .Some().Where(x => x < 0).Do(i => string.Format("{0} isn't positive", i))
                   .Some().Do(x => string.Format("{0} isn't odd", x))
                   .None().Do(() => string.Format("There was no value"))
                   .Result();
    }

See the Succinc<T> pattern matching guide for more details.

Partial Applications

Succinc<T> supports partial function applications. A parameter can be supplied to a multi-parameter method and a new function will be returned that takes the remaining parameters. For example:

Func<int,int> times = (p1, p2) => p1 * p2;
var times8 = times.Apply(8);
var result = times8(9); // <- result == 72

See the Succinc<T> partial applications guide for more details.

Types

The following types are defined by Succinc<T>. Links will appear as the pages for each one gets written.

  • Option
  • Union<T1,T2>
  • Union<T1,T2,T3>
  • Union<T1,T2,T3,T4>
  • ValueOrError