SharpX 6.2.1
See the version list below for details.
dotnet add package SharpX --version 6.2.1
NuGet\Install-Package SharpX -Version 6.2.1
<PackageReference Include="SharpX" Version="6.2.1" />
paket add SharpX --version 6.2.1
#r "nuget: SharpX, 6.2.1"
// Install SharpX as a Cake Addin #addin nuget:?package=SharpX&version=6.2.1 // Install SharpX as a Cake Tool #tool nuget:?package=SharpX&version=6.2.1
SharpX
SharpX is derived from CSharpx 2.8.0-rc.2 (which was practically a stable) and RailwaySharp 1.2.2. While both projects were meant mainly for source inclusion, SharpX is designed to be pulled from NuGet.
The library contains functional types and other utilities, following don't reinvent the wheel philosophy. This project was inspired by Real-World Functional Programming and includes code from MoreLINQ.
Targets
- .NET Core 3.1 LTS
- .NET 6.0 LTS
Install via NuGet
If you prefer, you can install it via NuGet:
$ dotnet add package SharpX --version 6.2.1
Determining projects to restore...
...
Overview
All types are available in root namespace, while extension methods (except ones very specific) are in SharpX.Extensions
.
[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.
var greet = true;
var value = greet ? "world".ToMaybe() : Maybe.Nothing<string>();
value.Match(
who => Console.WriteLine($"hello {who}!"),
() => Environment.Exit(1));
- Supports LINQ syntax:
var result1 = (30).ToJust();
var result2 = (10).ToJust();
var result3 = (2).ToJust();
var sum = from r1 in result1
from r2 in result2
where r1 > 0
select r1 - r2 into temp
from r3 in result3
select temp * r3;
var value = sum.FromJust(); // outcome: 40
- Features sequence extensions:
var maybeFirst = new int[] {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 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
.
Result
This type was originally present in RailwaySharp. Check the test project to see a more complete usage example.
public static Result<Request, string> ValidateInput(Request input)
{
if (input.Name == string.Empty) {
return Result<Request, string>.FailWith("Name must not be blank");
}
if (input.EMail == string.Empty) {
return Result<Request, string>.FailWith("Email must not be blank");
}
return Result<Request, string>.Succeed(input);
}
var request = new Request { Name = "Giacomo", EMail = "gsscoder@gmail.com" };
var result = Validation.ValidateInput(request);
result.Match(
(x, msgs) => { Logic.SendMail(x.EMail); },
msgs => { Logic.HandleFailure(msgs) });
Outcome
- Represents a value that can be a success or a failure in form of a type that can contains a custom error message and optionally an exception.
Outcome ValidateArtifact(Artifact artifact)
{
try {
artifact = ArtifactManager.Load(artifact.Path);
}
catch (IOException e) {
return Result.Failure($"Unable to load artifcat {path}:\n{e.Message}", exception: e);
}
return artifact.CheckIntegrity() switch {
Integrity.Healthy => Outcome.Success(),
_ => Outcome.Failure("Artifact integrity is compromised")
};
}
if (ValidateArtifact(artifact).MatchFailure(out Error error)) {
//Error::ToString creates a string with message and exception details
_logger.LogError(error.Exception.FromJust(), error.ToString());
Environment.Exit(1);
}
// do something useful with artifact
Unit
Unit
is similar tovoid
but, since it's a real 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 essentially F#unit
and HaskellUnit
.
// prints each word and returns 0 to the shell
static int Main(string[] args)
{
var sentence = "this is a sentence";
return (from _ in
from word in sentence.Split()
select Unit.Do(() => Console.WriteLine(word))
select 0).Distinct().Single();
}
FSharpResultExtensions
- Convenient extension methods to consume
FSharpResult<T, TError>
in simple and functional for other .NET languages.
// pattern match like
var result = Query.GetStockQuote("ORCL");
result.Match(
quote => Console.WriteLine($"Price: {quote.Price}"),
error => Console.WriteLine($"Trouble: {error}"));
// mapping
var result = Query.GetIndex(".DJI");
result.Map(
quote => CurrencyConverter.Change(quote.Price, "$", "€"));
- Blog post about it.
StringExtensions
- 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'
EnumerableExtensions
- Most useful extension methods from MoreLINQ.
- Some of these reimplemnted (e.g.
Choose
usingMaybe
):
var numbers = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
var evens = numbers.Choose(x => x % 2 == 0
? x.ToJust()
: Maybe.Nothing<int>());
// outcome: {0, 2, 4, 6, 8}
- With other useful methods too:
var sequence = new int[] {0, 1, 2, 3, 4}.Intersperse(5);
// outcome: {0, 5, 1, 5, 2, 5, 3, 5, 4}
var element = sequence.Choice();
// will choose a random element
var sequence = new int[] { 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 from The Noun Project
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- FSharp.Core (= 4.7.0)
-
net6.0
- FSharp.Core (= 4.7.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on SharpX:
Package | Downloads |
---|---|
PickAll
.NET agile and extensible web searching API |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
6.4.6 | 140 | 6/1/2024 |
6.4.2 | 291 | 1/14/2024 |
6.3.6 | 195 | 12/18/2023 |
6.3.2 | 186 | 9/28/2023 |
6.3.0 | 182 | 8/20/2023 |
6.2.1 | 831 | 5/26/2023 |
6.2.0 | 162 | 5/26/2023 |
6.1.0 | 203 | 4/25/2023 |
6.0.3 | 224 | 4/20/2023 |
6.0.2 | 383 | 4/16/2023 |
6.0.1 | 210 | 4/15/2023 |
6.0.0-preview.1 | 390 | 3/29/2022 |
1.1.11 | 3,274 | 3/20/2022 |
1.1.10 | 454 | 3/20/2022 |
1.1.8 | 462 | 2/16/2022 |
1.1.5 | 1,866 | 1/16/2022 |
1.1.0 | 312 | 12/5/2021 |
1.0.5 | 358 | 11/28/2021 |
1.0.3 | 709 | 11/20/2021 |