-
Notifications
You must be signed in to change notification settings - Fork 15
Cons
"cons" is a common concept in functional programming languages, related to joining and splitting sets of data. In F#, for example, linked lists use the cons operator (::
) to add or remove items from the head of the list. Combining this with recursively processing the tail, the whole list can then be iterated over. Conversely, recursively adding items to the tail allows new lists to be built.
Rather than trying to replicate F#'s linked list type, Succinc<T> provides "cons" support for IEnumerable<T>
. This implementation offers two features not available to a basic IEnumerable<T>
implementation:
-
Cons
can be used to add/remove items from the head of the enumeration, without affecting the original data set. - The new enumeration can be enumerated many times, without re-enumerating the original data. This is achieved via caching the elements when fetched and that cache is used for subsequent enumerations.
The ConsExtensionsForIEnumerable
provides a set of extension methods for IEnumerable<T>
for creating "cons enumerables" from an IEnumerable<T>
implementation.
public static IConsEnumerable<T> ToConsEnumerable<T>(this IEnumerable<T> collection)
Returns an IConsEnumerable<T>
for the enumeration. This IConsEnumerable<T>
can be safely enumerated many times, without the need to re-enumerate the original data set. Unlike calling ToList()
, the enumeration is not immediately read however, so there is no overhead in performing the conversion.
public static IConsEnumerable<T> Cons<T>(this IEnumerable<T> collection, T head)
Returns an IConsEnumerable<T>
composed of head
as the first element and the collection
as subsequent elements.
public static IConsEnumerable<T> Cons<T>(this IEnumerable<T> collection, IEnumerable<T> head)
This is a diversion away from standard "cons" features, in that it allows an entire collection of items to be added to the front of another collection. It returns an IConsEnumerable<T>
composed of the head
elements, followed by the the collection
as subsequent elements.
A deconstruct has been added that splits the the cons enumeration into a head and tail "tuple". This feature only works with C# 7 or later.
Any cons enumeration offered by Succint<T> implements ``IConsEnumerable`. Those implementations offer the following methods:
IConsEnumerable<T> Cons(T head)
Returns an IConsEnumerable<T>
composed of head
as the first element and the current cons enumerable as subsequent elements.
IConsEnumerable<T> Cons(IEnumerable<T> head)
Returns an IConsEnumerable<T>
composed of the head
elements, followed by the the current cons enumerable as subsequent elements.
ConsResult<T> Cons()
Returns a ConsResult<T>
, which contains the head and tail parts of the collection
. See below for details of ConsResult<T>
.
ConsResult<T>
is returned by Cons
calls that split a collection into a head and tail. It has two properties:
If the collection isn't empty, then Head
contains the first element of the collection. If the collection is empty, it contains none
.
Contains the rest of the collection, or a guaranteed empty collection if the the collection prior to Cons
was already empty.
Action
/Func
conversionsCycle
methods- Converting between
Action
andFunc
- Extension methods for existing types that use
Option<T>
- Indexed enumerations
IEnumerable<T>
cons- Option-based parsers
- Partial function applications
- Pattern matching
- Pipe Operators
- Typed lambdas
Any
Either<TLeft,TRight>
None
Option<T>
Success<T>
Union<T1,T2>
Union<T1,T2,T3>
Union<T1,T2,T3,T4>
Unit
ValueOrError