forked from vavr-io/vavr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request vavr-io#208 from danieldietrich/master
Fixes vavr-io#207: numeric operations of traversables
- Loading branch information
Showing
7 changed files
with
330 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Computes the sum of iterable elements as double. | ||
*/ | ||
var avg = function(iterable) { | ||
// division by zero is NaN | ||
return compute(iterable, 0.0, function(acc, value) { return acc + value }, function(acc, count) { return acc / count }) | ||
} | ||
|
||
/* | ||
* Computes the product of iterable elements as double. | ||
*/ | ||
var product = function(iterable) { | ||
return compute(iterable, 1.0, function(acc, value) { return acc * value }, function(acc, count) { return acc }) | ||
} | ||
|
||
/* | ||
* Computes the sum of iterable elements as double. | ||
*/ | ||
var sum = function(iterable) { | ||
return compute(iterable, 0.0, function(acc, value) { return acc + value }, function(acc, count) { return acc }) | ||
} | ||
|
||
/* | ||
* Represents a computation given iterable elements. | ||
* | ||
* @param iterable A java.lang.Iterable of arbitrary objects. | ||
* @param zero The neutral element regarding the accumulate operation. | ||
* @param accumulate(acc, value) Accumulates the current accumulator with the given value. | ||
* @param result(acc, count) Computes the result given the accumulator and the element count. | ||
* @return a double value or NaN if an element has no numerical representation using parseFloat() | ||
*/ | ||
var compute = function(iterable, zero, accumulate, result) { | ||
var acc = zero | ||
var count = 0 | ||
for each (elem in iterable) { | ||
var float = parseFloat(elem) | ||
if (isNaN(float)) { | ||
return float | ||
} | ||
acc = accumulate(acc, float) | ||
count += 1 | ||
} | ||
return result(acc, count) | ||
} |
Oops, something went wrong.