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

com.github.tonivade.purefun.free.Yoneda 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.free;

import static com.github.tonivade.purefun.Function1.identity;

import com.github.tonivade.purefun.Function1;
import com.github.tonivade.purefun.Higher1;
import com.github.tonivade.purefun.HigherKind;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.typeclasses.Functor;

@HigherKind
@FunctionalInterface
public interface Yoneda {

   Higher1 apply(Function1 map);
  
  default Higher1 lower() {
    return apply(identity());
  }
  
  default  Yoneda map(Function1 outer) {
    return new Yoneda() {
      @Override
      public  Higher1 apply(Function1 inner) {
        return Yoneda.this.apply(outer.andThen(inner));
      }
    };
  }
  
  static  Yoneda of(Higher1 value, Functor functor) {
    return new Yoneda() {
      @Override
      public  Higher1 apply(Function1 map) {
        return functor.map(value, map);
      }
    };
  }
}