我尝试使用IO和Maybe monads 来制作一个简单的示例。程序从DOM读取一个节点并向其写入一些innerHTML
。
我所依赖的是IO和Maybe的组合,例如: IO (Maybe NodeList)
。
如何使用此设置短路或抛出错误?
我可以使用getOrElse
来提取值或设置默认值,但将默认值设置为空数组并不会有任何帮助。
import R from 'ramda';
import { IO, Maybe } from 'ramda-fantasy';
const Just = Maybe.Just;
const Nothing = Maybe.Nothing;
// $ :: String -> Maybe NodeList
const $ = (selector) => {
const res = document.querySelectorAll(selector);
return res.length ? Just(res) : Nothing();
}
// getOrElse :: Monad m => m a -> a -> m a
var getOrElse = R.curry(function(val, m) {
return m.getOrElse(val);
});
// read :: String -> IO (Maybe NodeList)
const read = selector =>
IO(() => $(selector));
// write :: String -> DOMNode -> IO
const write = text =>
(domNode) =>
IO(() => domNode.innerHTML = text);
const prog = read('#app')
// What goes here? How do I short circuit or error?
.map(R.head)
.chain(write('Hello world'));
prog.runIO();
答案 0 :(得分:3)
您可以尝试编写EitherIO
monad变换器。 Monad变换器允许您将两个monad的效果组合成一个monad。它们可以用通用的方式编写,这样我们就可以根据需要创建monad的动态组合,但是在这里我将演示Either
和IO
的静态耦合。
首先,我们需要一种从IO (Either e a)
到EitherIO e a
的方法,以及从EitherIO e a
到IO (Either e a)
的方式
EitherIO :: IO (Either e a) -> EitherIO e a
runEitherIO :: EitherIO e a -> IO (Either e a)
我们需要一些辅助函数来将其他平面类型带到我们的嵌套monad
EitherIO.liftEither :: Either e a -> EitherIO e a
EitherIO.liftIO :: IO a -> EitherIO e a
为了符合梦幻之地,我们的新EitherIO
monad具有chain
方法和of
功能,并遵守monad法律。为方便起见,我还使用map
方法实现了仿函数接口。
<强> EitherIO.js 强>
import { IO, Either } from 'ramda-fantasy'
const { Left, Right, either } = Either
// type EitherIO e a = IO (Either e a)
export const EitherIO = runEitherIO => ({
// runEitherIO :: IO (Either e a)
runEitherIO,
// map :: EitherIO e a => (a -> b) -> EitherIO e b
map: f =>
EitherIO(runEitherIO.map(m => m.map(f))),
// chain :: EitherIO e a => (a -> EitherIO e b) -> EitherIO e b
chain: f =>
EitherIO(runEitherIO.chain(
either (x => IO.of(Left(x)), (x => f(x).runEitherIO))))
})
// of :: a -> EitherIO e a
EitherIO.of = x => EitherIO(IO.of(Right.of(x)))
// liftEither :: Either e a -> EitherIO e a
export const liftEither = m => EitherIO(IO.of(m))
// liftIO :: IO a -> EitherIO e a
export const liftIO = m => EitherIO(m.map(Right))
// runEitherIO :: EitherIO e a -> IO (Either e a)
export const runEitherIO = m => m.runEitherIO
调整您的程序以使用EitherIO
有什么好处是你的read
和write
函数没问题 - 你的程序中没有任何内容需要更改,除了我们如何在prog
中构建调用
import { compose } from 'ramda'
import { IO, Either } from 'ramda-fantasy'
const { Left, Right, either } = Either
import { EitherIO, liftEither, liftIO } from './EitherIO'
// ...
// prog :: IO (Either Error String)
const prog =
EitherIO(read('#app'))
.chain(compose(liftIO, write('Hello world')))
.runEitherIO
either (throwError, console.log) (prog.runIO())
补充说明
// prog :: IO (Either Error String)
const prog =
// read already returns IO (Either String DomNode)
// so we can plug it directly into EitherIO to work with our new type
EitherIO(read('#app'))
// write only returns IO (), so we have to use liftIO to return the correct EitherIO type that .chain is expecting
.chain(compose(liftIO, write('Hello world')))
// we don't care that EitherIO was used to do the hard work
// unwrap the EitherIO and just return (IO Either)
.runEitherIO
// this actually runs the program and clearly shows the fork
// if prog.runIO() causes an error, it will throw
// otherwise it will output any IO to the console
either (throwError, console.log) (prog.runIO())
检查错误
继续将'#app'
更改为某个不匹配的选择器(例如)'#foo'
。重新运行程序,您将看到适当的错误被禁止进入控制台
Error: Could not find DOMNode
Runnable演示
你做到了这一点。这是一个可运行的演示作为奖励:https://www.webpackbin.com/bins/-Kh5NqerKrROGRiRkkoA
使用EitherT的通用转换
monad变换器将monad作为参数并创建一个新的monad。在这种情况下,EitherT
会占用一些monad M
并创建一个有效行为为M (Either e a)
的monad。
所以现在我们有办法创建新的monads
// EitherIO :: IO (Either e a) -> EitherIO e a
const EitherIO = EitherT (IO)
我们还有将平面类型提升为嵌套类型的功能
EitherIO.liftEither :: Either e a -> EitherIO e a
EitherIO.liftIO :: IO a -> EitherIO e a
最后一个自定义运行函数,可以更容易地处理我们的嵌套IO (Either e a)
类型 - 注意,一层抽象(IO
)被删除,所以我们只需要考虑{{1} }
Either
<强> EitherT 强>
是面包和黄油 - 你在这里看到的主要区别是runEitherIO :: EitherIO e a -> Either e a
接受monad EitherT
作为输入并创建/返回一个新的Monad类型
M
<强> EitherIO 强>
现在可以用// EitherT.js
import { Either } from 'ramda-fantasy'
const { Left, Right, either } = Either
export const EitherT = M => {
const Monad = runEitherT => ({
runEitherT,
chain: f =>
Monad(runEitherT.chain(either (x => M.of(Left(x)),
x => f(x).runEitherT)))
})
Monad.of = x => Monad(M.of(Right(x)))
return Monad
}
export const runEitherT = m => m.runEitherT
实现- 一种极为简化的实现
EitherT
我们计划的更新
import { IO, Either } from 'ramda-fantasy'
import { EitherT, runEitherT } from './EitherT'
export const EitherIO = EitherT (IO)
// liftEither :: Either e a -> EitherIO e a
export const liftEither = m => EitherIO(IO.of(m))
// liftIO :: IO a -> EitherIO e a
export const liftIO = m => EitherIO(m.map(Either.Right))
// runEitherIO :: EitherIO e a -> Either e a
export const runEitherIO = m => runEitherT(m).runIO()
使用EitherT运行演示
这是使用EitherT的可运行代码:https://www.webpackbin.com/bins/-Kh8S2NZ8ufBStUSK1EU
答案 1 :(得分:2)
如果给定的谓词返回true,您可以创建一个辅助函数,它将有条件地与另一个IO生成函数链接。如果返回false,则会生成IO ()
。
// (a → Boolean) → (a → IO ()) → a → IO ()
const ioWhen = curry((pred, ioFn, val) =>
pred(val) ? ioFn(val) : IO(() => void 0))
const $ = document.querySelector.bind(document)
const read = selector =>
IO(() => $(selector))
const write = text => domNode =>
IO(() => domNode.innerHTML = text)
const prog = read('#app').chain(
ioWhen(node => node != null, write('Hello world'))
)
prog.runIO();