Skip to content

Type Interfaces : Foldable

johnmcclean-aol edited this page Nov 23, 2016 · 3 revisions

The Foldable interface

The Foldable interface represents a type that can be reduced by a combining function, potentially with additional parameters to a single value.

Note for terminating folds for infinite data structures see combine on [Traversable](https://github.com/aol/cyclops-react/wiki/Type-Interfaces-:-Traversable]

In cyclops-react it has the following methods

  • endsWith - returns true if the last value/s in the foldable match those supplied
  • startsWith - returns true if the first value/s in the foldable match those supplied
  • findAny - return any single value from the foldable, if present, Optional.empty otherwise
  • findFirst - return the first value from the foldable, if present, Optional.empty otherwise
  • firstValue - return the first value from the foldable, if present, NoSuchElementException otherwise
  • foldRight - reduce to a single value starting from the end
  • get - get the value at index
  • groupBy - group by a classifying function
  • join - reduce to a single String
  • mapReduce - perform a map operation before a reduction / foldLeft
  • print/printOut/printErr - write the contents of the foldable to a PrintOutputStream
  • reduce - reduce to a single value starting from the start. NB reduce is used everywhere rather than fold/foldLeft
  • schedule - emit values one at a time on a schedule to a HotStream
  • single - return the single value in this foldable, otherwise throw and Exception / replace with default if 0 or more than 1 element.
  • stream - convert foldable to a Stream
  • toConcurrentLazyCollection - lazily convert foldable to a Collection (materialized as needed on access) in a thread-safe manner
  • toLazyCollection - lazily convert foldable to a Collection (materialized as needed on access) without synchronization
  • validate - apply a validator to the values in this Function an recieve an Ior with a Stream of sucess and fail values

Example

Use a Validator to separate even and odd values (as a proxy for valid and invalid)

 ListX<Integer> numbers = ListX.of(1, 2, 3, 4, 5, 6, 7);
        Validator<Integer, Integer, Integer> validator = Validator.of(i -> i % 2 == 0, 1, 1);
        Ior<ReactiveSeq<Integer>, ReactiveSeq<Integer>> ior = numbers.validate(validator);
        int even = ior.get().sum().get();
        int odd = ior.secondaryGet().sum().get();
        assertThat(even, equalTo(3));
        assertThat(odd, equalTo(4));

Implementations

AnyM, AnyMSeq, AnyMValue,CollectionX, Combiner, DequeX, Eval, FeatureToggle, Ior<ST,PT>, IterableFoldable, IterableFunctor, LazyFutureStream, ListX, MapX<K,V>, Matchable.ValueAndOptionalMatcher, Maybe, PBagX, PersistentCollectionX, PMapX<K,V>, POrderedSetX, PQueueX, PSetX, PStackX, PVectorX, QueueX, ReactiveSeq, SetX, SortedSetX, Streamable, Trampoline, TransformerValue, Try<T,X>, Value, Xor<ST,PT>

Clone this wiki locally