Skip to content

Instantly share code, notes, and snippets.

@CaglarGonul
Last active December 15, 2015 20:48
Show Gist options
  • Save CaglarGonul/5320954 to your computer and use it in GitHub Desktop.
Save CaglarGonul/5320954 to your computer and use it in GitHub Desktop.
Playing with closures in Java7. Thanks to Prof. Grossman.
package com.fs;
/**
* This interface is responsible for holding the closures when it comes to map.
* It uses two generic types. One for the argument and one for the return type.
* @param <B> Generic type
* @param <A> Generic type
*/
public interface Func<B,A> {
/**
* Function prototype m takes an argument of type A and returns a type B.
* A map operation can produce a different type.
* @param x of type A
* @return type B
*/
B m(A x);
}
package com.fs;
/**
* This interface is responsible for holding the closures when it comes to filter.
* @param <A> Generic type
*/
public interface Pred<A> {
/**
* Function prototype m takes an argument of type A and returns a boolean.
* A filter operation checks every element if it fits a predicate.
* @param x of type A
* @return boolean
*/
boolean m(A x);
}
package com.impl.list;
import com.fs.*;
import java.util.Stack;
public class CList<T> {
T head;
CList<T> tail;
public CList(T x, CList<T> xs){
head = x;
tail = xs;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
CList<T> xs = this;
while(xs != null){
sb.append(xs.head.toString()).append(" ");
xs=xs.tail;
}
return sb.toString();
}
static <A,B> CList<B> map(Func<B,A> f, CList<A> xs){
if(xs==null){
return null;
}
return new CList<>(f.m(xs.head),map(f,xs.tail));
}
static <A,B> CList<B> maploop(Func<B,A> f, CList<A> xs){
Stack<CList<A>> stack = new Stack<>();
while(xs != null){
stack.push(xs);
xs = xs.tail;
}
CList<B> result = null;
while(!stack.empty()){
xs = stack.pop();
result = new CList<>(f.m(xs.head),result);
}
return result;
}
static <A> CList<A> filter(Pred<A> f, CList<A> xs){
if(xs == null){
return null;
}
if(f.m(xs.head)){
return new CList<>(xs.head, filter(f,xs.tail));
}
return filter(f,xs.tail);
}
static <A> int length(CList<A> xs){
int ans =0;
while(xs!= null){
++ans;
xs=xs.tail;
}
return ans;
}
}
package com.impl.list;
import com.fs.Func;
import com.fs.Pred;
public class CListClient {
public static CList<Integer> doubleAll(CList<Integer> xs){
Func<Integer, Integer> df = new Func<Integer, Integer>() {
@Override
public Integer m(Integer x) {
return x * 2;
}
};
return CList.map(df, xs);
}
public static int countNs(CList<Integer> xs,final int n){
Pred<Integer> pf = new Pred<Integer>() {
@Override
public boolean m(Integer x) {
return x==n;
}
};
return CList.length(CList.filter(pf, xs));
}
public static CList<Integer> doubleAllloop(CList<Integer> xs){
Func<Integer, Integer> df = new Func<Integer, Integer>() {
@Override
public Integer m(Integer x) {
return x * 2;
}
};
return CList.maploop(df, xs);
}
}
package basic;
import com.impl.list.CList;
import com.impl.list.CListClient;
import org.junit.Test;
public class ListTester {
CList<Integer> intlist_1 = new CList<>(new Integer(1),null);
CList<Integer> intlist_2 = new CList<>(new Integer(2),intlist_1);
CList<Integer> intlist_3 = new CList<>(new Integer(3),intlist_2);
CList<Integer> intlist_4 = new CList<>(new Integer(4),intlist_3);
CList<Integer> intlist_5 = new CList<>(new Integer(4),intlist_4);
CList<Integer> intlist = new CList<>(new Integer(5),intlist_5);
@Test
public void test_doubleAll(){
CList<Integer> doubled = CListClient.doubleAll(intlist);
System.out.println("doubled : " + doubled.toString());
CList<Integer> doubledloop = CListClient.doubleAllloop(intlist);
System.out.println("doubledloop : " + doubledloop.toString());
}
@Test
public void test_CountNs(){
int count3s = CListClient.countNs(intlist, 3);
System.out.println("count3s : " + count3s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment