Git Product home page Git Product logo

fsm's Introduction

Finite State machines with Refs

Assuming this simple interface for our FSM:

  type FSM[F[_], I, O] = Kleisli[F, I, O]

where I is the input and O is output, we can model it with Cats-effect's Ref as below:

object RefBasedFSM {
  def apply[F[_]: FlatMap, I, S, O](
      ref: Ref[F, S],
      modifyStateOnInput: I => S => (S, F[O])
  ): FSM[F, I, O] =
    Kleisli(input =>
      ref
        .modify(modifyStateOnInput(input))
        .flatten
    )
}
  • In the case of our state transitioning being synchronous, that is all there is to it. See this for a concrete example.

  • In the case of the state transition being asynchronous, ie, if there is a database layer, so for example:

  type Potentially[S] = Either[Throwable, S]
  type StateStore[F[_]] = Kleisli[F, State, Potentially[Unit]]

we can internally introduce this structure:

    sealed trait AsyncState[S]
    object AsyncState {
      case class Value[S](state: S) extends AsyncState[S]
      case class Updating[S](asyncState: Deferred[F, Either[Throwable, S]])
          extends AsyncState[S]
    }

and use an instance of Deferred to handle the Async part. See here for a concrete example.

Next steps

The sync impl, simply wants a I => S => (S, F[O]) to work, which is intuitive enough. In the case of Async impl however, as it stands, the usage of deferred is left to the developer and not managed by the code. Would be nice to come up with an abstraction that would manage that for us.

fsm's People

Contributors

shahodin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.