Repository navigation
Methods
Here you can find explanations and examples for all collection methods.
The reduce method converts a collection into some other type. For example it could be used to sum the items (converting the collection to a single integer)
Structure:
public T Reduce<T>(ReduceCallback<T> callable, T initial);Example:
// Create a new collection with three integer values (10, 20, 30)
var collection = new Collection<int>(10, 20, 30);
// Reduce this collection to a single integer value
var result = collection.Reduce((value, item) => {
return value + item;
}, 0);
// Print the result to the console
Console.Write(result); // The result is: 60Return all items as an array. It is in fact an alias for the 'ToArray' method.
Structure:
public TList[] All();Example:
// Create a new collection with three integer values (10, 20, 30)
var collection = new Collection<int>(10, 20, 30);
// Request all items
var result = collection.All();
// Print the first item to the console
Console.Write(result[0]); // The result is: 10Return the average value based on the return value of the callable.
Structure:
public int Avg(IntItemCallback callable);Example:
// Create a new collection with three persons (the first parameters is their age)
var collection = new Collection<int>(new Person(10), new Person(45), new Person(36));
// Calculate the average age
var result = collection.Avg(person => person.Age);
// Print the result to the console
Console.Write(result); // The result is: 30Return the median value based on the return value of the callable.
Structure:
public int Median(IntItemCallback callable);Example:
// Create a new collection with three persons (the first parameters is their age)
var collection = new Collection<int>(new Person(10), new Person(45), new Person(36));
// Calculate the median age
var result = collection.Median(person => person.Age);
// Print the result to the console
Console.Write(result); // The result is: 45Return the mode value(s) of the collection.
Structure:
public Collection<TList> Mode();Example:
// Create a new collection with six integers
var collection = new Collection<int>(1, 1, 4, 1, 3, 3);
// Calculate the mode value
var result = collection.Mode();
// Print the result to the console
Console.Write(result); // The result is: [1]Compare a collection against another one. It will return all values in the original collection that are not present in the other collection.
Structure:
public Collection<TList> Diff(IList<TList> other);Example:
// Create a new collection with three integers
var collection = new Collection<int>(10, 20, 30);
// Create another collection with three integers
var other = new Collection<int>(10, 30, 40);
// Calculate the diff collection
var result = collection.Diff(other);
// Print the result to the console
Console.Write(result); // The result is: [20]Dump the whole collection to the console
Structure:
public Collection<TList> Dump();Example:
// Create a new collection with three integers
var collection = new Collection<int>(10, 20, 30);
// Dump the collection
collection.Dump(); // The output is: Collection<System.Int32> (3) [10, 20, 30]Iterate through each item and perform some action
Structure:
public Collection<TList> Each(VoidItemCallback callable);Example:
// Create a new collection with three persons (the first parameters is their age)
var collection = new Collection<Person>(new Person(10), new Person(45), new Person(36));
// Iterate trough each item and print out the age
collection.Each(person => Console.WriteLine(person.Age));
// The console output is:
// 10
// 45
// 36Filter through a collection and return only the items that are passing a callable.
Structure:
public Collection<TList> Filter(BoolItemCallback callable);Example:
// Create a new collection with three integers
var collection = new Collection<int>(10, 20, 30);
// Filter trough each item (each item must be greater than 15)
var result = collection.Filter(item => item >= 15);
// Print the result to the console
Console.Write(result); // The output is: [20, 30]Run a given callable only when the first argument is true.
Structure:
public Collection<TList> When(bool shouldExecute, CollectionCallback callable);Example:
// Create a new collection with three integers
var collection = new Collection<int>(10, 20, 30);
// Add another integer if the first argument is true
var result = collection.When(true, col => {
col.Add(40);
return col;
});
// Print the result to the console
Console.Write(result); // The output is: [10, 20, 30, 40]Return the first item of a collection. Optional you can pass a callable to filter the collection first.
Structure:
public TList First(BoolCallback callable = null);Example:
// Create a new collection with three integers
var collection = new Collection<int>(10, 20, 30);
// Fetch the first item that is bigger than 15
var result = collection.First(item => item >= 15);
// Print the result to the console
Console.Write(result); // The output is: 20Glue all items of a collection together as a big string (with a specific glue string)
Structure:
public string Implode(string glue = ", ");Example:
// Create a new collection with three integers
var collection = new Collection<int>(10, 20, 30);
// Implode all items
var result = collection.Implode(" - ");
// Print the result to the console
Console.Write(result); // The output is: 10 - 20 - 30