Giter Site home page Giter Site logo

orxfun / orx-fun-option Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 1.57 MB

An option type for C# aiming to be explicit while concise.

Home Page: https://www.nuget.org/packages/Orx.Fun.Option

License: MIT License

C# 100.00%
csharp dotnet maybe monad option optional

orx-fun-option's Introduction

Orx.Fun.Option

An option type for C# aiming to be explicit while concise.

Complete auto-generated documentation can be found here: sandcastle-documentation.

Why?

  1. Optional (maybe) values are necessary.
    • Explicit optional arguments
    • Explicit optional returns
    • Continuations without overwhelming null/none checks
    • ...
  2. Built-in nullables do not seem to satisfy the requirements.

Explicit Optional Arguments

static void Solve(Instance instance, Opt<SolverParameters> parameters = default)
{
	// solve with the parameters if provided;
	// with default parameters otherwise.
}

// caller knows that it is okay not to provide parameters
Solve(instance);
Solve(instance, None<SolverParameters>());

// or solve with parameters
SolverParameters pars = new()
{
	TimeLimit = 60,
};
Solve(instance, Some(pars));

Explicit Optional Returns

An easy example to demonstrate this is the linq method First:

var numbers = new int[] { 1, 3, 5, 7 };
int firstEven = numbers.First(x => x % 2 == 0); // throws!!!

First method actually does not (cannot always) return an int. FirstOrDefault certainly does not fix the issue. On the other hand, solution is easy with optionals:

var numbers = new int[] { 1, 3, 5, 7 };
Opt<int> firstEven = numbers.FirstOrNone(x => x % 2 == 0);

Continuations

The following dummy example illustrates the use of Map and FlatMap to enable continuations.

/// Returns Some of the user if there exists one with the given id; None otherwise
static Opt<User> GetUserById(int id)
{
	// ...
}

// task to achieve:
// 1. get the first even number
// 2. halve it
// 3. get the user with the number as id, if exists
// 4. return the users's name

var numbers = new int[] { 1, 3, 5, 7 };
Opt<string> name = numbers.FirstOrNone(x => x % 2 == 0)
    .Map(x => x / 2)
    .FlatMap(GetUserById)
    .Map(user => user.Name);

orx-fun-option's People

Contributors

ugur-arikan avatar orxfun avatar

Watchers

 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.