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
25 changes: 25 additions & 0 deletions hw_04/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>2</groupId>
<artifactId>hw_04</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>hw_04</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
57 changes: 57 additions & 0 deletions hw_04/src/main/java/hw_04/Collections.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package hw_04;

import java.util.*;

public class Collections {
public static <R1, R extends R1, A> List<R1> map(Function1<R, A> f, Iterable<? extends A> a) {
ArrayList<R1> res = new ArrayList<R1>();
for (A i : a) {
res.add(f.apply(i));
}
return res;
}

public static <A1, A extends A1> List<A1> filter(Predicate<A> p, Iterable<? extends A> a) {
ArrayList<A1> res = new ArrayList<A1>();
for (A i : a) {
if (p.test(i)) {
res.add(i);
}
}
return res;
}

public static <A1, A extends A1> List<A1> takeWhile(Predicate<A> p, Iterable<? extends A> a) {
ArrayList<A1> res = new ArrayList<A1>();
for (A i : a) {
if (!p.test(i)) {
return res;
}
res.add(i);
}
return res;
}

public static <A1, A extends A1> List<A1> takeUnless(Predicate<A> p, Iterable<? extends A> a) {
return takeWhile(p.not(), a);
}

private static <R, A> R foldr(Function2<R, A, R> f, R ini, Iterator<? extends A> iter) {
if (!iter.hasNext()) {
return ini;
}
return f.apply(iter.next(), foldr(f, ini, iter));
}

public static <R, A> R foldr(Function2<R, A, R> f, R ini, Collection<? extends A> col) {
return foldr(f, ini, col.iterator());
}

public static <R, A> R foldl(Function2<R, R, A> f, R ini, Collection<? extends A> col) {
Iterator<? extends A> iter = col.iterator();
while (iter.hasNext()) {
ini = f.apply(ini, iter.next());
}
return ini;
}
}
15 changes: 15 additions & 0 deletions hw_04/src/main/java/hw_04/Function1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package hw_04;

public interface Function1<R, A1> {
R apply(A1 arg);

default <Rcomp, Rg extends Rcomp, Acomp extends A1> Function1<Rcomp, Acomp> compose(Function1<Rg, ? super R> g) {
return new Function1<Rcomp, Acomp>() {

@Override
public Rg apply(Acomp arg) {
return g.apply(Function1.this.apply(arg));
}
};
}
}
48 changes: 48 additions & 0 deletions hw_04/src/main/java/hw_04/Function2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package hw_04;

public abstract class Function2<R, A1, A2> {
public abstract R apply(A1 arg1, A2 arg2);

public <Rcomp, Rg extends Rcomp, A1comp extends A1, A2comp extends A2>
Function2<Rcomp, A1comp, A2comp> compose(Function1<Rg, ? super R> g) {
return new Function2<Rcomp, A1comp, A2comp>() {

@Override
public Rg apply(A1comp arg1, A2comp arg2) {
return g.apply(Function2.this.apply(arg1, arg2));
}

};
}

public Function1<R, A2> bind1(A1 arg1) {
return new Function1<R, A2>() {

@Override
public R apply(A2 arg) {
return Function2.this.apply(arg1, arg);
}
};
}

public Function1<R, A1> bind2(A2 arg2) {
return new Function1<R, A1>() {

@Override
public R apply(A1 arg) {
return Function2.this.apply(arg, arg2);
}
};
}

public Function1<Function1<R, A2>, A1> curry() {
return new Function1<Function1<R, A2>, A1>() {

@Override
public Function1<R, A2> apply(A1 arg) {
return Function2.this.bind1(arg);
}

};
}
}
50 changes: 50 additions & 0 deletions hw_04/src/main/java/hw_04/Predicate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package hw_04;

public abstract class Predicate<A1> {
public static final Predicate<Object> ALWAYS_TRUE = new Predicate<Object>() {

@Override
public boolean test(Object arg) {
return true;
}

};

public static final Predicate<Object> ALWAYS_FALSE = new Predicate<Object>() {

@Override
public boolean test(Object arg) {
return false;
}

};

abstract public boolean test(A1 arg);

public <T extends A1 >Predicate<T> or(Predicate<? super T> other) {
return new Predicate<T>() {
@Override
public boolean test(T arg) {
return Predicate.this.test(arg) || other.test(arg);
}
};
}

public <T extends A1> Predicate<T> and(Predicate<? super T> other) {
return new Predicate<T>() {
@Override
public boolean test(T arg) {
return Predicate.this.test(arg) && other.test(arg);
}
};
}

public Predicate<A1> not() {
return new Predicate<A1>() {
@Override
public boolean test(A1 arg) {
return !Predicate.this.test(arg);
}
};
}
}
Loading