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

com.github.tonivade.purefun.Function2 Maven / Gradle / Ivy

/*
 * Copyright (c) 2018-2019, Antonio Gabriel Muñoz Conejo 
 * Distributed under the terms of the MIT License
 */
package com.github.tonivade.purefun;

import com.github.tonivade.purefun.type.Either;
import com.github.tonivade.purefun.type.Option;
import com.github.tonivade.purefun.type.Try;

@FunctionalInterface
public interface Function2 {

  R apply(A a, B b);

  default Function1> curried() {
    return a -> b -> apply(a, b);
  }

  default Function1, R> tupled() {
    return tuple -> apply(tuple.get1(), tuple.get2());
  }

  default  Function2 andThen(Function1 after) {
    return (a, b) -> after.apply(apply(a, b));
  }

  default  Function1 compose(Function1 beforeT, Function1 beforeV) {
    return value -> apply(beforeT.apply(value), beforeV.apply(value));
  }

  default Function2> liftTry() {
    return (a, b) -> Try.of(() -> apply(a, b));
  }

  default Function2> liftEither() {
    return liftTry().andThen(Try::toEither);
  }

  default Function2> liftOption() {
    return liftTry().andThen(Try::toOption);
  }

  default Function2 memoized() {
    return (a, b) -> new MemoizedFunction<>(tupled()).apply(Tuple.of(a, b));
  }

  static  Function2 of(Function2 reference) {
    return reference;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy