evilsoft / crocks

A collection of well known Algebraic Data Types for your utter enjoyment.
https://crocks.dev
ISC License
1.59k stars 102 forks source link

Add the StateT monad #544

Open kierans opened 1 year ago

kierans commented 1 year ago

Is your feature request related to a problem? Please describe.

Much like with Reader when using the State monad with an inner monad when mapping, or chaining the code gets messy dealing with the inner monad

Describe the solution you'd like A description of what you'd like to have added to the project.

Like how there's a ReaderT in Crocks, there should be a StateT.

Describe alternatives for how you do this now

Just have to remember to handle the inner monad

// exclaim :: String -> String
const exclaim = concat("!!!")

// toUpper :: String -> String
const toUpper = (s) => s.toUpperCase()

// getName :: Object -> Maybe a
const getName = getProp("name")

// convertName :: String -> String
const convertName = compose(exclaim, toUpper);

// findName :: State Object (Maybe String)
const findName =
  State.get(getName).map(map(convertName))

findName.evalWith({ name: "evilsoft" }).toString()
//=> Just "EVILSOFT!!!"

findName.evalWith({})
//=> Nothing

Code

Additional context Add any other context or screenshots about the feature request here.

@evilsoft mentioned in one of his YouTube videos that he was planning on writing StateT