InCube Core
InCube Core is a set of C# libraries that provides extensions for functional
programming, standard collection types, value parsing and formatting, and more.
Consult the wiki for additional resources.
Adding InCube Core to your build
Binary packages of InCube Core are available on nuget.org. Add the following lines to your .csproj
in order to use the library in your .Net Core build:
<ItemGroup>
<PackageReference Include="InCube.Core" Version="1.0.0" />
</ItemGroup>
Functional Programming
The Functional package of InCube Core provides read-only wrapper types in C#
which are common in other functional programming languages.
All of the data types above implement the IEnumerable<T>
interface. They can be
viewed as a container which holds at most one valid element t
of type T
.
InCube Core provides container-like extension methods for all these data types
as well as for Nullable<T>
.
Property / Method |
Description |
None |
Create an empty wrapper. |
Some |
Create a wrapper holding a valid element t . |
HasValue |
Indicates whether a wrapper holds a valid element t . |
Value |
Returns either t or throws an InvalidOperationException . |
GetValueOrDefault |
Returns either t or a default value. |
GetValueOr |
Returns either t or produces a new value by calling a function delegate. The delegate may be used to throw a more meaningful exception than by accessing Value directly. |
Select |
Apply a mapping function to t . |
SelectMany |
Apply a mapping function to t that returns a wrapper type and flatten (corresponds to flatMap ). |
ForEach |
Consume t in an Action . |
Match |
Either apply a mapping function to t or produce a default element calling a function delegate. |
Where |
Remove t from the container unless it satisfies some predicate. |
Any |
Indicates that t exists and satisfies some predicate. |
All |
Indicates that t does not exist or satisfies some predicate. |
OrElse |
Fall back to another wrapper if the container is empty. |
Collections
The Collections package of InCube Core provides LINQ type extension methods for many standard containers.
Method |
Description |
MkString |
Format an IEnumerable as a string. |
ForEach |
Apply an action to all elements of an IEnumerable . |
MaxBy , MinBy |
Search for the maximum / minimum of an IEnumerable by a selection key. |
ArgMax , ArgMin |
Find the index of the maximum / minimum in an IEnumerable . |
Split |
Separate an IEnumerable into two parts by means of a predicate. |
ToEnumerable , Iterate , Generate |
Produce a new IEnumerable . |
Scan |
Generate a new sequence by mapping an IEnumerable with state. This is essentially a mix of Select and Aggregate . |
FirstOption , SingleOption |
Access the first or single element of this IEnumerable as an Option<T> . |
Flatten |
Join an IEnumerable of containers into a single sequence. |
Extensions for Lists
Method |
Description |
Empty |
Create an empty read-only dictionary. |
GetOrDefault |
Return a default value if a key is not found. |
GetOrThrow |
Throw a meaningful exception if a key is not found. |
GetOption , GetMaybe , GetNullable |
Return an empty wrapper type if a key is not found. |
ToDictionary |
Convert an IEnumerable of tuples to a Dictionary . |
AsReadOnlyDictionary |
Efficiently convert any IDictionary into an IReadOnlyDictionary . |
AsSorted |
Convert any IDictionary into a SortedDictionary |
Extensions for Tuples
Method |
Description |
MakePair , MakeTuple , MakeValueTuple |
Factories for KeyValuePair , Tuple , and ValueTuple which help inferring type arguments. |
ZipAsTuple |
Combine multiple sequences into a single sequence of tuples. |
Unzip |
Split sequences of tuples into their components. |
ZipWithIndex |
Zip a sequence with the index of each element. |
ZipI |
A variant of Zip which passes the index of each element as third argument to the function delegate. |
Zip3 , Zip4 |
Variants of Zip for 3 and 4 input sequences, respectively. |
TupleSelect |
A variant of Select specialized for mapping tuples. |
Keys , Values |
Select the keys or values in a sequence of key-value pairs, respectively. |
MapValues |
Apply a function delegate to the values in a sequence of key-value pairs. |
AsTuple , AsKeyValuePair |
Convert between sequences of ValueTuple s and KeyValuePair s. |
Numerical Extensions and Statistics
Type / Method |
Description |
Histogram<T> , MakeHistogram |
Compute a histogram from a collection of comparable elements. |
Rank , VectorRank |
Compute the rank of one or multiple elements in a collection. |
Range<T> |
Represents a closed interval and provides typical operations like overlap and intersection. |
Utility Classes
Links