Saber2pr's Blog

HaskellAndJs

In fact, a large number of Functor and Monad are already used in JS.

  1. Functor For example: In haskell
fmap (+1) [1,2,3] -- [2, 3, 4]

Similarly, there are in JS

Array.from([1, 2, 3], x => x + 1) // [2, 3, 4]
  1. Monad for example: In haskell
Just 233 >>= \x -> Just $ x + 1 -- Just 234

Similarly, there are in JS

Promise.resolve(233).then(x => x + 1) // Promise&nbsp;{<resolved>: 234}
  1. Monad do-block for example: In haskell
test :: Monad m => m Integer
test = pure 233

result :: Monad m => m Integer
result = do
  value <- test
  return $ value + 1

Similarly, there are in JS

const test = Promise.resolve(233)

const result = async () => {
  const value = await test
  return value + 1
}