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-2019, 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.Higher1;
import com.github.tonivade.purefun.Instance;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.type.Eval;
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(Id.narrowK(a).get(), Id.narrowK(b).get());
  }

  static Functor functor() {
    return new IdFunctor() {};
  }

  static Applicative applicative() {
    return new IdApplicative() {};
  }

  static Monad monad() {
    return new IdMonad() {};
  }

  static Comonad comonad() {
    return new IdComonad() {};
  }

  static Foldable foldable() {
    return new IdFoldable() {};
  }

  static Traverse traverse() {
    return new IdTraverse() {};
  }
}

@Instance
interface IdFunctor extends Functor {

  @Override
  default  Id map(Higher1 value, Function1 map) {
    return Id.narrowK(value).map(map);
  }
}

@Instance
interface IdPure extends Applicative {

  @Override
  default  Id pure(T value) {
    return Id.of(value);
  }
}

@Instance
interface IdApplicative extends IdPure {

  @Override
  default  Id ap(Higher1 value, Higher1> apply) {
    return Id.narrowK(value).flatMap(t -> Id.narrowK(apply).map(f -> f.apply(t)));
  }
}

@Instance
interface IdMonad extends IdPure, Monad {

  @Override
  default  Id flatMap(Higher1 value, Function1> map) {
    return Id.narrowK(value).flatMap(map.andThen(Id::narrowK));
  }
}

@Instance
interface IdComonad extends IdFunctor, Comonad {

  @Override
  default  Id coflatMap(Higher1 value, Function1, B> map) {
    return Id.of(map.apply(value));
  }

  @Override
  default  A extract(Higher1 value) {
    return Id.narrowK(value).get();
  }
}

@Instance
interface IdFoldable extends Foldable {

  @Override
  default  B foldLeft(Higher1 value, B initial, Function2 mapper) {
    return mapper.apply(initial, Id.narrowK(value).get());
  }

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

@Instance
interface IdTraverse extends Traverse, IdFoldable {

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