## File: README.md
C# Functional Programming Language Extensions
=============================================
This library uses and abuses the features of C# to provide a pure functional-programming framework that, if you squint, can look like
extensions to the language itself. The desire here is to make programming in C# much more robust by helping the engineer's inertia flow
in the direction of declarative and pure functional code rather than imperative. Using these techniques for large code-bases can bring
tangible benefits to long-term maintenance by removing hidden complexity and by easing the engineer's and team's cognitive load.
[](https://github.com/louthy/language-ext/discussions)
__Author on...__
* __Blog__: [Notes from a Small Functional Island](https://paullouth.com/)
* __Bluesky__: [@paullouth.bsky.social](https://bsky.app/profile/paullouth.bsky.social)
* __Mastodon:__ [@louthy@4four.org](https://4four.org/@louthy)
* __Github ReadME project__: ['Functional programming is finally going mainstream'](https://github.com/readme/featured/functional-programming)
## Contents
* [Reference](#reference)
* [Nu-get package](#nu-get)
* [Getting started](#getting-started)
* [Prologue](#prologue)
* [**Features**](#features)
* [Functional effects and IO](#functional-effects-and-io)
* [Atomic concurrency, shared state, and collections](#atomic-concurrency-and-collections)
* [Immutable collections](#immutable-collections)
* [Functional streams](#functional-streams)
* [Optional and Alternative value monads](#optional-and-alternative-value-monads)
* [State managing monads](#state-managing-monads)
* [Parser combinators](#parser-combinators)
* [Pretty: Produce nicely formatted text with smart layouts](#pretty)
* [Differencing](#differencing)
* [Traits](#traits)
* [Value traits](#value-traits)
* [Contributing & Code of Conduct](#contributing--code-of-conduct)
## Reference
* [API Reference](https://louthy.github.io/language-ext/)
* [Issues that contain documentation and examples](https://github.com/louthy/language-ext/issues?utf8=%E2%9C%93&q=is%3Aissue%20label%3A%22examples%20%2F%20documentation%22%20)
## Nu-get
Nu-get package | Description
---------------|-------------
[LanguageExt.Parsec](https://www.nuget.org/packages/LanguageExt.Parsec) | Port of the [Haskell parsec library](https://hackage.haskell.org/package/parsec)
[LanguageExt.Streaming](https://www.nuget.org/packages/LanguageExt.Streaming) | A set of compositional streaming types
[LanguageExt.FSharp](https://www.nuget.org/packages/LanguageExt.FSharp) | F# to C# interop package. Provides interop between the LanguageExt.Core types (like `Option`, `List` and `Map`) to the F# equivalents, as well as interop between core BCL types and F#
[LanguageExt.Parsec](https://www.nuget.org/packages/LanguageExt.Parsec) | Port of the [Haskell parsec library](https://hackage.haskell.org/package/parsec)
[LanguageExt.Rx](https://www.nuget.org/packages/LanguageExt.Rx) | Reactive Extensions support for various types within the Core
[LanguageExt.Sys](https://www.nuget.org/packages/LanguageExt.Sys) | Provides an effects wrapper around the .NET System namespace making common IO operations pure and unit-testable
## Getting started
To use this library, simply include `LanguageExt.Core.dll` in your project or grab it from NuGet. It is also worth setting up some `global using` for your project. This is the full list that will cover all functionality and bring it into scope:
```C#
global using LanguageExt;
global using LanguageExt.Common;
global using LanguageExt.Traits;
global using LanguageExt.Effects;
global using LanguageExt.Streaming;
global using LanguageExt.Pretty;
global using LanguageExt.Traits.Domain;
global using static LanguageExt.Prelude;
```
A minimum, might be:
```c#
global using LanguageExt;
global using static LanguageExt.Prelude;
```
The namespace `LanguageExt` contains most of the core types; `LanguageExt.Prelude` contains the functions that bring into scope the prelude functions that behave like standalone functions in ML style functional programming languages; `LanguageExt.Traits` brings in the higher-kinded trait-types and many extensions; `LanguageExt.Common` brings in the `Error` type and predefined `Errors`.
## Prologue
From C# 6 onwards we got the ability to treat static classes like namespaces. This means that we can use static
methods without qualifying them first. That instantly gives us access to single term method names that look exactly like functions
in ML-style functional languages. i.e.
```C#
using static System.Console;
WriteLine("Hello, World");
```
This library tries to bring some of the functional world into C#. It won't always sit well with the seasoned C# OO programmer,
especially the choice of `camelCase` names for a lot of functions and the seeming 'globalness' of a lot of the library.
I can understand that much of this library is non-idiomatic, but when you think of the journey C# has been on, is "idiomatic"
necessarily right? A lot of C#'s idioms are inherited from Java and C# 1.0. Since then we've had generics, closures, Func, LINQ,
async... C# as a language is becoming more and more like a functional language on every release. In fact, the bulk of the new
features are either inspired by or directly taken from features in functional languages. So perhaps it's time to move the C#
idioms closer to the functional world's idioms?
My goal with this library is very much to create a whole new community within the larger C# community. This community is not
constrained by the dogma of the past or by the norms of C#. It understands that the OOP approach to programming has some problems
and tries to address them head-on.
And for those that say "just use F#" or "just use Haskell", sure, go do that. But it's important to remember that C# has a lot
going for it:
* Incredible investment into a state-of-the art compiler
* Incredible tooling (Visual Studio and Rider)
* A large ecosystem of open-source libraries
* A large community of developers already using it
* This is also very important for companies that hire engineers
* It _is_ a functional programming language! It has first-class functions, lambdas, etc.
* And with this library it has a functional-first _Base Class Library_
### A note about naming
One of the areas that's likely to get seasoned C# heads worked up is my choice of naming style. The intent is to try and make
something that _feels_ like a functional language rather than following rules of naming conventions (mostly set out by
the BCL).
There is, however, a naming guide that will keep you in good stead while reading through this documentation:
* Type names are `PascalCase` in the normal way
* The types all have constructor functions rather than public constructors that you instantiate with `new`. They will always
be `PascalCase`:
```C#
Option x = Some(123);
Option y = None;
Seq items = Seq(1,2,3,4,5);
List items = List(1,2,3,4,5);
HashMap dict = HashMap((1, "Hello"), (2, "World"));
Map dict = Map((1, "Hello"), (2, "World"));
```
* Any (non-type constructor) static function that can be used on its own by `using static LanguageExt.Prelude` are `camelCase`.
```C#
var x = map(opt, v => v * 2);
```
* Any extension methods, or anything "fluent" are `PascalCase` in the normal way
```C#
var x = opt.Map(v => v * 2);
```
Even if you disagree with this non-idiomatic approach, all of the `camelCase` static functions have fluent variants, so you never actually have to see the non-standard stuff.
## Features
### [Functional effects and IO](https://louthy.github.io/language-ext/LanguageExt.Core/Effects/index.html)
| Location | Feature | Description |
|----------|--------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `Core` | `IO` | [A synchronous and asynchronous side-effect: an IO monad](https://louthy.github.io/language-ext/LanguageExt.Core/Effects/IO/index.html) |
| `Core` | `Eff` | [A synchronous and asynchronous side-effect with error handling](https://louthy.github.io/language-ext/LanguageExt.Core/Effects/Eff/Eff%20no%20runtime/index.html) |
| `Core` | `Eff