Today I had to implement an algorithm to find the minimum and maximum values for a multidimensional array. The domain variable holds an array containing [min, max].
Behold the functional goodness of java-/coffeescript:
data = [ { name: 'series 1' values: [5, 6, 7, 8] } { name: 'series 2' values: [1, 6, 7] } ] domain = data .map (series) -> [ Math.min.apply @, series.values Math.max.apply @, series.values ] .reduce (array, current) -> array[0] = Math.min array[0], current[0] array[1] = Math.max array[1], current[1] array , [Infinity, -Infinity]
That special map reduce feeling:
http://asset-9.soup.io/asset/3173/5343_925f.gif
On a tangent (for the Javascripters out there): that fancy Coffeescript @ symbol is shorthand for the “this” keyword.
http://coffeescript.org/#operators