Skip to content

Commit 212198d

Browse files
committed
Added sequence, traverse and join to IOFunctions
1 parent b138427 commit 212198d

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ subprojects {
6565

6666
repositories {
6767
mavenCentral()
68-
}
68+
maven {
69+
url sonatypeRepositoryUrl
70+
}
71+
72+
}
6973

7074
apply from: "$rootDir/lib.gradle"
7175
apply plugin: "maven"

consume/build.gradle

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11

2-
repositories {
3-
maven {
4-
url sonatypeRepositoryUrl
5-
}
6-
}
7-
82
dependencies {
93
compile("$group:$projectName:$fjConsumeVersion")
104
}

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@
1313
import java.nio.charset.Charset;
1414
import java.util.Arrays;
1515

16-
import fj.F;
17-
import fj.F1Functions;
18-
import fj.Function;
19-
import fj.P;
20-
import fj.P1;
21-
import fj.P2;
22-
import fj.Unit;
16+
import fj.*;
2317
import fj.data.Iteratee.Input;
2418
import fj.data.Iteratee.IterV;
2519

@@ -309,4 +303,36 @@ public B run() throws IOException {
309303
}
310304
};
311305
}
306+
307+
/**
308+
* Evaluate each action in the sequence from left to right, and collect the results.
309+
*
310+
* @param list
311+
* @return
312+
*/
313+
public static <A> IO<List<A>> sequence(List<IO<A>> list) {
314+
F2<IO<A>, IO<List<A>>, IO<List<A>>> f2 = (io, ioList) ->
315+
IOFunctions.bind(ioList, (xs) -> map(io, x -> List.cons(x, xs)));
316+
return list.foldRight(f2, IOFunctions.unit(List.<A>nil()));
317+
}
318+
319+
/**
320+
* Map each element of a structure to an action, evaluate these actions from left to right
321+
* and collect the results.
322+
*
323+
* @param list
324+
* @param f
325+
* @return
326+
*/
327+
public static <A, B> IO<List<B>> traverse(List<A> list, F<A, IO<B>> f) {
328+
F2<A, IO<List<B>>, IO<List<B>>> f2 = (a, acc) ->
329+
bind(acc, (bs) -> map(f.f(a), b -> bs.append(List.list(b))));
330+
return list.foldRight(f2, IOFunctions.unit(List.<B>nil()));
331+
}
332+
333+
public static <A> IO<A> join(IO<IO<A>> io1) {
334+
return bind(io1, io2 -> io2);
335+
}
336+
337+
312338
}

0 commit comments

Comments
 (0)