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

jio.Lambda Maven / Gradle / Ivy

The newest version!
package jio;

import static java.util.Objects.requireNonNull;

import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * Represents a function that takes an input and produces an IO effect.
 *
 * @param   the type of the input
 * @param  the type of the effect
 */
public interface Lambda extends Function> {

  /**
   * Composes this Lambda with another Lambda, producing a new Lambda. The resulting Lambda, when applied to an input,
   * will execute this Lambda followed by the other Lambda, creating a sequence of effects.
   *
   * @param  the type of the result produced by the other Lambda
   * @param other         the other Lambda to be executed after this Lambda
   * @return a new Lambda that represents the composed effects
   */
  default  Lambda then(final Lambda other) {
    Objects.requireNonNull(other);
    return i -> this.apply(i)
                    .then(other);
  }

  /**
   * Transforms a Predicate into a Lambda, producing boolean effects.
   *
   * @param    the type of the parameter of the predicate
   * @param predicate the predicate to be transformed
   * @return a Lambda that produces boolean effects
   */
  static  Lambda liftPredicate(final Predicate predicate) {
    requireNonNull(predicate);
    return input -> {
      try {
        return IO.succeed(predicate.test(input));
      } catch (Exception e) {
        return IO.fail(e);
      }
    };
  }

  /**
   * Transforms a Function into a Lambda, producing effects of type O.
   *
   * @param   the type of the function's input parameter
   * @param  the type of the function's output
   * @param fn       the function to be transformed
   * @return a Lambda that produces effects of type O
   */
  static  Lambda liftFunction(final Function fn) {
    requireNonNull(fn);
    return o -> {
      try {
        return IO.succeed(fn.apply(o));
      } catch (Exception e) {
        return IO.fail(e);
      }
    };
  }

  /**
   * Composes this Lambda with a mapping function that transforms the output of the inner IO operation.
   *
   * 

This method allows you to apply a function to transform or manipulate the result of the IO operation.

* * @param map A function that takes an {@code IO} and returns an {@code IO}. * @param The type of the result after applying the mapping function. * @return A new Lambda that represents the composition of this Lambda and the provided mapping function. * @throws NullPointerException If the mapping function {@code map} is {@code null}. * @since 1.0 */ default Lambda map(Function, IO> map) { return input -> map.apply(this.apply(input)); } /** * Composes this Lambda with a mapping function that transforms the successful output of the inner IO operation. * *

This method allows you to apply a function to transform or manipulate the successful result of the IO * operation.

* * @param mapSuccess A function that takes an {@code Output} and returns a {@code MappedOutput}. * @param The type of the result after applying the mapping function. * @return A new Lambda that represents the composition of this Lambda and the provided success mapping function. * @since 1.0 */ default Lambda mapSuccess(Function mapSuccess) { return input -> this.apply(input) .map(mapSuccess); } /** * Composes this Lambda with a mapping function that transforms the failure of the inner IO operation. * *

This method allows you to apply a function to transform or handle the failure of the IO operation.

* * @param mapFailure A function that takes a {@code Throwable} and returns a transformed {@code Throwable}. * @return A new Lambda that represents the composition of this Lambda and the provided failure mapping function. * @since 1.0 */ default Lambda mapFailure(Function mapFailure) { return input -> this.apply(input) .mapFailure(mapFailure); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy