在 fp-ts 中,我试图将一些可能失败的异步任务链接在一起,TaskEither
但我需要稍后在链中使用中间任务的结果。
在这个例子中:
const getFoo = (a: string): Promise<Foo> => {};
const getBar = (foo: Foo): Promise<Bar> => {};
const mkFooBar = (foo: Foo, bar: Bar): Promise<FooBar> => {};
const async main1: Promise<FooBar> => {
const a = "a";
const foo = await getFoo(a);
const bar = await getBar(foo);
const fooBar = await mkFooBar(foo, bar);
return Promise.resolve(fooBar);
};
const main2: Promise<FooBar> => {
const a = "a";
return pipe(
TE.tryCatch(() => getFoo(a), e => e),
TE.chain(foo => …
Run Code Online (Sandbox Code Playgroud) 我正在使用optparse-applicative
,我希望能够解析命令行参数,例如:
$ ./program -a file1 file2 -b filea fileb
Run Code Online (Sandbox Code Playgroud)
即,两个开关,两者都可以采用多个参数.
所以我的选项有一个数据类型,如下所示:
data MyOptions = MyOptions {
aFiles :: [String]
, bFiles :: [String] }
Run Code Online (Sandbox Code Playgroud)
然后Parser
像这样:
config :: Parser MyOptions
config = MyOptions
<$> option (str >>= parseStringList)
( short 'a' <> long "aFiles" )
<*> option (str >>= parseStringList)
( short 'b' <> long "bFiles" )
parseStringList :: Monad m => String -> m [String]
parseStringList = return . words
Run Code Online (Sandbox Code Playgroud)
这种方法失败的原因是,当为每个开关提供一个参数时,它将给出预期的结果,但是如果你提供第二个参数,则为第二个参数得到"无效的参数".
我想知道我是否可以通过假装我想要四个选项来克服它:一个布尔开关(即-a
); 字符串列表; 另一个布尔开关(即 …