Skip to content

Implement intersection monoid and semigroup #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Nov 3, 2019
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
50 changes: 50 additions & 0 deletions core/src/main/java/fj/Bounded.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package fj;

/**
* The Bounded class is used to name the upper and lower limits of a type.
* Ord is not a superclass of Bounded since types that are not totally ordered may also have upper and lower bounds.
*/
public final class Bounded<A> {

private final Definition<A> def;

/**
* Minimal definition of Bounded
*/
public interface Definition<A> {
A min();

A max();
}

private Bounded(Definition<A> definition) {
this.def = definition;
}

public A min() {
return def.min();
}

public A max() {
return def.max();
}

public static <A> Bounded<A> boundedDef(Definition<A> def) {
return new Bounded<>(def);
}

public static <A> Bounded<A> bounded(A min, A max) {
return boundedDef(new Definition<A>() {
@Override
public A min() {
return min;
}

@Override
public A max() {
return max;
}
});
}

}
32 changes: 23 additions & 9 deletions core/src/main/java/fj/Monoid.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@

import static fj.F1Functions.dimap;

import fj.data.Array;
import fj.data.DList;
import fj.data.List;
import fj.data.IO;
import fj.data.Natural;
import fj.data.Option;
import fj.data.Set;
import fj.data.Stream;
import fj.data.*;

import static fj.Function.*;
import static fj.Semigroup.semigroupDef;
Expand Down Expand Up @@ -1090,7 +1083,7 @@ public Unit append(Unit a1, Unit a2) {
});

/**
* A monoid for sets.
* A union monoid for sets.
*
* @param o An order for set elements.
* @return A monoid for sets whose elements have the given order.
Expand All @@ -1109,6 +1102,27 @@ public Set<A> append(Set<A> a1, Set<A> a2) {
});
}

/**
* A intersection monoid for sets.
*
* @param bounded A bound for all possible elements
* @param enumerator An enumerator for all possible elements
* @return A monoid for sets whose elements have the given order.
*/
public static <A> Monoid<Set<A>> setIntersectionMonoid(final Bounded<A> bounded, final Enumerator<A> enumerator) {
return monoidDef(new Definition<Set<A>>() {
@Override
public Set<A> empty() {
return Set.iteratorSet(enumerator.order(), enumerator.toStream(bounded).iterator());
}

@Override
public Set<A> append(Set<A> a1, Set<A> a2) {
return a1.intersect(a2);
}
});
}

/**
* A monoid for the maximum of elements with ordering o.
* @deprecated since 4.7. Use {@link Ord#maxMonoid(Object)}
Expand Down
11 changes: 10 additions & 1 deletion core/src/main/java/fj/Semigroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -621,12 +621,21 @@ public static <A> Semigroup<IO<A>> ioSemigroup(final Semigroup <A> sa) {
public static final Semigroup<Unit> unitSemigroup = unitMonoid.semigroup();

/**
* A semigroup for sets.
* A union semigroup for sets.
*
* @return a semigroup for sets.
*/
public static <A> Semigroup<Set<A>> setSemigroup() {
return semigroupDef(Set::union);
}

/**
* A intersection semigroup for sets.
*
* @return a semigroup for sets.
*/
public static <A> Semigroup<Set<A>> setIntersectionSemigroup() {
return semigroupDef(Set::intersect);
}

}
18 changes: 13 additions & 5 deletions core/src/main/java/fj/data/Enumerator.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package fj.data;

import fj.F;
import fj.*;

import static fj.Function.*;
import static fj.data.Option.none;
import static fj.data.Option.some;

import fj.Function;
import fj.Ord;

import static fj.Ord.*;
import fj.Ordering;
import static fj.Ordering.*;

import java.math.BigDecimal;
Expand Down Expand Up @@ -185,6 +181,18 @@ public Stream<A> toStream(final A a) {
return Stream.fromFunction(this, id, a);
}

/**
* Returns a stream of the values from this enumerator,
* starting at the min of given Bounded, ending at the max, counting up.
*
* @param bounded A value at which to begin the stream.
* @return a stream of the values from this enumerator, cut by bounded, counting up.
*/
public Stream<A> toStream(final Bounded<A> bounded) {
final F<A, A> id = identity();
return Stream.fromFunction(this, id, bounded.min()).takeWhile(item -> order.isLessThanOrEqualTo(item, bounded.max()));
}

/**
* Create a new enumerator with the given minimum value.
*
Expand Down
39 changes: 39 additions & 0 deletions core/src/test/java/fj/MonoidTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fj;

import fj.data.Enumerator;
import fj.data.Option;
import fj.data.Set;
import fj.data.Stream;
import org.junit.Test;

Expand All @@ -16,4 +18,41 @@ public void lifted_sum_of_two_numbers() {
assertThat(optionMonoid.sum(some(3), some(5)), is(some(8)));
assertThat(optionMonoid.sumLeft(Stream.arrayStream(some(3), some(5))), is(some(8)));
}

@Test
public void intersection_monoid_test() {
Bounded<Integer> integersBounded = Bounded.bounded(0, 10);
Monoid<Set<Integer>> intersectionMonoid = Monoid.setIntersectionMonoid(integersBounded, Enumerator.intEnumerator);
Set<Integer> first = Set.set(Ord.intOrd, 1, 2, 3, 4);
Set<Integer> second = Set.set(Ord.intOrd, 3, 4, 5, 6);
Set<Integer> actual = intersectionMonoid.sum(first, second);
assertThat(actual, is(Set.set(Ord.intOrd, 3, 4)));
}

@Test
public void union_monoid_test() {
Monoid<Set<Integer>> unionMonoid = Monoid.setMonoid(Ord.intOrd);
Set<Integer> first = Set.set(Ord.intOrd, 1, 2, 3, 4);
Set<Integer> second = Set.set(Ord.intOrd, 3, 4, 5, 6);
Set<Integer> actual = unionMonoid.sum(first, second);
assertThat(actual, is(Set.set(Ord.intOrd, 1, 2, 3, 4, 5, 6)));
}

@Test
public void intersection_monoid_zero_test() {
Bounded<Integer> integersBounded = Bounded.bounded(0, 10);
Monoid<Set<Integer>> monoid = Monoid.setIntersectionMonoid(integersBounded, Enumerator.intEnumerator);
Set<Integer> set = Set.set(Ord.intOrd, 7, 8, 9, 10);
Set<Integer> zero = monoid.zero();
assertThat(monoid.sum(zero, set), is(set));
}

@Test
public void union_monoid_zero_test() {
Monoid<Set<Integer>> monoid = Monoid.setMonoid(Ord.intOrd);
Set<Integer> set = Set.set(Ord.intOrd, 1, 2, 3, 4);
Set<Integer> zero = monoid.zero();
assertThat(monoid.sum(zero, set), is(set));
}

}
26 changes: 26 additions & 0 deletions core/src/test/java/fj/SemigroupTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fj;

import fj.data.Set;
import org.junit.Test;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class SemigroupTest {

@Test
public void intersection_semigroup_test() {
Semigroup<Set<Integer>> intersectionSemigroup = Semigroup.setIntersectionSemigroup();
Set<Integer> first = Set.set(Ord.intOrd, 1, 2, 3, 4);
Set<Integer> second = Set.set(Ord.intOrd, 3, 4, 5, 6);
assertThat(intersectionSemigroup.sum(first, second), is(Set.set(Ord.intOrd, 3, 4)));
}

@Test
public void union_semigroup_test() {
Semigroup<Set<Integer>> unionSemigroup = Semigroup.setSemigroup();
Set<Integer> first = Set.set(Ord.intOrd, 1, 2, 3, 4);
Set<Integer> second = Set.set(Ord.intOrd, 3, 4, 5, 6);
assertThat(unionSemigroup.sum(first, second), is(Set.set(Ord.intOrd, 1, 2, 3, 4, 5, 6)));
}
}