com.github.tonivade.purefun.instances.ProducerInstances Maven / Gradle / Ivy
/*
* Copyright (c) 2018-2022, Antonio Gabriel Muñoz Conejo
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.purefun.instances;
import com.github.tonivade.purefun.Function1;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.Producer;
import com.github.tonivade.purefun.ProducerOf;
import com.github.tonivade.purefun.Producer_;
import com.github.tonivade.purefun.typeclasses.Applicative;
import com.github.tonivade.purefun.typeclasses.Comonad;
import com.github.tonivade.purefun.typeclasses.Functor;
import com.github.tonivade.purefun.typeclasses.Monad;
public interface ProducerInstances {
static Functor functor() {
return ProducerFunctor.INSTANCE;
}
static Applicative applicative() {
return ProducerApplicative.INSTANCE;
}
static Monad monad() {
return ProducerMonad.INSTANCE;
}
static Comonad comonad() {
return ProducerComonad.INSTANCE;
}
}
interface ProducerFunctor extends Functor {
ProducerFunctor INSTANCE = new ProducerFunctor() {};
@Override
default Kind map(Kind value, Function1 super T, ? extends R> mapper) {
return value.fix(ProducerOf::narrowK).map(mapper);
}
}
interface ProducerPure extends Applicative {
@Override
default Kind pure(T value) {
return Producer.cons(value);
}
}
interface ProducerApplicative extends ProducerPure {
ProducerApplicative INSTANCE = new ProducerApplicative() {};
@Override
default Kind ap(Kind value,
Kind> apply) {
return ProducerOf.narrowK(value).flatMap(t -> ProducerOf.narrowK(apply).map(f -> f.apply(t)));
}
}
interface ProducerMonad extends ProducerPure, Monad {
ProducerMonad INSTANCE = new ProducerMonad() {};
@Override
default Kind flatMap(Kind value, Function1 super T, ? extends Kind> mapper) {
return value.fix(ProducerOf::narrowK).flatMap(mapper.andThen(ProducerOf::narrowK));
}
}
interface ProducerComonad extends ProducerFunctor, Comonad {
ProducerComonad INSTANCE = new ProducerComonad() {};
@Override
default Kind coflatMap(Kind value, Function1 super Kind, ? extends B> map) {
return Producer.cons(map.apply(value));
}
@Override
default A extract(Kind value) {
return value.fix(ProducerOf::narrowK).get();
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy