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

com.iodesystems.fn.aspects.Pairs Maven / Gradle / Ivy

Go to download

Fn is a lazy Java Library that helps utilize some rudimentary functional concepts with more nounular objects

There is a newer version: 3.0.4
Show newest version
package com.iodesystems.fn.aspects;

import com.iodesystems.fn.data.From;
import com.iodesystems.fn.data.Pair;
import java.util.Iterator;

public class Pairs {

  public static  Iterable> pairs(Iterable contents, From aExtractor) {
    return Iterables.convert(contents, s -> Pair.of(aExtractor.from(s), s));
  }

  public static  Iterable> pairs(A a, B b, Object... rest) {

    return () ->
        new Iterator>() {
          Pair current = new Pair<>(a, b);
          int currentIndex = 0;

          @Override
          public boolean hasNext() {
            if (current != null) {
              return true;
            }
            return false;
          }

          @Override
          public Pair next() {
            Pair tmp = this.current;
            if (rest.length >= currentIndex + 2) {
              this.current = new Pair<>((A) rest[currentIndex++], (B) rest[currentIndex++]);
            } else {
              this.current = null;
            }
            return tmp;
          }
        };
  }

  public static  Iterable> pairs(
      Iterable contents, From aExtractor, From bExtractor) {
    return Iterables.convert(contents, s -> Pair.of(aExtractor.from(s), bExtractor.from(s)));
  }

  public static  Iterable> index(Iterable contents) {
    return () -> {
      final Iterator iterator = contents.iterator();
      return new Iterator>() {
        int index = 0;

        @Override
        public boolean hasNext() {
          return iterator.hasNext();
        }

        @Override
        public Pair next() {
          return Pair.of(index++, iterator.next());
        }
      };
    };
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy