Skip to content

Commit 4377adb

Browse files
Fixes #407. Add fj.data.Seq.bind(F<A, Seq<B>> f).
1 parent d85daab commit 4377adb

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

core/src/main/java/fj/data/Seq.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
package fj.data;
22

33
import fj.*;
4+
import fj.data.List.Buffer;
5+
import fj.data.fingertrees.*;
6+
7+
import java.util.*;
48

59
import static fj.Bottom.error;
610
import static fj.Monoid.intAdditionMonoid;
711
import static fj.data.fingertrees.FingerTree.measured;
812

9-
import fj.data.List.Buffer;
10-
import fj.data.fingertrees.FingerTree;
11-
import fj.data.fingertrees.MakeTree;
12-
import fj.data.fingertrees.Measured;
13-
14-
import java.util.AbstractList;
15-
import java.util.Iterator;
16-
import java.util.NoSuchElementException;
17-
1813
/**
1914
* Provides an immutable finite sequence, implemented as a finger tree. This structure gives O(1) access to
2015
* the head and tail, as well as O(log n) random access and concatenation of sequences.
@@ -396,4 +391,16 @@ public <B> Seq<B> map(F<A, B> f) {
396391
return new Seq<>(ftree.map(f, Seq.elemMeasured()));
397392
}
398393

394+
/**
395+
* Bind the given function across this seq.
396+
*
397+
* @param f the given function
398+
* @param <B> the type of the seq value
399+
* @return the seq
400+
*/
401+
public <B> Seq<B> bind(final F<A, Seq<B>> f) {
402+
return foldRight(
403+
(element, accumulator) -> f.f(element).append(accumulator),
404+
empty());
405+
}
399406
}

core/src/test/java/fj/data/SeqTest.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
import fj.P2;
44
import org.junit.Test;
55

6+
import static fj.Function.constant;
67
import static org.hamcrest.MatcherAssert.assertThat;
78
import static org.hamcrest.core.Is.is;
8-
import static org.junit.Assert.assertEquals;
9-
import static org.junit.Assert.assertFalse;
10-
import static org.junit.Assert.assertTrue;
9+
import static org.junit.Assert.*;
1110

1211
/**
1312
* Created by MarkPerry on 16/01/2015.
@@ -46,4 +45,16 @@ public void test() {
4645
assertThat(p2._2(), is(Seq.empty()));
4746
}
4847

48+
@Test
49+
public void testBind() {
50+
assertEquals(Seq.empty(), Seq.empty().bind(constant(Seq.empty())));
51+
assertEquals(Seq.empty(), Seq.empty().bind(constant(Seq.single(0))));
52+
assertEquals(Seq.empty(), Seq.empty().bind(constant(Seq.arraySeq(0, 1))));
53+
assertEquals(Seq.empty(), Seq.single("zero").bind(constant(Seq.empty())));
54+
assertEquals(Seq.single(0), Seq.single("zero").bind(constant(Seq.single(0))));
55+
assertEquals(Seq.arraySeq(0, 1), Seq.single("zero").bind(constant(Seq.arraySeq(0, 1))));
56+
assertEquals(Seq.empty(), Seq.arraySeq("zero", "one").bind(constant(Seq.empty())));
57+
assertEquals(Seq.arraySeq(0, 0), Seq.arraySeq("zero", "one").bind(constant(Seq.single(0))));
58+
assertEquals(Seq.arraySeq(0, 1, 0, 1), Seq.arraySeq("zero", "one").bind(constant(Seq.arraySeq(0, 1))));
59+
}
4960
}

0 commit comments

Comments
 (0)