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
30 changes: 16 additions & 14 deletions core/src/main/java/fj/Equal.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
package fj;

import fj.data.Array;
import fj.data.Either;
import fj.data.LazyString;
import fj.data.List;
import fj.data.Natural;
import fj.data.NonEmptyList;
import fj.data.Option;
import fj.data.Seq;
import fj.data.Set;
import fj.data.Stream;
import fj.data.Tree;
import fj.data.TreeMap;
import fj.data.Validation;
import fj.data.Writer;
import fj.data.*;
import fj.data.hamt.BitSet;
import fj.data.hlist.HList;
import fj.data.vector.V2;
Expand Down Expand Up @@ -455,6 +442,21 @@ public static <A> Equal<Stream<A>> streamEqual(final Equal<A> ea) {
});
}

/**
* An equal instance for the {@link Zipper} type.
*
* @param ea Equality across the elements of the zipper.
* @return An equal instance for the {@link Zipper} type.
*/
public static <A> Equal<Zipper<A>> zipperEqual(final Equal<A> ea) {
Equal<Stream<A>> se = Equal.streamEqual(ea);
return equalDef((a1, a2) ->
se.eq(a1.lefts(), a2.lefts()) &&
ea.eq(a1.focus(), a2.focus()) &&
se.eq(a1.rights(), a2.rights())
);
}

/**
* An equal instance for the {@link Array} type.
*
Expand Down
20 changes: 20 additions & 0 deletions core/src/main/java/fj/Hash.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,26 @@ public static <A> Hash<Array<A>> arrayHash(final Hash<A> ha) {
});
}

/**
* A hash instance for the {@link Zipper} type.
*
* @param ha A hash for the elements of the zipper.
* @return A hash instance for the {@link Zipper} type.
*/
public static <A> Hash<Zipper<A>> zipperHash(final Hash<A> ha) {
Hash<Stream<A>> sh = streamHash(ha);
return hash(as -> {
final int p = 419;
int r = 239;

r = p * r + sh.hash(as.lefts());
r = p * r + ha.hash(as.focus());
r = p * r + sh.hash(as.rights());

return r;
});
}

/**
* A hash instance for the {@link Tree} type.
*
Expand Down
23 changes: 11 additions & 12 deletions core/src/main/java/fj/data/Zipper.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
package fj.data;

import fj.Equal;
import fj.F;
import fj.F2;
import fj.F2Functions;
import fj.F3;
import fj.Function;
import fj.Ord;
import fj.P;
import fj.P1;
import fj.P2;
import fj.P3;
import fj.Show;
import fj.*;
import fj.function.Integers;

import java.util.Iterator;
Expand Down Expand Up @@ -106,6 +95,16 @@ public static <A> Ord<Zipper<A>> ord(final Ord<A> o) {
return Ord.p3Ord(so, o, so).contramap(Zipper.p_());
}

@Override
public final boolean equals(Object other) {
return Equal.equals0(Zipper.class, this, other, () -> Equal.zipperEqual(Equal.anyEqual()));
}

@Override
public final int hashCode() {
return Hash.zipperHash(Hash.<A>anyHash()).hash(this);
}

/**
* An Equal instance for Zippers.
*
Expand Down
110 changes: 110 additions & 0 deletions core/src/test/java/fj/data/ZipperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package fj.data;

import org.junit.Test;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

public class ZipperTest {
@Test
public void testZipper() {
Zipper<Integer> z = Zipper.zipper(Stream.nil(), 0, Stream.range(1, 9));
assertThat(z.map(i -> i + 13).toStream(), is(Stream.range(13, 22)));
}

@Test
public void testNext() {
Zipper<Integer> z = Zipper.zipper(Stream.single(1), 2, Stream.single(3));
z = z.next().some();
assertThat(z.lefts(), is(Stream.arrayStream(new Integer[]{2, 1})));
assertThat(z.focus(), is(3));
assertThat(z.rights(), is(Stream.nil()));
}

@Test
public void testNextNone() {
Zipper<Integer> z = Zipper.zipper(Stream.single(1), 2, Stream.nil());
assertThat(z.next().isNone(), is(true));
}

@Test
public void testCycleNext() {
Zipper<Integer> z = Zipper.zipper(Stream.single(1), 2, Stream.single(3));
assertThat(z.cycleNext(), is(z.next().some()));
}

@Test
public void testCycleNextLast() {
Zipper<Integer> z = Zipper.zipper(Stream.single(1), 2, Stream.nil());
z = z.cycleNext();
assertThat(z.lefts(), is(Stream.nil()));
assertThat(z.focus(), is(1));
assertThat(z.rights(), is(Stream.single(2)));
}

@Test
public void testPrevious() {
Zipper<Integer> z = Zipper.zipper(Stream.single(1), 2, Stream.single(3));
z = z.previous().some();
assertThat(z.lefts(), is(Stream.nil()));
assertThat(z.focus(), is(1));
assertThat(z.rights(), is(Stream.arrayStream(new Integer[]{2, 3})));
}

@Test
public void testPreviousNone() {
Zipper<Integer> z = Zipper.zipper(Stream.nil(), 2, Stream.single(3));
assertThat(z.previous().isNone(), is(true));
}

@Test
public void testCyclePrevious() {
Zipper<Integer> z = Zipper.zipper(Stream.single(1), 2, Stream.single(3));
assertThat(z.cyclePrevious(), is(z.previous().some()));
}

@Test
public void testCyclePreviousFirst() {
Zipper<Integer> z = Zipper.zipper(Stream.nil(), 1, Stream.single(2));
z = z.cyclePrevious();
assertThat(z.lefts(), is(Stream.single(1)));
assertThat(z.focus(), is(2));
assertThat(z.rights(), is(Stream.nil()));
}

@Test
public void testInsertLeft() {
Zipper<Integer> z = Zipper.single(2);
z = z.insertLeft(1);
assertThat(z.lefts(), is(Stream.nil()));
assertThat(z.focus(), is(1));
assertThat(z.rights(), is(Stream.single(2)));
}

@Test
public void testInsertRight() {
Zipper<Integer> z = Zipper.single(2);
z = z.insertRight(3);
assertThat(z.lefts(), is(Stream.single(2)));
assertThat(z.focus(), is(3));
assertThat(z.rights(), is(Stream.nil()));
}

@Test
public void testDeleteOthers() {
Zipper<Integer> z = Zipper.zipper(Stream.single(1), 2, Stream.single(3));
z = z.deleteOthers();
assertThat(z.lefts(), is(Stream.nil()));
assertThat(z.focus(), is(2));
assertThat(z.rights(), is(Stream.nil()));
}

@Test
public void testFind() {
Zipper<Integer> z = Zipper.zipper(Stream.nil(), 0, Stream.range(1));
z = z.find(i -> i == 4).some();
assertThat(z.lefts(), is(Stream.arrayStream(new Integer[]{3, 2, 1, 0})));
assertThat(z.focus(), is(4));
assertThat(z.rights().take(3), is(Stream.range(5, 8)));
}
}