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

fj.data.Reader Maven / Gradle / Ivy

Go to download

Functional Java is an open source library that supports closures for the Java programming language

There is a newer version: 5.0
Show newest version
package fj.data;

import fj.F;
import fj.F1Functions;

/**
 * The Reader monad (also called the function monad, so equivalent to the idea of F).
 * Created by MarkPerry on 7/07/2014.
 */
public class Reader {

	private final F function;

	public Reader(F f) {
		function = f;
	}

	public final F getFunction() {
		return function;
	}

	public static  Reader unit(F f) {
		return new Reader<>(f);
	}

	public static  Reader constant(B b) {
		return unit(a -> b);
	}

	public final B f(A a) {
		return function.f(a);
	}

	public final  Reader map(F f) {
		return unit(F1Functions.andThen(function, f));
	}

	public final  Reader andThen(F f) {
		return map(f);
	}

	public final  Reader flatMap(F> f) {
		return unit(a -> f.f(function.f(a)).f(a));
	}

	public final  Reader bind(F> f) {
		return flatMap(f);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy