com.github.tonivade.purefun.monad.Reader Maven / Gradle / Ivy
/*
* Copyright (c) 2018-2023, 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;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.Bindable;
@HigherKind
@FunctionalInterface
public non-sealed interface Reader extends ReaderOf, Bindable, A> {
A eval(R reader);
@Override
default Reader map(Function1 super A, ? extends B> mapper) {
return reader -> mapper.apply(eval(reader));
}
@Override
default Reader flatMap(Function1 super A, ? extends Kind, ? extends B>> mapper) {
return reader -> mapper.andThen(ReaderOf::narrowK).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 super R, ? extends A> run) {
return run::apply;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy