Skip to content

Instantly share code, notes, and snippets.

@davetron5000
Created January 20, 2013 15:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davetron5000/4579399 to your computer and use it in GitHub Desktop.
Save davetron5000/4579399 to your computer and use it in GitHub Desktop.
More functional version of the take 25 squares integers thing
import java.util.*;
public class Functional {
public static void main(String args[]) {
System.out.println(squaresOf(integers()).take(25).toList());
}
private static List<Integer> take(int numToTake, RichIterable<Integer> seq) {
List<Integer> results = new ArrayList<Integer>();
Iterator<Integer> iterator = seq.iterator();
for(int i=0;i<numToTake;i++) {
if (iterator.hasNext()) {
results.add(iterator.next());
}
}
return results;
}
private static RichIterable<Integer> squaresOf(final RichIterable<Integer> seq) {
return new RichIterable<Integer>() {
public Iterator<Integer> iterator() {
Function1<Integer,Integer> square = new Function1<Integer,Integer>() {
public Integer apply(Integer i) {
return i * i;
}
};
return new TransformingIterator<Integer,Integer>(square, seq.iterator());
}
};
}
private static RichIterable<Integer> integers() {
return new RichIterable<Integer>() {
public Iterator<Integer> iterator() {
return new IntegersIterator();
}
};
}
private abstract static class RichIterable<T> implements Iterable<T> {
public static <T> RichIterable<T> fromIterable(Iterable<T> seq) {
return new ProxyRichIterable<T>(seq);
}
public RichIterable<T> take(int numToTake) {
List<T> results = new ArrayList<T>();
Iterator<T> iterator = this.iterator();
for(int i=0;i<numToTake;i++) {
if (iterator.hasNext()) {
results.add(iterator.next());
}
}
return fromIterable(results);
}
public List<T> toList() {
List<T> list = new ArrayList<T>();
for(T t: this) {
list.add(t);
}
return list;
}
}
private static class ProxyRichIterable<T> extends RichIterable<T> {
private Iterable<T> seq;
public ProxyRichIterable(Iterable<T> seq) {
this.seq = seq;
}
public Iterator<T> iterator() {
return seq.iterator();
}
}
private static interface Function1<A,R> {
public R apply(A a);
}
private static class TransformingIterator<A,R> implements Iterator<R> {
private Iterator<A> original;
private Function1<A,R> function;
public TransformingIterator(Function1<A,R> function, Iterator<A> iter) {
this.original = iter;
this.function = function;
}
public boolean hasNext() {
return this.original.hasNext();
}
public R next() {
A value = this.original.next();
return this.function.apply(value);
}
public void remove() {
original.remove();
}
}
private static class IntegersIterator implements Iterator<Integer> {
private int nextInt = 0;
public boolean hasNext() {
return nextInt < Integer.MAX_VALUE;
}
public Integer next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
nextInt++;
return nextInt - 1;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment