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
39 changes: 39 additions & 0 deletions core/src/main/java/fj/function/Longs.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package fj.function;

import fj.F;
import fj.Monoid;
import fj.data.List;
import fj.data.Option;

import static fj.Function.curry;
import static fj.Semigroup.longAdditionSemigroup;
import static fj.Semigroup.longMultiplicationSemigroup;

import static fj.data.Option.none;
import static fj.data.Option.some;
import static java.lang.Math.abs;

/**
Expand Down Expand Up @@ -48,4 +53,38 @@ private Longs() {
*/
public static final F<Long, F<Long, Long>> remainder = curry((a, b) -> a % b);

/**
* Sums a list of longs.
*
* @param longs A list of longs to sum.
* @return The sum of the longs in the list.
*/
public static long sum(final List<Long> longs) {
return Monoid.longAdditionMonoid.sumLeft(longs);
}

/**
* Returns the product of a list of integers.
*
* @param longs A list of longs to multiply together.
* @return The product of the longs in the list.
*/
public static long product(final List<Long> longs) {
return Monoid.longMultiplicationMonoid.sumLeft(longs);
}

/**
* A function that converts strings to integers.
*
* @return A function that converts strings to integers.
*/
public static F<String, Option<Long>> fromString() {
return s -> {
try { return some(Long.valueOf(s)); }
catch (final NumberFormatException ignored) {
return none();
}
};
}

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

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import static fj.data.List.list;
import static fj.data.Option.none;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;

public class LongsTest {

@Test
public void testSum() {
assertThat(Longs.sum(list(3L, 4L, 5L)), is(12L));
}

@Test
public void testProduct() {
assertThat(Longs.product(list(3L, 4L, 5L)), is(60L));
}

@Test
public void testAdd() {
assertThat(Longs.add.f(10L).f(20L), is(30L));
}

@Test
public void testMultiply() {
assertThat(Longs.multiply.f(3L).f(5L), is(15L));
}

@Test
public void testAbs() {
assertThat(Longs.abs.f(-5L), is(5L));
}

@Test
public void testFromString() {
assertThat(Longs.fromString().f("-123").some(), is(-123L));
}

@Test
public void testFromStringFail() {
assertThat(Longs.fromString().f("w"), is(none()));
}

@Test
public void testCannotInstantiate() throws NoSuchMethodException, IllegalAccessException, InstantiationException {
Constructor<Longs> constructor = Longs.class.getDeclaredConstructor();
constructor.setAccessible(true);
try {
constructor.newInstance();
fail("expected InvocationTargetException");
} catch (InvocationTargetException ite) {
assertTrue(ite.getCause() instanceof UnsupportedOperationException);
}
}

}