我试图使用foldr实现filterM函数,但是接收到一个我无法理解的错误.
我的实现(这只是为了理解,我知道我应该使用do notation ...):
filterM :: (Monad m) => (a -> (m Bool)) -> [a] -> m [a]
filterM f list = foldr foldFn (return []) list
where
foldFn :: (Monad m) => a -> m [a] -> m [a]
foldFn x acc = let
m = f x
in acc >>=
\l -> m >>=
\b -> (if b == True then return (x:l) else return l)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Could not deduce (a ~ a1)
from the context (Monad m) …Run Code Online (Sandbox Code Playgroud) 我在网上找到的用于缩小flowtype中不相交联合的所有示例都使用字符串文字,就像官方文字一样.我想知道是否有办法检查枚举中的值,如:
const ACTION_A = 'LITERAL_STRING_A';
const ACTION_B = 'LITERAL_STRING_B';
type ActionA = {
// This is not allowed
type: ACTION_A,
// type: 'LITERAL_STRING_A' is allowed
dataA: ActionAData,
}
type ActionB = {
// This is not allowed
type: ACTION_B,
// type: 'LITERAL_STRING_B' is allowed
dataB: ActionBData,
}
type Action = ActionA | ActionB;
function reducer(state: State, action: Action): State {
// Want to narrow Action to ActionA or ActionB based on type
switch (action.type) {
// case 'LITERAL_STRING_A': …Run Code Online (Sandbox Code Playgroud)