com.github.tonivade.purefun.instances.URIOInstances 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.URIO_;
import com.github.tonivade.purefun.effect.URIO;
import com.github.tonivade.purefun.effect.URIOOf;
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 URIOInstances {
static Functor> functor() {
return URIOFunctor.INSTANCE;
}
static Applicative> applicative() {
return URIOApplicative.INSTANCE;
}
static Monad> monad() {
return URIOMonad.INSTANCE;
}
static MonadThrow> monadThrow() {
return URIOMonadThrow.INSTANCE;
}
static MonadDefer> monadDefer() {
return URIOMonadDefer.INSTANCE;
}
static Reference, A> ref(A value) {
return Reference.of(monadDefer(), value);
}
static Resource, A> resource(URIO acquire) {
return resource(acquire, AutoCloseable::close);
}
static Resource, A> resource(URIO acquire, Consumer1 release) {
return Resource.from(monadDefer(), acquire, release);
}
static Console, Throwable>> console() {
return ConsoleURIO.INSTANCE;
}
}
interface URIOFunctor extends Functor> {
@SuppressWarnings("rawtypes")
URIOFunctor INSTANCE = new URIOFunctor() {};
@Override
default URIO
map(Kind, A> value, Function1 map) {
return URIOOf.narrowK(value).map(map);
}
}
interface URIOPure extends Applicative> {
@Override
default URIO pure(A value) {
return URIO.pure(value);
}
}
interface URIOApplicative extends URIOPure {
@SuppressWarnings("rawtypes")
URIOApplicative INSTANCE = new URIOApplicative() {};
@Override
default URIO
ap(Kind, A> value,
Kind, Function1> apply) {
return URIOOf.narrowK(apply).flatMap(map -> URIOOf.narrowK(value).map(map));
}
}
interface URIOMonad extends URIOPure, Monad> {
@SuppressWarnings("rawtypes")
URIOMonad INSTANCE = new URIOMonad() {};
@Override
default URIO
flatMap(Kind, A> value,
Function1, B>> map) {
return URIOOf.narrowK(value).flatMap(map.andThen(URIOOf::narrowK));
}
}
interface URIOMonadError extends URIOMonad, MonadError, Throwable> {
@SuppressWarnings("rawtypes")
URIOMonadError INSTANCE = new URIOMonadError © 2015 - 2025 Weber Informatics LLC | Privacy Policy