Skip to content

Commit

Permalink
#189 more ctors
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jun 22, 2017
1 parent a51b4d1 commit 613e56b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 27 deletions.
47 changes: 20 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ new IterableAsCollection<>(
To iterate a collection:

```java
new AllOf(
new TransformedIterable<>(
new And(
new MappedIterable<>(
new ArrayAsIterable<>("how", "are", "you"),
new ProcAsFunc<>(
input -> {
Expand All @@ -157,11 +157,9 @@ new AllOf(
Or even more compact:

```java
new IterableAsBoolean(
new ArrayAsIterable<>("how", "are", "you"),
new ProcAsFunc<>(
input -> System.out.printf("Item: %s\n", i)
)
new And(
input -> System.out.printf("Item: %s\n", i),
"how", "are", "you"
).asValue();
```

Expand All @@ -171,9 +169,11 @@ To sort a list of words in the file:
List<String> sorted = new SortedList<>(
new IterableAsList<>(
new TextAsLines(
new InputAsText(
new FileAsInput(
new File("/tmp/names.txt")
new BytesAsText(
new InputAsBytes(
new FileAsInput(
new File("/tmp/names.txt")
)
)
)
)
Expand All @@ -185,7 +185,7 @@ To count elements in an iterable:

```java
int total = new LengthOfIterable(
new ArrayAsIterable<>("how", "are", "you")
"how", "are", "you"
).asValue();
```

Expand All @@ -203,15 +203,10 @@ This is its object-oriented alternative (no streams!):

```java
new And<>(
new MappedIterable<>(
names,
new ProcAsFunc<>(
n -> {
System.out.printf("Hello, %s!\n", n);
},
() -> true
)
)
names,
n -> {
System.out.printf("Hello, %s!\n", n);
}
).asValue();
```

Expand All @@ -227,13 +222,11 @@ Here is its object-oriented alternative:

```java
new And<>(
new MappedIterable<>(
new EndlessIterable<>(ready),
r -> {
System.out.prinln("Still waiting...");
return !ready;
}
)
new EndlessIterable<>(ready),
r -> {
System.out.prinln("Still waiting...");
return !ready;
}
).asValue();
```

Expand Down
50 changes: 50 additions & 0 deletions src/main/java/org/cactoos/func/And.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
package org.cactoos.func;

import java.util.Iterator;
import org.cactoos.Func;
import org.cactoos.Proc;
import org.cactoos.Scalar;
import org.cactoos.list.ArrayAsIterable;
import org.cactoos.list.MappedIterable;

/**
* Logical conjunction.
Expand All @@ -43,6 +46,53 @@ public final class And implements Scalar<Boolean> {
*/
private final Iterable<Scalar<Boolean>> iterable;

/**
* Ctor.
* @param proc Proc to map
* @param src The iterable
* @param <X> Type of items in the iterable
*/
@SafeVarargs
public <X> And(final Proc<X> proc, final X... src) {
this(new ProcAsFunc<>(proc, true), src);
}

/**
* Ctor.
* @param func Func to map
* @param src The iterable
* @param <X> Type of items in the iterable
*/
@SafeVarargs
public <X> And(final Func<X, Boolean> func, final X... src) {
this(new ArrayAsIterable<>(src), func);
}

/**
* Ctor.
* @param src The iterable
* @param proc Proc to use
* @param <X> Type of items in the iterable
*/
public <X> And(final Iterable<X> src, final Proc<X> proc) {
this(src, new ProcAsFunc<>(proc, true));
}

/**
* Ctor.
* @param src The iterable
* @param func Func to map
* @param <X> Type of items in the iterable
*/
public <X> And(final Iterable<X> src, final Func<X, Boolean> func) {
this(
new MappedIterable<>(
src,
item -> (Scalar<Boolean>) () -> func.apply(item)
)
);
}

/**
* Ctor.
* @param src The iterable
Expand Down

0 comments on commit 613e56b

Please sign in to comment.