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

com.github.tonivade.purefun.instances.StateTInstances 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 static java.util.Objects.requireNonNull;

import com.github.tonivade.purefun.Function1;
import com.github.tonivade.purefun.Higher1;
import com.github.tonivade.purefun.Instance;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.transformer.StateT;
import com.github.tonivade.purefun.typeclasses.Monad;

public interface StateTInstances {

  static  Monad, S>> monad(Monad monadF) {
    requireNonNull(monadF);
    return new StateTMonad() {

      @Override
      public Monad monadF() { return monadF; }
    };
  }
}

@Instance
interface StateTMonad extends Monad, S>> {

  Monad monadF();

  @Override
  default  StateT pure(T value) {
    return StateT.pure(monadF(), value);
  }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy