Giter Site home page Giter Site logo

infrastructure.option's Introduction

Build Status

Usage

Get nuget https://www.nuget.org/packages/Infrastructure.Option/

Overview

Option<T> is a simple option type usable with C#

Option.Some<T> represents an available value and Option.None<T> is non-available value.

Option is a static class that can be used to create Option<T> instances.

For more information about option types see https://en.wikipedia.org/wiki/Option_type

Examples

Creating some values

var someString = Option.Some("Example value");
var someInteger = Option.Some(12345);

Creating none values

var noneString = Option.None<string>();
var noneInteger = Option.None<int>();

Pattern matching optional values

var option = Option.Some("Example value");
var value = option is Some<string> some ? some.Value : "Someting else";

Fallback values using fluent syntax

Option<string> option = Option.None<string>();
var fallback = option.Or("Fallback value");
var anotherFallback = option.Or(() => "Fallback expression")
Option<string> option = Option.None<string>();
Option<string> anotherOption = Option.Some("Example value");
var fallback = option.Otherwise(anotherOption);
var anotherFallback = option.Otherwise(() => anotherOption);

Choosing first some value from many options

var options = new Option<string>[] {
    Option.None<string>,
    Option.Some("Example value")
};

var option = options.FirstSomeOtherwiseNone();

Mapping collection values

var collection = new Option<string>[]
    {
        Option.None<string>(),
        Option.Some("1"),
        Option.None<string>(),
        Option.None<string>(),
        Option.Some("2"),
        Option.Some("3"),
        Option.Some("4"),
        Option.None<string>(),
        Option.Some("5"),
        Option.None<string>(),
    };
var values = collection.Values(); // values is [ "1", "2", "3", "4", "5" ]

infrastructure.option's People

Contributors

vilppu avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.