Skip to content
This repository was archived by the owner on Nov 13, 2018. It is now read-only.

Methods

Jordan Kniest edited this page Jun 25, 2017 · 13 revisions

Here you can find explanations and examples for all collection methods.


Reduce

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: 60

All

Return 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: 10

Avg

Return 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: 30

Median

Return 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: 45

Mode

Return 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]

Diff

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

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]

Each

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
// 36

Filter

Filter 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]

When

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]

First

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: 20

Implode

Glue 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

Clone this wiki locally