Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

.NET functional programming and other utilities

License

NotificationsYou must be signed in to change notification settings

gsscoder/sharpx

Repository files navigation

SharpX Logo

SharpX is derived fromCSharpx 2.8.0-rc.2 (which was practically a stable) andRailwaySharp 1.2.2.

CSharpX Downloads

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.

SharpX Downloads

Targets

  • .NET Standard 2.1
  • .NET 8.0

Install via NuGet

You can install it via NuGet:

$ dotnet add package SharpX --version 8.3.6  Determining projects to restore...  ...

Overview

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.

Maybe

  • 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)

Either

  • Represents a value that can contain either a value or an error.
  • Similar to Haskelldata Either a b = Left a | Right b type.
  • Similar also to F#Choice<'T, 'U>.
  • Like in Haskell the convention is to letRight 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 usingResult.

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

  • 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();}

FSharpResultExtensions

  • Convenient extension methods to consumeFSharpResult<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,"$","€"));

StringExtensions

  • General purpose and randomness string manipulation extensions. Few more methods and non-extension version can be found inStrings 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'

EnumerableExtensions

  • 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] }

Icon

Tool icon designed by Cattaleeya Thongsriphong fromThe Noun Project

About

.NET functional programming and other utilities

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp