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

com.jnape.palatable.lambda.functions.builtin.fn2.Map Maven / Gradle / Ivy

There is a newer version: 5.5.0
Show newest version
package com.jnape.palatable.lambda.functions.builtin.fn2;

import com.jnape.palatable.lambda.functions.Fn1;
import com.jnape.palatable.lambda.functions.Fn2;
import com.jnape.palatable.lambda.iterators.MappingIterator;

import java.util.function.Function;

/**
 * Lazily apply a function to each element in an Iterable, producing an Iterable of the mapped
 * results.
 *
 * @param  A type contravariant to the input Iterable element type
 * @param  A type covariant to the output Iterable element type
 */
public final class Map implements Fn2, Iterable, Iterable> {

    private Map() {
    }

    @Override
    public Iterable apply(Function fn, Iterable as) {
        return () -> new MappingIterator<>(fn, as.iterator());
    }

    public static  Map map() {
        return new Map<>();
    }

    public static  Fn1, Iterable> map(Function fn) {
        return Map.map().apply(fn);
    }

    public static  Iterable map(Function fn, Iterable as) {
        return Map.map(fn).apply(as);
    }
}