All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.shaftesbury.utils.functional.IterableHelper Maven / Gradle / Ivy

There is a newer version: 1.17
Show newest version
package me.shaftesbury.utils.functional;

import org.javatuples.Triplet;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Bob
 * Date: 29/11/13
 * Time: 17:03
 * To change this template use File | Settings | File Templates.
 */
public class IterableHelper
{
    public static final Iterable2 create(final java.lang.Iterable it)
    {
        return new Iterable2()
        {
            private final java.lang.Iterable i = it;
            @Override
            public Iterator iterator() { return i.iterator(); }

            public final Iterable2 filter(final Func f) { return create(Functional.seq.filter(f,i)); }
            public final Iterable2 map(final Func f) { return create(Functional.seq.map(f, i)); }
            public final Iterable2 choose(final Func> f) { return create(Functional.seq.choose(f, i)); }
            public final boolean exists(final Func f) { return Functional.exists(f, i); }
            public final boolean forAll(final Func f) { return Functional.forAll(f, i); }
            public final U fold(final Func2 f, final U seed) { return Functional.fold(f, seed, i); }
            public final List toList() { return Functional.toList(i); }
            public final Iterable2 sortWith(final Comparator f) { return create(Functional.sortWith(f,toList())); }
            public final Iterable2 concat(final Iterable2 list2) { return create(Functional.seq.concat(i,list2));}
            public final T find(final Func f) { return Functional.find(f,i);}
            public int findIndex(final Func f) { return Functional.findIndex(f,i);}
            public final B pick(final Func> f){return Functional.pick(f,i);}
            public final Iterable2 take(final int howMany){return create(Functional.take(howMany, i));}

            public final Iterable2> zip(final Iterable2 l2) { return create(Functional.zip(i,l2));}
            //public final org.javatuples.Pair,List> unzip(){return Functional.unzip(i);}
            public final Iterable2> zip3(final Iterable l2, final Iterable l3){return create(Functional.zip3(i,l2,l3));}

            public final Iterable2 collect(final Func> f){return create(Functional.collect(f,i));}
            public U in(final Func, U> f){ return f.apply(this); }
        };
    }

    public static final Iterable2 init(Func f, int howMany) { return create(Functional.seq.init(f, howMany)); }
    public final static Iterable2 init(final Func f) { return create(Functional.seq.init(f));}
    @SafeVarargs
    public static Iterable2 asList(T... a) { return create(Arrays.asList(a)); }
}