Re: Gee max/min for Collection?



On Sat, 2011-08-20 at 01:27 +0800, Just Fill Bugs wrote:
> Should there be max/min methods or properties for collections?

For SortedSet/SortedMap you have .first()/.last() methods which gives
you the maximum/minimum element.

For lists and similar data structures you can implement it by folds:

Collection<int> list = new List<int>(...);
...
int? max = list.fold<int?>((a, b) => {
	return b == null? a : int.max(a, b);
}, null);

Alternatively:
int? max = null;
list.foreach ((a) => {
	max = max == null ? a : int.max(a, max);
});

// max is null if list is empty or contains maximum

Regards

Attachment: signature.asc
Description: This is a digitally signed message part



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]