com.github.tonivade.purefun.instances.RIOInstances 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.instances;
import java.time.Duration;
import com.github.tonivade.purefun.Consumer1;
import com.github.tonivade.purefun.Function1;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.Producer;
import com.github.tonivade.purefun.Unit;
import com.github.tonivade.purefun.effect.UIO;
import com.github.tonivade.purefun.effect.RIO_;
import com.github.tonivade.purefun.effect.RIO;
import com.github.tonivade.purefun.effect.RIOOf;
import com.github.tonivade.purefun.typeclasses.Applicative;
import com.github.tonivade.purefun.typeclasses.Bracket;
import com.github.tonivade.purefun.typeclasses.Console;
import com.github.tonivade.purefun.typeclasses.Defer;
import com.github.tonivade.purefun.typeclasses.Functor;
import com.github.tonivade.purefun.typeclasses.Monad;
import com.github.tonivade.purefun.typeclasses.MonadDefer;
import com.github.tonivade.purefun.typeclasses.MonadError;
import com.github.tonivade.purefun.typeclasses.MonadThrow;
import com.github.tonivade.purefun.typeclasses.Reference;
import com.github.tonivade.purefun.typeclasses.Resource;
@SuppressWarnings("unchecked")
public interface RIOInstances {
static Functor> functor() {
return RIOFunctor.INSTANCE;
}
static Applicative> applicative() {
return RIOApplicative.INSTANCE;
}
static Monad> monad() {
return RIOMonad.INSTANCE;
}
static MonadThrow> monadThrow() {
return RIOMonadThrow.INSTANCE;
}
static MonadDefer> monadDefer() {
return RIOMonadDefer.INSTANCE;
}
static Reference, A> ref(A value) {
return Reference.of(monadDefer(), value);
}
static Resource, A> resource(RIO acquire) {
return resource(acquire, AutoCloseable::close);
}
static Resource, A> resource(RIO acquire, Consumer1 release) {
return Resource.from(monadDefer(), acquire, release);
}
static Console, Throwable>> console() {
return ConsoleRIO.INSTANCE;
}
}
interface RIOFunctor extends Functor> {
@SuppressWarnings("rawtypes")
RIOFunctor INSTANCE = new RIOFunctor() {};
@Override
default RIO
map(Kind, A> value, Function1 map) {
return RIOOf.narrowK(value).map(map);
}
}
interface RIOPure extends Applicative> {
@Override
default RIO pure(A value) {
return RIO.pure(value);
}
}
interface RIOApplicative extends RIOPure {
@SuppressWarnings("rawtypes")
RIOApplicative INSTANCE = new RIOApplicative() {};
@Override
default RIO
ap(Kind, A> value,
Kind, Function1> apply) {
return RIOOf.narrowK(apply).flatMap(map -> RIOOf.narrowK(value).map(map));
}
}
interface RIOMonad extends RIOPure, Monad> {
@SuppressWarnings("rawtypes")
RIOMonad INSTANCE = new RIOMonad() {};
@Override
default RIO
flatMap(Kind, A> value,
Function1, B>> map) {
return RIOOf.narrowK(value).flatMap(map.andThen(RIOOf::narrowK));
}
}
interface RIOMonadError extends RIOMonad, MonadError, Throwable> {
@SuppressWarnings("rawtypes")
RIOMonadError INSTANCE = new RIOMonadError © 2015 - 2025 Weber Informatics LLC | Privacy Policy