Skip to content

Commit

Permalink
no recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jun 22, 2017
1 parent 613e56b commit aa2eb70
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 42 deletions.
28 changes: 7 additions & 21 deletions src/main/java/org/cactoos/func/And.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.cactoos.func;

import java.util.Iterator;
import org.cactoos.Func;
import org.cactoos.Proc;
import org.cactoos.Scalar;
Expand Down Expand Up @@ -112,27 +111,14 @@ public And(final Iterable<Scalar<Boolean>> src) {

@Override
public Boolean asValue() throws Exception {
return this.conjunction(this.iterable.iterator(), true);
}

/**
* Conjunction.
*
* @param iterator The iterator
* @param value Previous value
* @return The result
* @throws Exception If fails
*/
private Boolean conjunction(
final Iterator<Scalar<Boolean>> iterator,
final boolean value
) throws Exception {
final Boolean result;
if (iterator.hasNext() && value) {
result = this.conjunction(iterator, iterator.next().asValue());
} else {
result = value;
boolean result = true;
for (final Scalar<Boolean> item : this.iterable) {
if (!item.asValue()) {
result = false;
break;
}
}
return result;
}

}
28 changes: 7 additions & 21 deletions src/main/java/org/cactoos/func/Or.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.cactoos.func;

import java.util.Iterator;
import org.cactoos.Scalar;
import org.cactoos.list.ArrayAsIterable;

Expand Down Expand Up @@ -62,27 +61,14 @@ public Or(final Iterable<Scalar<Boolean>> src) {

@Override
public Boolean asValue() throws Exception {
return this.disjunction(this.iterable.iterator(), false);
}

/**
* Disjunction.
*
* @param iterator The iterator
* @param value Previous value
* @return The result
* @throws Exception If fails
*/
private Boolean disjunction(
final Iterator<Scalar<Boolean>> iterator,
final boolean value
) throws Exception {
final Boolean result;
if (iterator.hasNext() && !value) {
result = this.disjunction(iterator, iterator.next().asValue());
} else {
result = value;
boolean result = false;
for (final Scalar<Boolean> item : this.iterable) {
if (item.asValue()) {
result = true;
break;
}
}
return result;
}

}

0 comments on commit aa2eb70

Please sign in to comment.