patrickkk / functionalsharp Goto Github PK
View Code? Open in Web Editor NEWProvides some functional mechanisms in C#
Home Page: http://pvanlohuizen.nl/FunctionalSharp/
License: Apache License 2.0
Provides some functional mechanisms in C#
Home Page: http://pvanlohuizen.nl/FunctionalSharp/
License: Apache License 2.0
Create code comments and a wiki on the usage of the Discriminated union list
Create code ocmments and wiki documentation for Document Discriminated Extensions
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; });
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;
}
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.
When trying to navigate to the documentation page: http://pvanlohuizen.nl/FunctionalSharp/ there is a "There isn't a GitHub Pages site here." 404 page.
Sort of similar focus:
https://github.com/wallymathieu/with
Is there anything you can use from that lib? Or anything that could inspire other ideas?
have a look into migrating the generation code to scripty
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.
FunctionalSharp should be ported to dotnetstandard. Fody.NullGuard
is not available, so there need to be some additional checks.
Consolidate everything in a single project allowing for easier publishing and usage
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();
}
}
create a T4 template where a named class inherrits from a DiscruminatedUnion/withBase/List that automatically implements the constructors and allows for addition (make partial)
Create code comments and a wiki on how to use the Discriminated union.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.