All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.tonivade.purefun.instances.IdInstances 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 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.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 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> 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, 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 mapper) {
    return mapper.apply(initial, IdOf.narrowK(value).get());
  }

  @Override
  default  Eval foldRight(Kind value, Eval initial, Function2, Eval> mapper) {
    return mapper.apply(IdOf.narrowK(value).get(), initial);
  }
}

interface IdTraverse extends Traverse, IdFoldable {

  IdTraverse INSTANCE = new IdTraverse() {};

  @Override
  default  Kind> traverse(
      Applicative applicative, Kind value,
      Function1> mapper) {
    return applicative.map(mapper.apply(IdOf.narrowK(value).get()), a -> Id.of(a));
  }
}