Giter VIP home page Giter VIP logo

functionalsharp's Issues

PatternMatching should work with Action

FunctionalSharp.PatternMatching should work with Action instead of Action<T> too:

value.Match()
    .With(v => v == "foo", DoSomething())
    .Else(DoSomethingElse()); 

instead of:

 var iDoNotNeedThis = value.Match()
      .With(v => v == "foo", v => { DoSomething(); return 0; })
      .Else(v => { DoSomethingElse(); return 0; }); 

DiscriminatedUnion not setting tag properly

There seems to be an issue with the tag numbering. Instead of tag=0; tag=1; tag=2; it reads tag=1; tag=1; tag=2;:

if(value.GetType() == typeof(Type1))
{
	Item1 = (Type1)value; 
	tag = 1; // ERROR: should be 0
}
else if(value.GetType() == typeof(Type2))
{
	Item2 = (Type2)value; 
	tag = 1; 
}

PatternMatchExtensions should work with null values

I like to match a value against null and convert it to an option type.

var item = selectedItem.Match()
    .With(si => string.IsNullOrEmpty(si), new None<Fruit>())
    .With(si => si == "Banana", (new Banana()).ToOption())
    .With(si => si == "Apple", (new Apple()).ToOption())
    .Else(new None<Fruit>()); 

unfortunately this does not work when 'selectedItem' is null in the current version.

Option-Types should work with non-nullables (structs)

I regularily have the case that I wast to use the option type instead of nullables because of the added savety. unfortunately it is rather cruel to cast otructure types to a nullable before making them an option type and then unpacking them.

instead of:

var optionalYear = year == 0 ? new None<DateTime?> : ((DateTime?)new DateTime(year,1,1)).ToOption(); 
var unpackedYear = optionalYear.Match(y => y.HasValue ? y.Value.Year : DateTime.MinValue.Year, DateTime.MinValue.Year); 

i want to simply:

var optionalYear = year == 0 ? new None<DateTime> : (new DateTime(year,1,1)).ToOption(); 
var unpackedYear = optionalYear.Match(y => y.Year, DateTime.MinValue.Year); 

which is much simpler to use.

PatternMatchSingleAction<T, TResult> missing

I am missing the following method in PatternMatchSingleContext<T>:

public PatternMatchSingleAction<T, TResult> With<TResult>(T valueToMatch, Action<TResult> action)
{
    return With(this.value, (value) => value.Equals(valueToMatch), _ => action());
}

This also requires an implementation of the class PatternMatchSingleAction<T, TResult> and likely an implementation of other appropriate methods.

Use case

public enum VerticalPosition{
   Top, Center, Bottom
}

private double CalculateRefY(VerticalPosition position)
{
    return position.Match<VerticalPosition, double>()
        .With<double>(VerticalPosition.Top, CalculateRefYTop)
        .With<double>(VerticalPosition.Bottom, CalculateRefYBottom)
        .Else<double>(CalculateRefYCenter);
}

private double CalculateRefYTop() { return 0d; }
private double CalculateRefYBottom() { return Height; }
private double CalculateRefYCenter() { return Height / 2d; }

Workaround
Thankfully it is about the same amount of coding to use an switch statement. I just don't like switch as much:

private double CalculateRefY(VerticalPosition position)
{
    switch(position)
    {
        case VerticalPosition.Top: return CalculateRefYTop();
        case VerticalPosition.Bottom: return CalculateRefYBottom();
        default: return CalculateRefYCenter();
    }
}

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.