Skip to content

Instantly share code, notes, and snippets.

@ckirkendall
Last active December 21, 2015 05:39
Show Gist options
  • Save ckirkendall/6258613 to your computer and use it in GitHub Desktop.
Save ckirkendall/6258613 to your computer and use it in GitHub Desktop.
package org.cinjug;
import java.util.Iterator;
interface Seq<T> {
public T next() throws EmptySequenceException;
public T peek() throws EmptySequenceException;
public boolean hasNext();
public default Seq<T> filter(Predicate<T> p){
return new FilterSeq<>(this, p);
}
public default <U> Seq<U> map(Function<T, U> p){
return new MapSeq<>(this, p);
}
public default <U> U reduce(U init, Reducer<T,U> p){
U cur=init;
while(this.hasNext()){
cur=p.apply(cur, this.next());
}
return cur;
}
public default void forEach(Consumer<T> c){
while(this.hasNext()){
c.apply(this.next());
}
}
}
interface Sequenceable<T> extends Iterable<T> {
public default Seq<T> seq(){
return new IteratorSeq<T>(this.iterator());
}
}
class IteratorSeq<T> implements Seq<T> {
T cur=null;
Iterator<T> it;
public IteratorSeq(Iterator<T> it){
if(it.hasNext())
cur=it.next();
this.it=it;
}
public T next() throws EmptySequenceException{
if(cur==null){
throw new EmptySequenceException();
}else{
T tcur = cur;
cur=null;
if(it.hasNext()){
cur = it.next();
}
return tcur;
}
}
public T peek() throws EmptySequenceException{
if(this.hasNext()){
return cur;
}
throw new EmptySequenceException();
}
public boolean hasNext(){
if(cur!=null)
return true;
else
return false;
}
}
class FilterSeq<T> implements Seq<T>{
public Predicate<T> pred;
public Seq<T> delegate;
public FilterSeq(Seq<T> d, Predicate<T> p){
this.pred=p;
this.delegate =d;
}
public T next() throws EmptySequenceException{
if(this.hasNext()){
return delegate.next();
}
throw new EmptySequenceException();
}
public T peek() throws EmptySequenceException{
if(this.hasNext()){
return delegate.peek();
}
throw new EmptySequenceException();
}
public boolean hasNext(){
while(delegate.hasNext()){
if(pred.test(delegate.peek())){
return true;
}
delegate.next();
}
return false;
}
}
class MapSeq<T,U> implements Seq<U>{
public Function<T,U> func;
public Seq<T> deligate;
public MapSeq(Seq<T> d, Function<T,U> p){
this.func=p;
this.deligate=d;
}
public U next() throws EmptySequenceException{
return func.apply(deligate.next());
}
public U peek() throws EmptySequenceException{
return func.apply(deligate.peek());
}
public boolean hasNext(){
return deligate.hasNext();
}
}
interface Predicate<T> {
public boolean test(T item);
}
interface Function<T,U> {
public U apply(T item);
}
interface Reducer<T,U> {
public U apply(U init, T item);
}
interface Consumer<T> {
public void apply(T item);
}
interface Block {
public void apply();
}
class EmptySequenceException extends RuntimeException {}
package org.cinjug;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Created with IntelliJ IDEA.
* User: ckirkendall
* Date: 8/17/13
* Time: 10:30 AM
* To change this template use File | Settings | File Templates.
*/
public class SeqTest {
public static void time(Block b){
long startTime=System.currentTimeMillis();
b.apply();
System.out.println((System.currentTimeMillis()-startTime));
}
public static void main(String args[]){
ArrayListSeq<String> lst =new ArrayListSeq<>();
Random rand=new Random();
Stream.generate(() -> rand.nextInt(1000))
.map((Integer a) -> {
if(a % 3 == 0)
return "this is a band";
else
return "this is a test";
})
.limit(1000000)
.forEach(lst::add);
lst.add("I like band-aids");
time(()-> {
int maxLen = 0;
for(String item : lst){
if(item.contains("band")){
maxLen=Math.max(maxLen, item.length());
}
}
System.out.print(maxLen+":");
});
time(()-> {
int maxLen = lst.seq()
.filter(p -> p.contains("band"))
.map(String::length)
.reduce(0, Math::max);
System.out.print(maxLen+":");
});
time(()-> {
int maxLen = lst.stream().parallel()
.filter(p -> p.contains("band"))
.map(String::length)
.reduce(0, Math::max);
System.out.print(maxLen+":");
});
}
}
class ArrayListSeq<T> extends ArrayList<T> implements Sequenceable<T>{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment