Skip to content
Open
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
64 changes: 64 additions & 0 deletions src/main/java/ru/spbau/mit/Collections.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ru.spbau.mit;

import java.util.ArrayList;
import java.util.List;

public final class Collections {
private Collections() {}

public static <T, S> List<S> map(Function1<? super T, S> f, Iterable<T> c) {
List<S> newcol = new ArrayList<>();
for (T el : c) {
newcol.add(f.apply(el));
}
return newcol;
}

public static <T> List<T> filter(Predicate<? super T> pred, Iterable<T> c) {
List<T> newcol = new ArrayList<>();
for (T el : c) {
if (pred.apply(el)) {
newcol.add(el);
}
}
return newcol;
}

public static <T> List<T> takeWhile(Predicate<? super T> pred, Iterable<T> c) {
List<T> newcol = new ArrayList<>();
for (T el : c) {
if (!pred.apply(el)) {
break;
}
newcol.add(el);
}
return newcol;
}

public static <T> List<T> takeUnless(Predicate<? super T> pred, Iterable<T> c) {
return takeWhile(pred.not(), c);
}


public static <I, T> I foldl(Function2<I, ? super T, I> f, Iterable<T> c, I init) {
I res = init;
for (T el : c) {
res = f.apply2(res, el);
}
return res;
}

public static <I, T> I foldr(Function2<? super T, I, I> f, Iterable<T> c, I init) {
Function1<I, I> resfunc = new Function1<I, I>() {
@Override
public I apply(I arg) {
return arg;
}
};

for (T el : c) {
resfunc = f.bind1(el).compose(resfunc);
}
return resfunc.apply(init);
}
}
14 changes: 14 additions & 0 deletions src/main/java/ru/spbau/mit/Function1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.spbau.mit;

public abstract class Function1<T, S> {
public <G> Function1<T, G> compose(final Function1<? super S, G> g) {
return new Function1<T, G>() {
@Override
public G apply(T arg) {
return g.apply(Function1.this.apply(arg));
}
};
}

public abstract S apply(T arg);
}
41 changes: 41 additions & 0 deletions src/main/java/ru/spbau/mit/Function2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ru.spbau.mit;

public abstract class Function2<T1, T2, S> {
public Function1<T1, Function1<T2, S>> curry() {
return new Function1<T1, Function1<T2, S>>() {
@Override
public Function1<T2, S> apply(T1 arg1) {
return Function2.this.bind1(arg1);
}
};
}

// because of compose can't extend from Function1
public <S2> Function2<T1, T2, S2> compose(final Function1<? super S, S2> g) {
return new Function2<T1, T2, S2>() {
public S2 apply2(T1 arg1, T2 arg2) {
return g.apply(Function2.this.apply2(arg1, arg2));
}
};
}

public Function1<T2, S> bind1(final T1 arg1) {
return new Function1<T2, S>() {
@Override
public S apply(T2 arg2) {
return apply2(arg1, arg2);
}
};
}

public Function1<T1, S> bind2(final T2 arg2) {
return new Function1<T1, S>() {
@Override
public S apply(T1 arg1) {
return apply2(arg1, arg2);
}
};
}

public abstract S apply2(T1 arg1, T2 arg2);
}
44 changes: 44 additions & 0 deletions src/main/java/ru/spbau/mit/Predicate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ru.spbau.mit;

public abstract class Predicate<T> extends Function1<T, Boolean> {
public static final Predicate<Object> ALWAYS_TRUE = new Predicate<Object>() {
@Override
public Boolean apply(Object arg) {
return true;
}
};

public static final Predicate<Object> ALWAYS_FALSE = new Predicate<Object>() {
@Override
public Boolean apply(Object arg) {
return false;
}
};

public Predicate<T> and(final Predicate<? super T> rhs) {
return new Predicate<T>() {
@Override
public Boolean apply(T arg) {
return Predicate.this.apply(arg) && rhs.apply(arg);
}
};
}

public Predicate<T> or(final Predicate<? super T> rhs) {
return new Predicate<T>() {
@Override
public Boolean apply(T arg) {
return Predicate.this.apply(arg) || rhs.apply(arg);
}
};
}

public Predicate<T> not() {
return new Predicate<T>() {
@Override
public Boolean apply(T arg) {
return !Predicate.this.apply(arg);
}
};
}
}
179 changes: 179 additions & 0 deletions src/test/java/ru/spbau/mit/CollectionsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package ru.spbau.mit;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.junit.Assert.*;

public class CollectionsTest {
private static final Integer MAGIC_1 = 1;
private static final Integer MAGIC_2 = -4;
private static final Integer MAGIC_3 = 3;
private static final Integer MAGIC_4 = 10;
private static final Integer MAGIC_5 = -20;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше отключить проверку на константы в тестах

и не использовать обертки без надобности

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkstyle.xml находится не в моем репозитории. Был бы рад отключить проверку на константы, если бы знал, что сделаю это всего один раз.


private static final List<Integer> INT_ARR = Arrays.asList(
MAGIC_1, MAGIC_2, MAGIC_3, MAGIC_4, MAGIC_5);

private static final List<Integer> INT_ARR_NEGATED = Arrays.asList(
-MAGIC_1, -MAGIC_2, -MAGIC_3, -MAGIC_4, -MAGIC_5);

private static final List<Integer> ARR_WITH_NULL = Arrays.asList(
null, 1, null, 4, null);

private static final List<String> INT_ARR_STRINGED = Arrays.asList(
MAGIC_1.toString(),
MAGIC_2.toString(),
MAGIC_3.toString(),
MAGIC_4.toString(),
MAGIC_5.toString()
);

private static final Predicate<Integer> IS_POSITIVE_INTEGER = new Predicate<Integer>() {
@Override
public Boolean apply(Integer arg) {
return arg > 0;
}
};

private static final Predicate<Object> IS_NULL = new Predicate<Object>() {
@Override
public Boolean apply(Object arg) {
return arg == null;
}
};

private static final Predicate<Object> NOT_NULL = IS_NULL.not();

private static final List<Object> EMPTY_ARR = emptyList();

private static final Function2<Integer, Integer, Integer> MINUS = new Function2<Integer, Integer, Integer>() {
@Override
public Integer apply2(Integer arg1, Integer arg2) {
return arg1 - arg2;
}
};

private static final Function1<Object, Object> ID = new Function1<Object, Object>() {
@Override
public Object apply(Object arg) {
return arg;
}
};

private static final Function1<Object, String> TO_STRING = new Function1<Object, String>() {
@Override
public String apply(Object arg) {
return arg.toString();
}
};

private static final Function2<Object, Object, Object> FAIL_FUNCTION =
new Function2<Object, Object, Object>() {
@Override
public Object apply2(Object arg1, Object arg2) {
assert false;
return null;
}
};

@Test
public void map() throws Exception {
assertEquals(INT_ARR_NEGATED, Collections.map(MINUS.bind1(0), INT_ARR));
assertEquals(EMPTY_ARR, Collections.map(ID, EMPTY_ARR));
assertEquals(INT_ARR_STRINGED, Collections.map(TO_STRING, INT_ARR));
assertEquals(INT_ARR, Collections.map(ID, INT_ARR));
}

@Test
public void filter() throws Exception {
final List<Integer> positiveOnly = Arrays.asList(-MAGIC_2, -MAGIC_5);
assertEquals(positiveOnly, Collections.filter(IS_POSITIVE_INTEGER, INT_ARR_NEGATED));
assertEquals(INT_ARR, Collections.filter(Predicate.ALWAYS_TRUE, INT_ARR));
assertEquals(EMPTY_ARR, Collections.filter(Predicate.ALWAYS_FALSE, INT_ARR));
assertEquals(EMPTY_ARR, Collections.filter(IS_NULL, INT_ARR));
assertEquals(Arrays.asList(null, null, null), Collections.filter(IS_NULL, ARR_WITH_NULL));
Predicate<Object> isTen = new Predicate<Object>() {
@Override
public Boolean apply(Object arg) {
return (arg).toString().equals("10");
}
};
final int ten = 10;
assertEquals(singletonList(ten), Collections.filter(isTen, INT_ARR));
}

@Test
public void takeWhile() throws Exception {
Predicate<CharSequence> u2 = new Predicate<CharSequence>() {
@Override
public Boolean apply(CharSequence charSequence) {
return charSequence.length() < 2;
}
};
assertEquals(singletonList("1"), Collections.takeWhile(u2, INT_ARR_STRINGED));
assertEquals(EMPTY_ARR, Collections.takeWhile(IS_POSITIVE_INTEGER, INT_ARR_NEGATED));
assertEquals(INT_ARR_STRINGED, Collections.takeWhile(Predicate.ALWAYS_TRUE, INT_ARR_STRINGED));
ArrayList<Integer> nullArray = new ArrayList<>();
nullArray.add(null);
assertEquals(nullArray, Collections.takeWhile(IS_NULL, ARR_WITH_NULL));
assertEquals(EMPTY_ARR, Collections.takeWhile(IS_NULL, INT_ARR));
}

@Test
public void takeUnless() throws Exception {
assertEquals(EMPTY_ARR, Collections.takeUnless(IS_POSITIVE_INTEGER.not(), INT_ARR_NEGATED));
assertEquals(singletonList(-1), Collections.takeUnless(IS_POSITIVE_INTEGER, INT_ARR_NEGATED));
assertEquals(INT_ARR, Collections.takeUnless(Predicate.ALWAYS_FALSE, INT_ARR));
ArrayList<Integer> nullArr = new ArrayList<>();
nullArr.add(null);
assertEquals(nullArr, Collections.takeUnless(NOT_NULL, ARR_WITH_NULL));
assertEquals(EMPTY_ARR, Collections.takeUnless(IS_NULL, ARR_WITH_NULL));
}

@Test
public void foldl() throws Exception {
final Integer result = 10;
assertEquals(result, Collections.foldl(MINUS, INT_ARR, 0));

final Function2<String, Object, String> accumulateString = new Function2<String, Object, String>() {
@Override
public String apply2(String arg1, Object arg2) {
return arg1 + Objects.toString(arg2);
}
};

final String initString = "Hello, ";
final String resultString = "Hello, 1-4310-20";
assertEquals(resultString, Collections.foldl(accumulateString, INT_ARR, initString));
assertEquals(0, Collections.foldl(FAIL_FUNCTION, EMPTY_ARR, 0));
String expected = "null1null4null";
assertEquals(expected, Collections.foldl(accumulateString, ARR_WITH_NULL, ""));
}

@Test
public void foldr() throws Exception {

final Function2<Object, String, String> accumulateString = new Function2<Object, String, String>() {
@Override
public String apply2(Object arg1, String arg2) {
return Objects.toString(arg1) + arg2;
}
};

final String initString = "123";
final String resultString = "-14-3-1020123";
assertEquals(resultString, Collections.foldr(accumulateString, INT_ARR_NEGATED, initString));

final Integer init = -33;
final Integer result = 11;
assertEquals(result, Collections.foldr(MINUS, INT_ARR, init));
assertEquals(0, Collections.foldr(FAIL_FUNCTION, EMPTY_ARR, 0));
}
}
Loading