{"id":18732498,"url":"https://github.com/gsscoder/sharpx","last_synced_at":"2025-04-12T18:31:15.167Z","repository":{"id":144106967,"uuid":"426535290","full_name":"gsscoder/sharpx","owner":"gsscoder","description":".NET functional programming and other utilities","archived":false,"fork":false,"pushed_at":"2025-04-06T08:36:13.000Z","size":355,"stargazers_count":30,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-06T08:45:31.517Z","etag":null,"topics":["csharp","dotnet","error-handling","functional","library","railway-oriented-programming","string-manipulation","testing"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gsscoder.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-11-10T08:05:45.000Z","updated_at":"2025-04-06T08:36:16.000Z","dependencies_parsed_at":"2023-12-28T13:26:49.829Z","dependency_job_id":"da110ea0-f256-4ca9-aaaa-d3699727dfa5","html_url":"https://github.com/gsscoder/sharpx","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsscoder%2Fsharpx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsscoder%2Fsharpx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsscoder%2Fsharpx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsscoder%2Fsharpx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gsscoder","download_url":"https://codeload.github.com/gsscoder/sharpx/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248613325,"owners_count":21133492,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnet","error-handling","functional","library","railway-oriented-programming","string-manipulation","testing"],"created_at":"2024-11-07T15:06:26.551Z","updated_at":"2025-04-12T18:31:15.153Z","avatar_url":"https://github.com/gsscoder.png","language":"C#","readme":"# SharpX\n\n![SharpX Logo](/assets/icon.png)\n\nSharpX is derived from [CSharpx](https://github.com/gsscoder/csharpx) 2.8.0-rc.2 (_which was practically a stable_) and [RailwaySharp](https://github.com/gsscoder/railwaysharp) 1.2.2.\n\n![CSharpX Downloads](https://img.shields.io/nuget/dt/CSharpX.svg)\n\nWhile both projects were meant mainly for source inclusion, SharpX is designed to be pulled from [NuGet](https://www.nuget.org/).\n\nThe library contains **functional types** and other utilities, including **test oriented tools**. It follows the _don't reinvent the wheel_ philosophy. This project was originally inspired by [Real-World Functional Programming](https://www.amazon.com/Real-World-Functional-Programming-Tomas-Petricek/dp/1933988924/ref=sr_1_1?keywords=Real-World+Functional+Programming\u0026qid=1580118924\u0026s=books\u0026sr=1-1) and includes code from [MoreLINQ](https://github.com/morelinq/MoreLINQ).\n\n![SharpX Downloads](https://img.shields.io/nuget/dt/SharpX.svg)\n\n## Targets\n\n- .NET Standard 2.1\n- .NET 8.0\n\n## Install via NuGet\n\nYou can install it via NuGet:\n\n```sh\n$ dotnet add package SharpX --version 8.3.6\n  Determining projects to restore...\n  ...\n```\n\n## Overview\n\nAll types are available in the root namespace. Extension methods (except the very specific ones) are in `SharpX.Extensions`.\n`SharpX.FsCheck` contains [FsCheck](https://github.com/fscheck/FsCheck) generators for property-based testing.\n\n## Maybe\n\n- Encapsulates an optional value that can contain a value or being empty.\n- Similar to F# `'T option` / Haskell `data Maybe a = Just a | Nothing` type.\n\n```csharp\nvar greet = true;\nvar value = greet ? \"world\".ToMaybe() : Maybe.Nothing\u003cstring\u003e();\nvalue.Match(\n    who =\u003e Console.WriteLine($\"hello {who}!\"),\n    () =\u003e Environment.Exit(1));\n```\n\n- Supports LINQ syntax:\n\n```csharp\nvar result1 = (30).ToJust();\nvar result2 = (10).ToJust();\nvar result3 = (2).ToJust();\n\nvar sum = from r1 in result1\n          from r2 in result2\n          where r1 \u003e 0\n          select r1 - r2 into temp\n          from r3 in result3\n          select temp * r3;\n\nvar value = sum.FromJust(); // outcome: 40\n```\n\n- Features sequence extensions:\n\n```csharp\nvar maybeFirst = new int[] {0, 1, 2}.FirstOrNothing(x =\u003e x == 1)\n// outcome: Just(1)\n```\n\n## Either\n\n- Represents a value that can contain either a value or an error.\n- Similar to Haskell `data Either a b = Left a | Right b` type.\n- Similar also to F# `Choice\u003c'T, 'U\u003e`.\n- Like in Haskell the convention is to let `Right` case hold the value and `Left` keep track of error or similar data.\n- If you want a more complete implementation of this kind of types, consider using `Result`.\n\n## Result\n\nThis type was originally present in RailwaySharp. Check the test project to see a more complete usage example.\n\n``` csharp\npublic static Result\u003cRequest, string\u003e ValidateInput(Request input)\n{\n    if (input.Name == string.Empty) {\n        return Result\u003cRequest, string\u003e.FailWith(\"Name must not be blank\");\n    }\n    if (input.EMail == string.Empty) {\n        return Result\u003cRequest, string\u003e.FailWith(\"Email must not be blank\");\n    }\n    return Result\u003cRequest, string\u003e.Succeed(input);\n}\n\nvar request = new Request { Name = \"Giacomo\", EMail = \"gsscoder@gmail.com\" };\nvar result = Validation.ValidateInput(request);\nresult.Match(\n    (x, msgs) =\u003e { Logic.SendMail(x.EMail); },\n    msgs =\u003e { Logic.HandleFailure(msgs) });\n```\n\n## Unit\n\n- `Unit` is similar to `void` 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 **Haskell** `Unit`.\n\n```csharp\n// prints each word and returns 0 to the shell\nstatic int Main(string[] args)\n{\n    var sentence = \"this is a sentence\";\n    return (from _ in\n            from word in sentence.Split()\n            select Unit.Do(() =\u003e Console.WriteLine(word))\n            select 0).Distinct().Single();\n}\n```\n\n## FSharpResultExtensions\n\n- Convenient extension methods to consume `FSharpResult\u003cT, TError\u003e` in simple and functional for other **.NET** languages.\n\n```csharp\n// pattern match like\nvar result = Query.GetStockQuote(\"ORCL\");\nresult.Match(\n    quote =\u003e Console.WriteLine($\"Price: {quote.Price}\"),\n    error =\u003e Console.WriteLine($\"Trouble: {error}\"));\n// mapping\nvar result = Query.GetIndex(\".DJI\");\nresult.Map(\n    quote =\u003e CurrencyConverter.Change(quote.Price, \"$\", \"€\"));\n```\n\n- Blog [post](https://gsscoder.github.io/consuming-fsharp-results-in-c/) about it.\n\n## StringExtensions\n\n- General purpose and randomness string manipulation extensions. Few more methods and non-extension version can be found in `Strings` class.\n\n```csharp\nConsole.WriteLine(\n    \"\\t[hello\\world@\\t\".Sanitize(normalizeWhiteSpace: true));\n// outcome: ' hello world '\n\nConsole.WriteLine(\n    \"I want to change a word\".ApplyAt(4, word =\u003e word.Mangle()));\n// outcome like: 'I want to change \u0026a word'\n```\n\n## EnumerableExtensions\n\n- Most useful extension methods from [MoreLINQ](https://github.com/morelinq/MoreLINQ).\n- Some of these reimplemnted (e.g. `Choose` using `Maybe`):\n\n```csharp\nvar numbers = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};\nvar evens = numbers.Choose(x =\u003e x % 2 == 0\n                                ? x.ToJust()\n                                : Maybe.Nothing\u003cint\u003e());\n// outcome: {0, 2, 4, 6, 8}\n```\n\n- With other useful methods too:\n\n```CSharp\nvar sequence = new int[] {0, 1, 2, 3, 4}.Intersperse(5);\n// outcome: {0, 5, 1, 5, 2, 5, 3, 5, 4}\nvar element = sequence.Choice();\n// will choose a random element\nvar sequence = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }.ChunkBySize(3);\n// outcome: { [0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10] }\n```\n\n## Icon\n\n[Tool](https://thenounproject.com/search/?q=tool\u0026i=3902696) icon designed by Cattaleeya Thongsriphong from [The Noun Project](https://thenounproject.com/)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgsscoder%2Fsharpx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgsscoder%2Fsharpx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgsscoder%2Fsharpx/lists"}