UnityUtilities
UnityUtilities.LINQExtensions Class Reference

A collection of extension methods for IEnumerable<T>, List<T> and arrays. More...

Static Public Member Functions

static TElement FirstByGeneratedValue< TElement, TValue > (this IEnumerable< TElement > list, Func< TElement, TValue > valueConverter, Predicate< TValue > predicateValue=null)
 Takes a collection, generates values from the items and and returns the item with the lowest generated value. Useful for example to find the closest item. Reverse the generated values to find the item with the highest generated value. More...
 
static TElement Nearest< TElement > (this IEnumerable< TElement > list, Vector3 referencePoint, float minDistance=0, float maxDistance=float.PositiveInfinity)
 Returns the component nearest to the referencePoint and in minimum/maximum range. More...
 
static void Shuffle< T > (this T[] list)
 Shuffles an array in place. More...
 
static void Shuffle< T > (this List< T > list)
 Shuffles a list in place. More...
 
static T RandomElement< T > (this T[] array)
 Returns a random element from the array. More...
 
static T RandomElement< T > (this List< T > list)
 Returns a random element from the list. More...
 
static string ToOneLineString< T > (this IEnumerable< T > list, string separator=", ", string encapsulate="\)
 Calls ToString() on every element of the list, puts [encapsulate] directly before and after the result and then concatenates the results with [seperator] between them. More...
 

Detailed Description

A collection of extension methods for IEnumerable<T>, List<T> and arrays.

Member Function Documentation

§ FirstByGeneratedValue< TElement, TValue >()

static TElement UnityUtilities.LINQExtensions.FirstByGeneratedValue< TElement, TValue > ( this IEnumerable< TElement >  list,
Func< TElement, TValue >  valueConverter,
Predicate< TValue >  predicateValue = null 
)
static

Takes a collection, generates values from the items and and returns the item with the lowest generated value. Useful for example to find the closest item. Reverse the generated values to find the item with the highest generated value.

This returns the same as list.Where(element => predicateValue(valueConverter(element))).OrderBy(valueConverter).First(), but it a) doesn't order the whole list and b) doesn't call valueConverted more than once per element.

Template Parameters
TElementThe collection element type.
TValueThe generated value type.
Parameters
listThe list of elements.
valueConverterThe method to convert an element to a generated value used for ordering.
predicateValueA predicate testing whether the generated value is permitted. If true, the element is used; if false, the element is skipped.
Returns
The first element by the generated value in order that succeeded the predicateValue test.
Type Constraints
TValue :IComparable 

§ Nearest< TElement >()

static TElement UnityUtilities.LINQExtensions.Nearest< TElement > ( this IEnumerable< TElement >  list,
Vector3  referencePoint,
float  minDistance = 0,
float  maxDistance = float.PositiveInfinity 
)
static

Returns the component nearest to the referencePoint and in minimum/maximum range.

Template Parameters
TElementThe collection element type. Needs to be a subclass of Component.
Parameters
listThe list of components.
referencePointA reference point to get the distance from.
minDistanceOptional: The minimum distance.
maxDistanceOptional: The maximum distance.
Returns
The element nearest to the referencePoint and in minimum/maximum range.
Type Constraints
TElement :Component 

§ RandomElement< T >() [1/2]

static T UnityUtilities.LINQExtensions.RandomElement< T > ( this T []  array)
static

Returns a random element from the array.

Template Parameters
TThe array element type.
Parameters
arrayThe array to return an element from.
Returns
A random element from the array.

§ RandomElement< T >() [2/2]

static T UnityUtilities.LINQExtensions.RandomElement< T > ( this List< T >  list)
static

Returns a random element from the list.

Template Parameters
TThe list element type.
Parameters
listThe list to return an element from.
Returns
A random element from the list.

§ Shuffle< T >() [1/2]

static void UnityUtilities.LINQExtensions.Shuffle< T > ( this T []  list)
static

Shuffles an array in place.

Template Parameters
TThe array element type.
Parameters
listThe array to shuffle.

§ Shuffle< T >() [2/2]

static void UnityUtilities.LINQExtensions.Shuffle< T > ( this List< T >  list)
static

Shuffles a list in place.

Template Parameters
TThe list element type.
Parameters
listThe list to shuffle.

§ ToOneLineString< T >()

static string UnityUtilities.LINQExtensions.ToOneLineString< T > ( this IEnumerable< T >  list,
string  separator = ", ",
string  encapsulate = "\"" 
)
static

Calls ToString() on every element of the list, puts [encapsulate] directly before and after the result and then concatenates the results with [seperator] between them.

Template Parameters
TThe collection element type.
Parameters
listThe collection to concatenate.
separatorThe seperator between entries.
encapsulateThe string to put directly before and after every item.
Returns
A string containing the ToString() results of all items.

The documentation for this class was generated from the following file: