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

javaslang.collection.Foldable Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC4
Show newest version
/*     / \____  _    _  ____   ______  / \ ____  __    _ _____
 *    /  /    \/ \  / \/    \ /  /\__\/  //    \/  \  / /  _  \   Javaslang
 *  _/  /  /\  \  \/  /  /\  \\__\\  \  //  /\  \ /\\/  \__/  /   Copyright 2014-now Daniel Dietrich
 * /___/\_/  \_/\____/\_/  \_/\__\/__/___\_/  \_//  \__/_____/    Licensed under the Apache License, Version 2.0
 */
package javaslang.collection;

import javaslang.control.Option;

import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.BiFunction;

/**
 * Interface of folable data structures.
 * 

* Example: * *


 * // = "123"
 * Stream.of("1", "2", "3").fold("", (a1, a2) -> a1 + a2);
 * 
* * @param Component type of this foldable * @author Daniel Dietrich * @since 2.0.0 */ public interface Foldable { /** * Folds this elements from the left, starting with {@code zero} and successively calling {@code combine}. * * @param zero A zero element to start with. * @param combine A function which combines elements. * @return a folded value * @throws NullPointerException if {@code combine} is null */ default T fold(T zero, BiFunction combine) { Objects.requireNonNull(combine, "fold combine is null"); return foldLeft(zero, combine); } /** * Folds this elements from the left, starting with {@code zero} and successively calling {@code combine}. * * @param the type to fold over * @param zero A zero element to start with. * @param combine A function which combines elements. * @return a folded value * @throws NullPointerException if {@code combine} is null */ U foldLeft(U zero, BiFunction combine); /** * Folds this elements from the right, starting with {@code zero} and successively calling {@code combine}. * * @param the type of the folded value * @param zero A zero element to start with. * @param combine A function which combines elements. * @return a folded value * @throws NullPointerException if {@code combine} is null */ U foldRight(U zero, BiFunction combine); /** * Accumulates the elements of this Foldable by successively calling the given operation {@code op}. * The order of element iteration is undetermined. * * @param op A BiFunction of type T * @return the reduced value. * @throws NoSuchElementException if this is empty * @throws NullPointerException if {@code op} is null */ default T reduce(BiFunction op) { Objects.requireNonNull(op, "op is null"); return reduceLeft(op); } /** * Accumulates the elements of this Foldable by successively calling the given operation {@code op}. * The order of element iteration is undetermined. * * @param op A BiFunction of type T * @return Some of reduced value or None if the Foldable is empty. * @throws NullPointerException if {@code op} is null */ default Option reduceOption(BiFunction op) { Objects.requireNonNull(op, "op is null"); return reduceLeftOption(op); } /** * Accumulates the elements of this Foldable by successively calling the given operation {@code op} from the left. * * @param op A BiFunction of type T * @return the reduced value. * @throws NoSuchElementException if this is empty * @throws NullPointerException if {@code op} is null */ T reduceLeft(BiFunction op); /** * Accumulates the elements of this Foldable by successively calling the given operation {@code op} from the left. * * @param op A BiFunction of type T * @return Some of reduced value or None if the Foldable is empty. * @throws NullPointerException if {@code op} is null */ Option reduceLeftOption(BiFunction op); /** * Accumulates the elements of this Foldable by successively calling the given operation {@code op} from the right. * * @param op An operation of type T * @return the reduced value. * @throws NoSuchElementException if this is empty * @throws NullPointerException if {@code op} is null */ T reduceRight(BiFunction op); /** * Accumulates the elements of this Foldable by successively calling the given operation {@code op} from the right. * * @param op An operation of type T * @return Some of reduced value or None. * @throws NullPointerException if {@code op} is null */ Option reduceRightOption(BiFunction op); }