Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/uriegel/linqtools


https://github.com/uriegel/linqtools

Last synced: 8 days ago
JSON representation

Awesome Lists containing this project

README

        

# LinqTools

Extensions for LINQ

## Overview

* Extension functions to make Nullable a Monad/Functor
* Extension functions to make Async Function a Monad/Functor
* Ohter Tools usable for fluent syntax

## Option< T >

There are Extension Functions for Option to make them Monads/Functors:
* Select
* SelectMany

If you are not sure, if an object is null although the compiler says no, you can create a Nullable from an apparently not Nullable with
```
static T? AsNullable(this T t)

```
With the extension functions you can work with Options as if they were the containing type:
```
var res = (from n in teststr
from m in GetFirstStringChecked(n)
where m.Length == 9
from p in ThrowIfNull(m)
select p)
.GetOrDefault("Nothing");
```
As soon as a function returns None, all further processing is skipped.

## Result Monad/Functor

There is a Result Monad/Functor, containing either an Ok value or an Error value. You can transform
Results with LINQ queries like:

```
var test = from n in await Process.RunAsync("lsblk", "--bytes --output SIZE,NAME,LABEL,MOUNTPOINT,FSTYPE")
let driveLines = n.Split('\n', StringSplitOptions.RemoveEmptyEntries)
let titles = driveLines[0]
let positions = new[]
{
0,
titles.IndexOf("NAME"),
titles.IndexOf("LABEL"),
titles.IndexOf("MOUNT"),
titles.IndexOf("FSTYPE")
}
select driveLines
.Skip(1)
.Select(n => CreateRootItem(n, positions))
.ToArray();
```