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

fj.data.fingertrees.Node 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.fingertrees;

import fj.F;
import fj.F2;
import fj.P2;
import fj.P3;
import fj.data.Option;

import static fj.Function.curry;

/**
 * An inner node of the 2-3 tree.
 */
public abstract class Node {
  private final Measured m;
  private final V measure;

  public abstract  B foldRight(final F> f, final B z);

  public abstract  B foldLeft(final F> f, final B z);

  public static  F, B>> foldLeft_(final F> bff) {
    return curry(new F2, B>() {
      public B f(final B b, final Node node) { return node.foldLeft(bff, b); }
    });
  }

  public static  F, B>> foldRight_(final F> aff) {
    return curry(new F2, B>() {
      public B f(final B b, final Node node) { return node.foldRight(aff, b); }
    });
  }

  public final  Node map(final F f, final Measured m) {
    return match(new F, Node>() {
      public Node f(final Node2 node2) {
        return new Node2(m, node2.toVector().map(f));
      }
    }, new F, Node>() {
      public Node f(final Node3 node3) {
        return new Node3(m, node3.toVector().map(f));
      }
    });
  }

  public static  F, Node> liftM(final F f, final Measured m) {
    return new F, Node>() {
      public Node f(final Node node) {
        return node.map(f, m);
      }
    };
  }

  public abstract Digit toDigit();

  Node(final Measured m, final V measure) {
    this.m = m;
    this.measure = measure;
  }

  public final V measure() {
    return measure;
  }

  Measured measured() {
    return m;
  }

  abstract P3>, A, Option>> split1(final F predicate, final V acc);

  public abstract P2 lookup(final F o, final int i);

  public abstract  B match(final F, B> n2, final F, B> n3);
}