C# pure functional programming framework - come and get declarative!
C# pure functional programming framework - come and get declarative!
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. Author on...
| Nu-get package | Description |
|---|---|
| LanguageExt.Parsec | Port of the Haskell parsec library |
| LanguageExt.Streaming | A set of compositional streaming types |
| 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 | Port of the Haskell parsec library |
| LanguageExt.Rx | Reactive Extensions support for various types within the Core |
| LanguageExt.Sys | Provides an effects wrapper around the .NET System namespace making common IO operations pure and unit-testable |
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:
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:
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.
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.
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:
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:
PascalCase in the normal waynew. They will always
be PascalCase: Option<int> x = Some(123);
Option<int> y = None;
Seq<int> items = Seq(1,2,3,4,5);
List<int> items = List(1,2,3,4,5);
HashMap<int, string> dict = HashMap((1, "Hello"), (2, "World"));
Map<int, string> dict = Map((1, "Hello"), (2, "World"));using static LanguageExt.Prelude are camelCase. var x = map(opt, v => v * 2);PascalCase in the normal way 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.
| Location | Feature | Description |
|---|---|---|
Core |
IO<A> |
A synchronous and asynchronous side-effect: an IO monad |
Core |
Eff<A> |
A synchronous and asynchronous side-effect with error handling |
Core |
Eff<RT, A> |
Same as Eff<A> but with an injectable runtime for dependency-injection: a unit testable IO monad |
| Location | Feature | Description |
|---|---|---|
Core |
Atom<A> |
A lock-free atomically mutable reference for working with shared state |
Core |
Ref<A> |
An atomic reference to be used in the transactional memory system |
Core |
AtomHashMap<K, V> |
An immutable HashMap with a lock-free atomically mutable reference |
Core |
AtomSeq<A> |
An immutable Seq with a lock-free atomically mutable reference |
Core |
VectorClock<A> |
Understand distributed causality |
Core |
VersionVector<A> |
A vector clock with some versioned data |
Core |
VersionHashMap <ConflictV, K, V> |
Distrubuted atomic versioning of keys in a hash-map |
| Location | Feature | Description |
|---|---|---|
Core |
Arr<A> |
Immutable array |
Core |
Seq<A> |
Lazy immutable list, evaluate at-most-once - very, very fast! |
Core |
Iterable<A> |
[Wrapper around `IEn |
yieldAll with source breaks consumer IO
Idea: ExpressionLens<A, B>
LanguageExt.QueryExtensions.Distinct<T> conflicts with IQueryable.Distinct
MultiListenerPureSourceT doesn't cleanup internal listeners upon repeated invocation of ReduceInternalM
Proposal: add a TakeUntil for Seq
Set repo url in Nuget package
Issues in the blog
Idea: Use Source Generators to rewrite LINQ expressions
Prelude timeout does not use the HasTime<RT> runtime trait
`Atom().Change` fires always