FutureMUD Collection Extension Reference

These functions are accessed by doing something in the following form after a collection variable: CollectionVariable.FunctionName(ItemVariableName, InnerFunction) Where: CollectionVariable is any variable or function returning a collection FunctionName is the specific collection extension function you want to run (e.g. Any, Sum, etc) ItemVariableName is a variable name that will be used inside the inner function to refer to each item in the collection InnerFunction is a function (usually returning a Boolean or Number) that is run on each element in the collection For example, if you had a Number Collection called fibonacci that contained the following items: 1, 1, 2, 3, 5, 8, 13 You could run: @fibonacci.Sum(number, @number) And the result would be a number with the value of 33.

ALL

The ALL function runs the supplied inner function (which itself returns a boolean value) over all elements of a collection, and returns true if all the items return true. Otherwise, returns false. Also returns true if there are no elements.

ANY

The ANY function runs the supplied inner function (which itself returns a boolean value) over all elements of a collection, and returns true if any of the items return true. Otherwise, returns false. Returns false if there are no elements.

AVERAGE

The AVERAGE function runs the inner function (which itself returns a number) over all elements of the collection, and then returns the numerical average (mean) of the results. Will return 0 if the collection is empty. Note: If the collection itself is a collection of numbers, you can use the pattern .Average(x, @x)

COUNT

The COUNT function runs the supplied inner function (which itself returns a boolean value) over all elements of a collection, and counts the total number of elements that return true. If you just want to take the count of the number of elements in a collection, use the .Count property instead of this function.

FIND

The FIND function accepts an inner function (which must return a boolean) and returns the index position of the first item in the collection that is TRUE for the inner function. If it finds no matches, it will return -1. For example if you had a CHARACTER COLLECTION, .Find(x, @x.Age > 50) would return the index (position in collection starting at zero) of the first character over the age of 50.

FIRST

The FIRST function runs the inner function (which must return a boolean) over the collection and returns the first element from that collection that matches the criteria. If it doesn't find anything, it returns NULL, so you must protect against this by checking ISNULL or using IFNULL. For example, if you had a CHARACTER COLLECTION and you ran .First(x, @x.Age > 35) it would either return the first character in the collection who was aged over 35 years old, or it would return null if nothing was found.

MAX

The MAX function runs the inner function (which itself returns a number) over all elements of the collection, and then returns the maximum value of the results. Will return 0 if the collection is empty. Note: If the collection itself is a collection of numbers, you can use the pattern .Max(x, @x)

MIN

The MIN function runs the inner function (which itself returns a number) over all elements of the collection, and then returns the minimum value of the results. Will return 0 if the collection is empty. Note: If the collection itself is a collection of numbers, you can use the pattern .Min(x, @x)

SELECT

The SELECT function is used to transform a collection of one type of thing into a collection of something else. It will run the inner function over each item in the collection and return a collection of those items. For example, if you have a CHARACTER COLLECTION and you run .Select(x, @x.Name) over the collection, it will instead be a TEXT COLLECTION containing all the character names.

SELECTMANY

The SELECTMANY function is used when you have a collection of things that themselves contain another collection, and you want to aggregate them all together. For example, if you have a ROOM COLLECTION and you want to get all of the characters in all of those rooms, you could do .SelectMany(x, @x.Characters) and you would have a CHARACTER COLLECTION with all these characters.

STDDEV

The STDDEV function runs the inner function (which itself returns a number) over all elements of the collection, and then returns the standard deviation of the results. Will return 0 if the collection is empty. Note: If the collection itself is a collection of numbers, you can use the pattern .StdDev(x, @x)

SUM

The SUM function runs the inner function (which itself returns a number) over all elements of the collection, and then returns the sum of the results. Will return 0 if the collection is empty. Note: If the collection itself is a collection of numbers, you can use the pattern .Sum(x, @x)

WHERE

The WHERE function runs the inner function (which must return a boolean) over all items in the collection, and returns a new collection that only contain whichever items were TRUE from the inner function. For example, if you had a CHARACTER COLLECTION, you could run .Where(x, @x.Age >= 21) to get all characters who are 21 years or older.