- Notifications
You must be signed in to change notification settings - Fork2
.NET functional programming and other utilities
License
gsscoder/sharpx
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
SharpX is derived fromCSharpx 2.8.0-rc.2 (which was practically a stable) andRailwaySharp 1.2.2.
While both projects were meant mainly for source inclusion, SharpX is designed to be pulled fromNuGet.
The library containsfunctional types and other utilities, includingtest oriented tools. It follows thedon't reinvent the wheel philosophy. This project was originally inspired byReal-World Functional Programming and includes code fromMoreLINQ.
- .NET Standard 2.1
- .NET 8.0
You can install it via NuGet:
$ dotnet add package SharpX --version 8.3.6 Determining projects to restore... ...
All types are available in the root namespace. Extension methods (except the very specific ones) are inSharpX.Extensions
.SharpX.FsCheck
containsFsCheck generators for property-based testing.
- Encapsulates an optional value that can contain a value or being empty.
- Similar to F#
'T option
/ Haskelldata Maybe a = Just a | Nothing
type.
vargreet=true;varvalue=greet?"world".ToMaybe():Maybe.Nothing<string>();value.Match( who=>Console.WriteLine($"hello{who}!"),()=>Environment.Exit(1));
- Supports LINQ syntax:
varresult1=(30).ToJust();varresult2=(10).ToJust();varresult3=(2).ToJust();varsum=fromr1inresult1fromr2inresult2wherer1>0selectr1-r2 intotempfromr3inresult3selecttemp*r3;varvalue=sum.FromJust();// outcome: 40
- Features sequence extensions:
varmaybeFirst=newint[]{0,1,2}.FirstOrNothing(x=>x==1)// outcome: Just(1)
- Represents a value that can contain either a value or an error.
- Similar to Haskell
data Either a b = Left a | Right b
type. - Similar also to F#
Choice<'T, 'U>
. - Like in Haskell the convention is to let
Right
case hold the value andLeft
keep track of error or similar data. - If you want a more complete implementation of this kind of types, consider using
Result
.
This type was originally present in RailwaySharp. Check the test project to see a more complete usage example.
publicstaticResult<Request,string>ValidateInput(Requestinput){if(input.Name==string.Empty){returnResult<Request,string>.FailWith("Name must not be blank");}if(input.EMail==string.Empty){returnResult<Request,string>.FailWith("Email must not be blank");}returnResult<Request,string>.Succeed(input);}varrequest=newRequest{Name="Giacomo",EMail="gsscoder@gmail.com"};varresult=Validation.ValidateInput(request);result.Match((x,msgs)=>{Logic.SendMail(x.EMail);}, msgs=>{Logic.HandleFailure(msgs)});
Unit
is similar tovoid
but, since it's areal type.void
is not, in fact you can't declare a variable of that type.Unit
allows the use functions without a result in a computation (functional style). It's essentiallyF#unit
andHaskellUnit
.
// prints each word and returns 0 to the shellstaticintMain(string[]args){varsentence="this is a sentence";return(from_infromwordinsentence.Split()selectUnit.Do(()=>Console.WriteLine(word))select0).Distinct().Single();}
- Convenient extension methods to consume
FSharpResult<T, TError>
in simple and functional for other.NET languages.
// pattern match likevarresult=Query.GetStockQuote("ORCL");result.Match( quote=>Console.WriteLine($"Price:{quote.Price}"), error=>Console.WriteLine($"Trouble:{error}"));// mappingvarresult=Query.GetIndex(".DJI");result.Map( quote=>CurrencyConverter.Change(quote.Price,"$","€"));
- Blogpost about it.
- General purpose and randomness string manipulation extensions. Few more methods and non-extension version can be found in
Strings
class.
Console.WriteLine("\t[hello\world@\t".Sanitize(normalizeWhiteSpace:true));// outcome: ' hello world 'Console.WriteLine("I want to change a word".ApplyAt(4, word=>word.Mangle()));// outcome like: 'I want to change &a word'
- Most useful extension methods fromMoreLINQ.
- Some of these reimplemnted (e.g.
Choose
usingMaybe
):
varnumbers=newint[]{0,1,2,3,4,5,6,7,8,9};varevens=numbers.Choose(x=>x%2==0?x.ToJust():Maybe.Nothing<int>());// outcome: {0, 2, 4, 6, 8}
- With other useful methods too:
varsequence=newint[]{0,1,2,3,4}.Intersperse(5);// outcome: {0, 5, 1, 5, 2, 5, 3, 5, 4}varelement=sequence.Choice();// will choose a random elementvarsequence=newint[]{0,1,2,3,4,5,6,7,8,9,10}.ChunkBySize(3);// outcome: { [0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10] }
Tool icon designed by Cattaleeya Thongsriphong fromThe Noun Project
About
.NET functional programming and other utilities
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.