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

cyclops.companion.vavr.VavrSemigroups Maven / Gradle / Ivy

The newest version!
package cyclops.companion.vavr;

import com.aol.cyclops2.types.Zippable;
import cyclops.function.Semigroup;
import io.vavr.collection.*;
import io.vavr.collection.HashSet;
import io.vavr.collection.LinkedHashSet;
import io.vavr.collection.List;
import io.vavr.collection.Queue;
import io.vavr.collection.Set;
import io.vavr.collection.TreeSet;
import io.vavr.collection.Vector;
import io.vavr.concurrent.Future;
import io.vavr.control.Either;
import io.vavr.control.Option;
import io.vavr.control.Try;

import java.util.*;
import java.util.function.BiFunction;


/**
 *
 * A static class with a large number of Semigroups  or Combiners.
 *
 * A semigroup is an Object that can be used to combine objects of the same type.
 *
 *  @author johnmcclean
 */
public interface VavrSemigroups {

    /**
     * To manage javac type inference first assign the semigroup
     * 
     * {@code
     *
     *    Semigroup> listX = VavrSemigroups.linearSeqConcat();
     *    Semigroup> streamX = Semigroups.linearSeqConcat();
     *
     *
     *
     * }
     * 
* @return A Semigroup that can combine any vavr Set type */ static > Semigroup linearSeqConcat() { return (a, b) -> (C) a.appendAll(b); } static > Semigroup indexedSeqConcat() { return (a, b) -> (C) a.appendAll(b); } static > Semigroup setConcat() { return (a, b) -> (C) a.addAll(b); } static Semigroup> listConcat() { return VavrSemigroups.linearSeqConcat(); } static Semigroup> vectorConcat() { return VavrSemigroups.indexedSeqConcat(); } static Semigroup charSeqConcat() { return VavrSemigroups.indexedSeqConcat(); } static Semigroup> arrayConcat() { return VavrSemigroups.indexedSeqConcat(); } static Semigroup> streamConcat() { return VavrSemigroups.linearSeqConcat(); } static Semigroup> queueConcat() { return VavrSemigroups.linearSeqConcat(); } static Semigroup> linkedHashSetConcat() { return VavrSemigroups.setConcat(); } static Semigroup> hashSetConcat() { return VavrSemigroups.setConcat(); } static Semigroup> treeSetConcat() { return VavrSemigroups.setConcat(); } /** * @return Combination of two Collection, first non-empty is returned */ static > Semigroup firstNonEmptySeq() { return (a, b) -> a.isEmpty() ? b: a; } /** * @return Combination of two Collection, first non-empty is returned */ static > Semigroup firstNonEmptySet() { return (a, b) -> a.isEmpty() ? b: a; } /** * @return Combination of two Collection, last non-empty is returned */ static > Semigroup lastNonEmptySeq() { return (a, b) -> b.isEmpty() ? a: b; } /** * @return Combination of two Collection, last non-empty is returned */ static > Semigroup lastNonEmptySet() { return (a, b) -> b.isEmpty() ? a: b; } /** * @return Combine two Future's by taking the first result */ static Semigroup> firstCompleteFuture() { return (a, b) -> Futures.anyOf(a,b); } /** * @return Combine two Future's by taking the first successful */ static Semigroup> firstSuccessfulFuture() { return (a, b) -> Futures.firstSuccess(a,b); } /** * @return Combine two Either's by taking the first primary */ static Semigroup> firstRightEither() { return (a, b) -> a.isRight() ? a : b; } /** * @return Combine two Either's by taking the first secondary */ static Semigroup> firstLeftEither() { return (a, b) -> a.isLeft() ? a : b; } /** * @return Combine two Either's by taking the last primary */ static Semigroup> lastRightEither() { return (a, b) -> b.isRight() ? b : a; } /** * @return Combine two Either's by taking the last secondary */ static Semigroup> lastLeftEither() { return (a, b) -> b.isLeft() ? b : a; } /** * @return Combine two Try's by taking the first primary */ static Semigroup> firstTrySuccess() { return (a, b) -> a.isSuccess() ? a : b; } /** * @return Combine two Try's by taking the first secondary */ static Semigroup> firstTryFailure() { return (a, b) -> a.isFailure() ? a : b; } /** * @return Combine two Tryr's by taking the last primary */ static Semigroup> lastTrySuccess() { return (a, b) -> b.isSuccess() ? b : a; } /** * @return Combine two Try's by taking the last secondary */ static Semigroup>lastTryFailure() { return (a, b) -> b.isFailure() ? b : a; } /** * @return Combine two Options by taking the first present */ static Semigroup> firstPresentOption() { return (a, b) -> a.isDefined() ? a : b; } /** * @return Combine two Options by taking the last present */ static Semigroup> lastPresentOption() { return (a, b) -> b.isDefined() ? b : a; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy