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

com.github.tonivade.purefun.monad.Reader Maven / Gradle / Ivy

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

import com.github.tonivade.purefun.Function1;
import com.github.tonivade.purefun.HigherKind;

@HigherKind
@FunctionalInterface
public interface Reader extends ReaderOf {

  A eval(R reader);

  default  Reader map(Function1 mapper) {
    return reader -> mapper.apply(eval(reader));
  }

  default  Reader flatMap(Function1> mapper) {
    return reader -> mapper.apply(eval(reader)).eval(reader);
  }

  default  Reader andThen(Reader next) {
    return flatMap(ignore -> next);
  }

  static  Reader env() {
    return reader -> reader;
  }

  static  Reader pure(A value) {
    return reader -> value;
  }

  static  Reader reader(Function1 run) {
    return run::apply;
  }
}