Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions core/src/main/java/fj/data/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import fj.Unit;
import fj.Show;
import fj.function.Effect1;
import fj.Equal;
import fj.Ord;

import static fj.Function.*;
import static fj.P.p;
Expand Down Expand Up @@ -415,6 +417,47 @@ public final <B> Option<B> sequence(final Option<B> o) {
return bind(c);
}

public <L, B> Either<L, Option<B>> traverseEither(F<A, Either<L, B>> f) {
return map(a -> f.f(a).right().map(b -> some(b))).orSome(Either.right(none()));
}

public <B> IO<Option<B>> traverseIO(F<A, IO<B>> f) {
return map(a -> IOFunctions.map(f.f(a), b -> some(b))).orSome(IOFunctions.lazy(u -> none()));
}

public <B> List<Option<B>> traverseList(F<A, List<B>> f) {
return map(a -> f.f(a).map(b -> some(b))).orSome(List.list());
}

public <B> Option<Option<B>> traverseOption(F<A, Option<B>> f) {
return map(f);
}

public <B> Stream<Option<B>> traverseStream(F<A, Stream<B>> f) {
return map(a -> f.f(a).map(b -> some(b))).orSome(Stream.nil());
}

public <B> P1<Option<B>> traverseP1(F<A, P1<B>> f) {
return map(a -> f.f(a).map(b -> some(b))).orSome(P.p(none()));
}

public <B> Seq<Option<B>> traverseSeq(F<A, Seq<B>> f) {
return map(a -> f.f(a).map(b -> some(b))).orSome(Seq.empty());
}

public <B> Set<Option<B>> traverseSet(Ord<B> ord, F<A, Set<B>> f) {
Ord<Option<B>> optOrd = Ord.optionOrd(ord);
return map(a -> f.f(a).map(optOrd, b -> some(b))).orSome(Set.empty(optOrd));
}

public <B> F2<Ord<B>, F<A, Set<B>>, Set<Option<B>>> traverseSet() {
return (o, f) -> traverseSet(o, f);
}

public <E, B> Validation<E, Option<B>> traverseValidation(F<A, Validation<E, B>> f) {
return map(a -> f.f(a).map(b -> some(b))).orSome(Validation.success(none()));
}

/**
* Performs function application within an optional value (applicative functor pattern).
*
Expand Down
30 changes: 30 additions & 0 deletions core/src/test/java/fj/data/OptionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fj.data;

import org.junit.Assert;
import org.junit.Test;

import static fj.data.Option.some;
import static org.junit.Assert.assertTrue;

/**
* Created by MarkPerry on 15/01/2015.
*/
public class OptionTest {

@Test
public void equals() {
int max = 4;
assertTrue(some(1).equals(some(1)));
assertTrue(some(List.range(1, max)).equals(some(List.range(1, max))));
}

@Test
public void traverseList() {
int max = 3;
List<Option<Integer>> actual = some(max).traverseList(a -> List.range(1, a + 1));
List<Option<Integer>> expected = List.range(1, max + 1).map(i -> some(i));
System.out.println(String.format("actual: %s, expected: %s", actual.toString(), expected.toString()));
assertTrue(actual.equals(expected));
}

}