Skip to content

Commit

Permalink
Merge pull request vavr-io#208 from danieldietrich/master
Browse files Browse the repository at this point in the history
Fixes vavr-io#207: numeric operations of traversables
  • Loading branch information
danieldietrich committed May 2, 2015
2 parents 1a4f186 + 008b6ef commit 287e6c8
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 315 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
<scala.version>2.11.6</scala.version>
</properties>
<build>
<resources>
<resource>
<directory>src/main/javascript</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
265 changes: 95 additions & 170 deletions src/main/java/javaslang/collection/Traversable.java

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/main/java/javaslang/test/Gen.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ Gen<T> gen(int n, Stream<Tuple2<Integer, Gen<T>>> stream) {
return (n <= k) ? stream.head()._2 : gen(n - k, stream.tail());
}
}
final int size = stream.map(t -> t._1).sum();
final int size = stream.foldLeft(0, (i, t) -> i + t._1);
return choose(1, size).flatMap(n -> new Frequency().gen(n, stream));
}

Expand Down
44 changes: 44 additions & 0 deletions src/main/javascript/javaslang/collection/traversable.js
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)
}
Loading

0 comments on commit 287e6c8

Please sign in to comment.