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

fj.data.State 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.*;

import java.util.*;

import static fj.P.p;

/**
 * Created by MarkPerry on 7/07/2014.
 */
public class State {

	private F> run;

	private State(F> f) {
		run = f;
	}

	public P2 run(S s) {
		return run.f(s);
	}

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

	public static  State units(F f) {
		return unit((S s) -> {
			S s2 = f.f(s);
			return p(s2, s2);
		});
	}

	public static  State constant(A a) {
		return unit(s -> p(s, a));
	}

	public  State map(F f) {
		return unit((S s) -> {
			P2 p2 = run(s);
			B b = f.f(p2._2());
			return p(p2._1(), b);
		});
	}

	public static  State modify(F f) {
		return State.init().flatMap(s -> unit(s2 -> p(f.f(s), Unit.unit())));
	}

	public  State mapState(F, P2> f) {
		return unit(s -> f.f(run(s)));
	}

	public static  State flatMap(State mb, F> f) {
		return mb.flatMap(f);
	}

	public  State flatMap(F> f) {
		return unit((S s) -> {
			P2 p = run(s);
			A a = p._2();
			S s2 = p._1();
			State smb = f.f(a);
			return smb.run(s2);
		});
	}

	public static  State init() {
		return unit(s -> p(s, s));
	}

	public State gets() {
		return unit(s -> {
			P2 p = run(s);
			S s2 = p._1();
			return p(s2, s2);
		});
	}

	public static  State put(S s) {
		return State.unit((S z) -> p(s, Unit.unit()));
	}

	public A eval(S s) {
		return run(s)._2();
	}

	public S exec(S s) {
		return run(s)._1();
	}

	public State withs(F f) {
		return unit(F1Functions.andThen(f, run));
	}

	public static  State gets(F f) {
		return State.init().map(s -> f.f(s));
	}

	/**
	 * Evaluate each action in the sequence from left to right, and collect the results.
	 */
	public static  State> sequence(List> list) {
		return list.foldLeft((State> acc, State ma) ->
			acc.flatMap((List xs) -> ma.map((A x) -> xs.snoc(x))
		), constant(List.nil()));
	}

	/**
	 * Map each element of a structure to an action, evaluate these actions from left to right
	 * and collect the results.
	 */
	public static  State> traverse(List list, F> f) {
		return list.foldLeft((State> acc, A a) ->
			acc.flatMap(bs -> f.f(a).map(b -> bs.snoc(b))
		), constant(List.nil()));
	}

}