/** This implements a "groupBy" function. The function is applied to each member of list, returning a value y for each. The results are grouped in a dictionary where the key is the value produced by the function and the value is an array of items from the list that returned that value y, in the order they were encountered in the list. */ groupBy[list, func] := { result = new dict for item = list result.addToList[func[item], item] return result } // println[groupBy[1 to 15, {|x| x mod 3}]]