Skip to content
Barış Aydek edited this page Jun 1, 2016 · 12 revisions

These functions represent behaviours on both IEnumerable<T> and Lst<T>.

Note all examples below expect you to include the following using statements. If you're not using C# 6 then some functions will need a Prelude. prefix:

    using LanguageExt;
    using static LanguageExt.Prelude;

Lst<T> is an immutable list type, and can be created in a few different ways.

    // Create a list of 5 integers
    var list = List(1,2,3,4,5);

    // These all create an empty list of integers
    var list1 = List<int>();
    var list2 = List.create<int>();
    var list3 = List.empty<int>();

The functions in the reference below are all in the static LanguageExt.List class. i.e.

   var list = List(1,2,3,4,5);
   var total = List.fold(list, 0, (s,x) => s + x);

There are fluent variants of most of the functions listed below. The fluent variants are PascalCase:

   var list = List(1,2,3,4,5);
   var total = list.Fold(0, (s,x) => s + x);
Function Behaviour
add Add an item to the list
addRange Add a range of items to the list
append Concatenate two lists (Concat in LINQ)
collect For each element of the list, applies the given function. Concatenates all the results and returns the combined list.
choose Applies the given function f to each element x of the list. Returns the list comprised of the results for each element where the function returns Some(f(x)).
exists Returns true if any item in the list matches the predicate provided
forall Returns true if all items in the list match a predicate (Any in LINQ)
filter Removes items from the list that do not match the given predicate (Where in LINQ)
find Returns Some(x) for the first item in the list that matches the predicate provided, None otherwise.
fold Applies a function f to each element of the collection, threading an accumulator argument through the computation. The fold function takes the state argument, and applies the function f to it and the first element of the list. Then, it feeds this result into the function f along with the second element, and so on. It returns the final result. (Aggregate in LINQ)
foldBack fold(s, rev(list), f) - Does a fold from the other end of the list.
freeze Turns an IEnumerable<T> into an immutable list Lst<T>
head Returns the first item in the list, if one exists, throws otherwise (First in LINQ)
headSafe Returns the Some(x) first item in the list, if one exists, None otherwise (FirstOrDefault in LINQ except instead of returning null it returns None)
init Generates a finite sequence of T using the provided delegate to initialise each item.
initInfinite Generates an infinite sequence of T using the provided delegate to initialise each item.
iter Invokes an action for each item in the list
length Returns the number of items in the list
map Projects the values in the list using a map function into a new list (Select in LINQ).
match Functional list pattern matching
reduce Applies a function to each element of the collection, threading an accumulator argument through the computation. This function first applies the function to the first two elements of the list. Then, it passes this result into the function along with the third element and so on. Finally, it returns the final result.
reduceBack reduce(rev(list),f)
remove Removes from the list the first item that matches the predicate provided
removeAt Removes an item from the list at a specified offset
repeat Generates a sequence that contains one repeated value
rev Reverses the list (Reverse in LINQ)
scan Applies a function to each element of the collection, threading an accumulator argument through the computation. This function takes the state argument, and applies the function to it and the first element of the list. Then, it passes this result into the function along with the second element, and so on. Finally, it returns the list of intermediate results and the final result.
scanBack scan(rev(list))
sum Returns the sum of all the items in the list (Sum in LINQ)
tail Returns the list but drops the first element (Skip(1) in LINQ)
takeWhile Iterate the list, yielding items if they match the predicate provided, and stopping as soon as one doesn't
unfold Generate a new list from an intial state value and an 'unfolding' function.
zip Joins two lists together either into a list of tuples or by using the join function provided
Clone this wiki locally