- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些从 Haskell 调用的用 C 语言编写的函数。这些函数返回 IO (CInt)
.有时我想运行所有函数,而不管它们返回什么,这很容易。为了示例代码,这是当前正在发生的事情的一般概念:
Prelude> let f x = print x >> return x
Prelude> mapM_ f [0..5]
0
1
2
3
4
5
Prelude>
Prelude> takeWhile (<4) $ mapM f [0..5]
<interactive>:1:22: Couldn't match expected type `[b]' against inferred type `IO a' In the first argument of `mapM', namely `f' In the second argument of `($)', namely `mapM f ([0 .. 5])' In the expression: takeWhile (< 4) $ mapM f ([0 .. 5])
And that makes sense to me - the result is still contained in the IO monad, and I can't just compare two values contained in the IO monad. I know this is precisely the purpose of monads -- chaining results together and discarding operations when a certain condition is met -- but is there an easy way to "wrap up" the IO monad in this case to stop executing the chain upon a condition of my choosing, without writing an instance of MonadPlus
?
Can I just "unlift" the values from f
, for the purposes of the takeWhile?
Is this a solution where functors fit? Functors haven't "clicked" with me yet, but I sort of have the impression that this might be a good situation to use them.
@sth has the closest answer to what I want - in fact, that's almost exactly what I was going for, but I'd still like to see whether there is a standard solution that isn't explicitly recursive -- this is Haskell, after all! Looking back on how I worded my question, now I can see that I wasn't clear enough about my desired behavior.
The f
function I used above for an example was merely an example. The real functions are written in C and used exclusively for their side effects. I can't use @Tom's suggestion of mapM_ f (takeWhile (<4) [0..5])
because I have no idea whether any input will really result in success or failure until executed.
I don't actually care about the returned list, either -- I just want to call the C functions until either the list is exhausted or the first C function returns a failure code.
In C-style pseudocode, my behavior would be:
do {
result = function_with_side_effects(input_list[index++]);
} while (result == success && index < max_index);
dropWhileM_
功能对于我的目的来说是等效的。为什么没有这样的功能或
takeWhileM_
在 Control.Monad 中?我看到有
a similar discussion on a mailing list ,但似乎什么都没有发生。
最佳答案
您可以定义 sequence作为
sequence xs = foldr (liftM2 (:)) (return []) xs
liftM2
的问题你一直看到的是你没有机会停下来
m2
,可能是
launchTheMissiles
!
liftM2 :: (Monad m) => (a -> b -> c) -> m a -> m b -> m c
liftM2 f m1 m2 = do
x1 <- m1
x2 <- m2
return (f x1 x2)
guard
如下所示似乎很有吸引力:
sequenceUntil p xs = foldr (myLiftM2 p (:)) (return []) xs
where myLiftM2 p f m1 m2 = do
x1 <- m1
guard $ p x1
x2 <- m2
return (f x1 x2)
module Main where
import Control.Monad
printx :: Int -> IO Int
printx x = do
print x
return x
sequenceUntil :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
sequenceUntil p xs = foldr (myLiftM2 (:) []) (return []) xs
where myLiftM2 f z m1 m2 = do
x1 <- m1
if p x1 then do x2 <- m2
return $ f x1 x2
else return z
main :: IO ()
main = do
let as :: [IO Int]
as = map printx [1..10]
ys <- sequenceUntil (< 4) as
print ys
as
是超过 1 到 10 的 Action 列表,输出为
1
2
3
4
[1,2,3]
sequenceUntil_ :: (Monad m) => (a -> Bool) -> [m a] -> m ()
sequenceUntil_ p xs = sequenceUntil p xs >> return ()
main :: IO ()
main = do
let as :: [IO Int]
as = map printx [1..]
sequenceUntil_ (< 4) as
[1..]
的使用显示新的组合器
maintains laziness .
spanM
:
spanM :: (Monad m) => (a -> Bool) -> [m a] -> m ([a], [m a])
spanM _ [] = return ([], [])
spanM p (a:as) = do
x <- a
if p x then do (xs,bs) <- spanM p as
return (x:xs, bs)
else return ([x], as)
*Main> (xs,bs) <- spanM (< 4) as
1
2
3
4
*Main> xs
[1,2,3,4]
*Main> sequence bs
5
6
7
8
9
10
[5,6,7,8,9,10]
untilM :: Monad m => (a -> Bool) -> [m a] -> m ()
untilM p (x:xs) = do
y <- x
unless (p y) $ untilM p xs
*Main> untilM (>= 4) as
1
2
3
4
关于Haskell: monadic takeWhile?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1133800/
在我针对 SQL Server 2012 实例运行的以下 Linq 查询中,我尝试读取最新记录,直到之前建立的 MasterId标记(这是一个 Guid )。 var cdc = crm.CdcCon
我正在运行以下代码: val it = List(1,1,1,2,2,3,3).iterator.buffered val compare = it.head it.takeWhile(_ ==
我有一些从 Haskell 调用的用 C 语言编写的函数。这些函数返回 IO (CInt) .有时我想运行所有函数,而不管它们返回什么,这很容易。为了示例代码,这是当前正在发生的事情的一般概念: Pr
我有一个 RxJS5 管道看起来像这样 Rx.Observable.from([2, 3, 4, 5, 6]) .takeWhile((v) => { v !== 4 }) 我想保留订阅直到看到
我是 Haskell 的新手,正在尝试执行以下操作: takeWhile (length < 3) [[1],[1,2],[1..3],[1..4]] .但这给出了一个错误,我认为这是因为 takeW
我有类似以下内容: [bla z|n 1) , let z = 10*n , _ ) = ap instance Monad F w
我想要一些能够有效地执行与 TakeWhile 相同但返回两个序列的东西: TakeWhile 的结果 1. 删除了输入序列的其余部分 我知道我可以做这样的事情: var a = input.Take
如何编写一个 takeWhile 来保留第一个不符合条件的元素? 示例(显然我的示例比这更棘手): 而不是 takeWhile (\× - > x! = 3) [1..10]返回 [1,2]我需要[1
我想要一些能够有效地执行与 TakeWhile 相同但返回两个序列的东西: TakeWhile 的结果 1. 删除了输入序列的其余部分 我知道我可以做这样的事情: var a = input.Take
似乎没有 e.TakeWhile(predicate, atLeastNElements) 重载。有没有一种方便的方式来表达TakeWhile,但是,如果有>= N个元素可用的话,至少要取N个元素。?
如何使用折叠在 Haskell 中实现 takeWhile 函数? takeWhile :: (a -> Bool) -> [a] -> [a] 我尝试了一种类似于实现这样的过滤器的策略 filter
我想知道是否有办法添加根据方法 takeWhile() 的条件测试的流的最后一个元素。我相信我想实现类似于 RxJava 的 takeUntil() 方法的东西。 我猜没有直接的方法可以做到这一点(如
有 public static IEnumerable FibonacciNumbers() { long current = 0; long next = 1; while (true) {
我想在 LINQ to Objects 上使用 LINQ TakeWhile 函数。但是,我还需要知道“破坏”函数的第一个元素,即条件不成立的第一个元素。 是否有一个函数可以获取所有不匹配的对象以及第
takeWhile() 与 Java 9 中的 filter() 有何不同。它有什么额外的实用程序? Stream.of(1,2,3,4,5,6,7,8,9,10).filter(i -> i i
我正在使用 takeWhile 创建片段来探索它的可能性。与 flatMap 配合使用时,行为不符合预期。请在下面找到代码片段。 String[][] strArray = {{"Sample1",
val i = (1 to 8).toIterator val oneToThree = i.takeWhile(_ val i = (1 to 8).iterator i: Iterator[In
我看到它们(where 和 takeWhile)具有相同的功能..否则我可能会在这里漏掉一些东西! 最佳答案 documentation for Iterable.where说: Returns a
isqrt :: Integer -> Integer isqrt = floor . sqrt . fromIntegral primes :: [Integer] primes = sieve [
Javadoc指出 This is a short-circuiting stateful intermediate operation. 有状态的定义来自 Javadoc : Stateful op
我是一名优秀的程序员,十分优秀!