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

com.github.tonivade.purefun.instances.CofreeInstances 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 com.github.tonivade.purefun.Function1;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.Witness;
import com.github.tonivade.purefun.free.Cofree;
import com.github.tonivade.purefun.free.CofreeOf;
import com.github.tonivade.purefun.free.Cofree_;
import com.github.tonivade.purefun.typeclasses.Comonad;
import com.github.tonivade.purefun.typeclasses.Functor;

@SuppressWarnings("unchecked")
public interface CofreeInstances {

  static  Functor> functor() {
    return CofreeFunctor.INSTANCE;
  }

  static  Comonad> comonad() {
    return CofreeComonad.INSTANCE;
  }
}

interface CofreeFunctor extends Functor> {

  @SuppressWarnings("rawtypes")
  CofreeFunctor INSTANCE = new CofreeFunctor() {};

  @Override
  default  Cofree map(Kind, ? extends T> value, Function1 map) {
    return value.fix(CofreeOf::narrowK).map(map);
  }
}

interface CofreeComonad extends Comonad>, CofreeFunctor {

  @SuppressWarnings("rawtypes")
  CofreeComonad INSTANCE = new CofreeComonad() { };

  @Override
  default  A extract(Kind, ? extends A> value) {
    return value.fix(CofreeOf::narrowK).extract();
  }

  @Override
  default  Cofree coflatMap(
      Kind, ? extends A> value, Function1, ? extends A>, ? extends B> map) {
    return value.fix(CofreeOf::narrowK).coflatMap(map::apply);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy