com.github.tonivade.purefun.instances.Function1Instances 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.Function1Of.toFunction1;
import static com.github.tonivade.purefun.typeclasses.Conested.conest;
import static com.github.tonivade.purefun.typeclasses.Conested.counnest;
import com.github.tonivade.purefun.Function1;
import com.github.tonivade.purefun.Function1Of;
import com.github.tonivade.purefun.Function1_;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.typeclasses.Applicative;
import com.github.tonivade.purefun.typeclasses.Conested;
import com.github.tonivade.purefun.typeclasses.Contravariant;
import com.github.tonivade.purefun.typeclasses.Functor;
import com.github.tonivade.purefun.typeclasses.Monad;
import com.github.tonivade.purefun.typeclasses.Profunctor;
@SuppressWarnings("unchecked")
public interface Function1Instances {
static Functor> functor() {
return Function1Functor.INSTANCE;
}
static Applicative> applicative() {
return Function1Applicative.INSTANCE;
}
static Monad> monad() {
return Function1Monad.INSTANCE;
}
static Contravariant> contravariant() {
return Function1Contravariant.INSTANCE;
}
static Profunctor profunctor() {
return Function1Profunctor.INSTANCE;
}
}
interface Function1Functor extends Functor> {
@SuppressWarnings("rawtypes")
Function1Functor INSTANCE = new Function1Functor() {};
@Override
default Function1 map(Kind, ? extends A> value,
Function1 super A, ? extends R> map) {
Function1 function = value.fix(Function1Of::narrowK);
return function.andThen(map);
}
}
interface Function1Pure extends Applicative> {
@Override
default Function1 pure(A value) {
return Function1.cons(value);
}
}
interface Function1Applicative extends Function1Pure {
@SuppressWarnings("rawtypes")
Function1Applicative INSTANCE = new Function1Applicative() {};
@Override
default Function1 ap(Kind, ? extends A> value,
Kind, ? extends Function1 super A, ? extends R>> apply) {
return value.fix(toFunction1())
.flatMap(a -> apply.fix(Function1Of::narrowK).andThen(f -> f.apply(a)));
}
}
interface Function1Monad extends Function1Pure, Monad> {
@SuppressWarnings("rawtypes")
Function1Monad INSTANCE = new Function1Monad() {};
@Override
default Function1 flatMap(Kind, ? extends A> value,
Function1 super A, ? extends Kind, ? extends R>> map) {
Function1 function = value.fix(Function1Of::narrowK);
return function.flatMap(map.andThen(Function1Of::narrowK));
}
}
interface Function1Contravariant extends Contravariant> {
@SuppressWarnings("rawtypes")
Function1Contravariant INSTANCE = new Function1Contravariant() {};
@Override
default Kind, B> contramap(Kind, ? extends A> value, Function1 super B, ? extends A> map) {
Kind, R> counnest = counnest(value);
Function1 function = counnest.fix(Function1Of::narrowK);
return conest(function.compose(map));
}
}
interface Function1Profunctor extends Profunctor {
Function1Profunctor INSTANCE = new Function1Profunctor() {};
@Override
default Function1 dimap(Kind, ? extends B> value, Function1 super C, ? extends A> contramap, Function1 super B, ? extends D> map) {
return value.fix(Function1Of::narrowK).dimap(contramap, map);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy