Skip to content

Commit 11d0b6a

Browse files
committed
Unfoldr is now lazier
1 parent b51fd9b commit 11d0b6a

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
2020
- `View`/`Set`/`Over` now only require `LensLike`
2121
- `HMap#keys` now returns a `Set`
2222
- `HMap#values` now returns a `Collection`
23+
- `Unfoldr` is now lazier, deferring all computations until `hasNext/next` calls
2324

2425
### Added
2526
- `BoundedBifunctor`, a `Bifunctor` super type that offers upper bounds for both parameters

‎src/main/java/com/jnape/palatable/lambda/iteration/UnfoldingIterator.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@
1010

1111
public class UnfoldingIterator<A, B> extends ImmutableIterator<A> {
1212
private final Function<? super B, Maybe<Tuple2<A, B>>> function;
13+
private B seed;
1314
private Maybe<Tuple2<A, B>> maybeAcc;
1415

15-
public UnfoldingIterator(Function<? super B, Maybe<Tuple2<A, B>>> function, B b) {
16+
public UnfoldingIterator(Function<? super B, Maybe<Tuple2<A, B>>> function, B seed) {
1617
this.function = function;
17-
maybeAcc = function.apply(b);
18+
this.seed = seed;
1819
}
1920

2021
@Override
2122
public boolean hasNext() {
23+
if (maybeAcc == null)
24+
maybeAcc = function.apply(seed);
25+
2226
return maybeAcc.fmap(constantly(true)).orElse(false);
2327
}
2428

@@ -30,7 +34,8 @@ public A next() {
3034

3135
Tuple2<A, B> acc = maybeAcc.orElseThrow(NoSuchElementException::new);
3236
A next = acc._1();
33-
maybeAcc = function.apply(acc._2());
37+
seed = acc._2();
38+
maybeAcc = null;
3439
return next;
3540
}
3641
}

‎src/test/java/com/jnape/palatable/lambda/iteration/UnfoldingIteratorTest.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
import com.jnape.palatable.lambda.functions.Fn1;
66
import org.junit.Test;
77

8+
import java.util.concurrent.atomic.AtomicInteger;
9+
810
import static com.jnape.palatable.lambda.adt.Maybe.just;
911
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
1012
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
1113
import static org.hamcrest.core.Is.is;
14+
import static org.junit.Assert.assertEquals;
1215
import static org.junit.Assert.assertThat;
1316

1417
public class UnfoldingIteratorTest {
@@ -24,4 +27,17 @@ public void hasNextIfFunctionProducesPresentValue() {
2427
public void doesNotHaveNextIfFunctionProducesEmptyValue() {
2528
assertThat(new UnfoldingIterator<String, Integer>(x -> nothing(), 0).hasNext(), is(false));
2629
}
30+
31+
@Test
32+
public void defersNextCallForAsLongAsPossible() {
33+
AtomicInteger invocations = new AtomicInteger(0);
34+
UnfoldingIterator<String, Integer> iterator = new UnfoldingIterator<>(x -> {
35+
invocations.incrementAndGet();
36+
return just(tuple(x.toString(), x + 1));
37+
}, 1);
38+
39+
assertEquals(0, invocations.get());
40+
iterator.next();
41+
assertEquals(1, invocations.get());
42+
}
2743
}

0 commit comments

Comments
 (0)