com.github.tonivade.purefun.instances.Function1Instances 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 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, A> value, Function1 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, A> value, Kind, Function1> apply) {
Function1 function = value.fix(Function1Of::narrowK);
Function1> map = apply.fix(Function1Of::narrowK);
return function.flatMap(a -> map.andThen(f -> f.apply(a)));
}
}
interface Function1Monad extends Function1Pure, Monad> {
@SuppressWarnings("rawtypes")
Function1Monad INSTANCE = new Function1Monad() {};
@Override
default Function1 flatMap(Kind, A> value, Function1, 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, A> value, Function1 map) {
Function1 function = counnest(value).fix(Function1Of::narrowK);
return conest(function.compose(map));
}
}
interface Function1Profunctor extends Profunctor {
Function1Profunctor INSTANCE = new Function1Profunctor() {};
@Override
default Function1 dimap(Kind, B> value, Function1 contramap, Function1 map) {
Function1 function = value.fix(Function1Of::narrowK);
return function.compose(contramap).andThen(map);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy