com.github.tonivade.purefun.instances.IdInstances 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 static com.github.tonivade.purefun.type.IdOf.toId;
import com.github.tonivade.purefun.Eq;
import com.github.tonivade.purefun.Function1;
import com.github.tonivade.purefun.Function2;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.Witness;
import com.github.tonivade.purefun.type.Eval;
import com.github.tonivade.purefun.type.EvalOf;
import com.github.tonivade.purefun.type.Id;
import com.github.tonivade.purefun.type.IdOf;
import com.github.tonivade.purefun.type.Id_;
import com.github.tonivade.purefun.typeclasses.Applicative;
import com.github.tonivade.purefun.typeclasses.Comonad;
import com.github.tonivade.purefun.typeclasses.Foldable;
import com.github.tonivade.purefun.typeclasses.Functor;
import com.github.tonivade.purefun.typeclasses.Monad;
import com.github.tonivade.purefun.typeclasses.Traverse;
public interface IdInstances {
static Eq> eq(Eq idEq) {
return (a, b) -> idEq.eqv(IdOf.narrowK(a).get(), IdOf.narrowK(b).get());
}
static Functor functor() {
return IdFunctor.INSTANCE;
}
static Applicative applicative() {
return IdApplicative.INSTANCE;
}
static Monad monad() {
return IdMonad.INSTANCE;
}
static Comonad comonad() {
return IdComonad.INSTANCE;
}
static Foldable foldable() {
return IdFoldable.INSTANCE;
}
static Traverse traverse() {
return IdTraverse.INSTANCE;
}
}
interface IdFunctor extends Functor {
IdFunctor INSTANCE = new IdFunctor() {};
@Override
default Kind map(Kind value, Function1 super T, ? extends R> map) {
return IdOf.narrowK(value).map(map);
}
}
interface IdPure extends Applicative {
@Override
default Kind pure(T value) {
return Id.of(value);
}
}
interface IdApplicative extends IdPure {
IdApplicative INSTANCE = new IdApplicative() {};
@Override
default Kind ap(Kind value,
Kind> apply) {
return IdOf.narrowK(value).flatMap(t -> IdOf.narrowK(apply).map(f -> f.apply(t)));
}
}
interface IdMonad extends IdPure, Monad {
IdMonad INSTANCE = new IdMonad() {};
@Override
default Kind flatMap(Kind value, Function1 super T, ? extends Kind> map) {
return IdOf.narrowK(value).flatMap(map.andThen(IdOf::narrowK));
}
}
interface IdComonad extends IdFunctor, Comonad {
IdComonad INSTANCE = new IdComonad() {};
@Override
default Kind coflatMap(Kind value, Function1 super Kind, ? extends B> map) {
return Id.of(map.apply(value));
}
@Override
default A extract(Kind value) {
return IdOf.narrowK(value).get();
}
}
interface IdFoldable extends Foldable {
IdFoldable INSTANCE = new IdFoldable() {};
@Override
default B foldLeft(Kind value, B initial, Function2 super B, ? super A, ? extends B> mapper) {
return mapper.apply(initial, value.fix(toId()).get());
}
@Override
default Eval foldRight(Kind value, Eval extends B> initial, Function2 super A, ? super Eval extends B>, ? extends Eval extends B>> mapper) {
return EvalOf.narrowK(mapper.apply(value.fix(toId()).get(), initial));
}
}
interface IdTraverse extends Traverse, IdFoldable {
IdTraverse INSTANCE = new IdTraverse() {};
@Override
default Kind> traverse(
Applicative applicative, Kind value,
Function1 super T, ? extends Kind> mapper) {
Kind apply = mapper.apply(value.fix(toId()).get());
return applicative.map(apply, a -> Id.of(a));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy