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

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

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

import java.time.Duration;

public interface Effect extends Bindable, Applicable {
  
  @Override
   Effect map(Function1 mapper);

  @Override
   Effect ap(Kind> apply);

  @Override
   Effect flatMap(Function1> mapper);

  @Override
  default  Effect andThen(Kind next) {
    return flatMap(ignore -> next);
  }

  @Override
  default  Effect> zip(Kind other) {
    return zipWith(other, Tuple::of);
  }

  @Override
  default  Effect zipLeft(Kind other) {
    return zipWith(other, Function2.first());
  }

  @Override
  default  Effect zipRight(Kind other) {
    return zipWith(other, Function2.second());
  }

  @Override
  default  Effect zipWith(Kind other, Function2 mapper) {
    return narrowK(other).ap(this.map(mapper.curried()));
  }

  Effect> timed();

  default Effect repeat() {
    return repeat(1);
  }

  Effect repeat(int times);

  default Effect repeat(Duration delay) {
    return repeat(delay, 1);
  }

  Effect repeat(Duration delay, int times);

  default Effect retry() {
    return retry(1);
  }

  Effect retry(int maxRetries);

  default Effect retry(Duration delay) {
    return retry(delay, 1);
  }

  Effect retry(Duration delay, int maxRetries);
  
  @SuppressWarnings("unchecked")
  static  Effect narrowK(Kind kind) {
    return (Effect) kind;
  }
}