monads

Standard combinator to get first “non-empty” value from a set of monadic actions

ⅰ亾dé卋堺 提交于 2020-02-05 02:34:07
问题 I'm sure I am missing something very obvious here. Here's what I'm trying to achieve at a conceptual level: action1 :: (MonadIO m) => m [a] action1 = pure [] action2 :: (MonadIO m) => m [a] action2 = pure [1, 2, 3] action3 :: (MonadIO m) => m [a] action3 = error "should not get evaluated" someCombinator [action1, action2, action3] == m [1, 2, 3] Does this hypothetical someCombinator exist? I have tried playing round with <|> and msum but couldn't get what I want. I guess, this could be

Adding Haskell's Monadic Bind Operator to Scala

两盒软妹~` 提交于 2020-02-03 03:26:08
问题 In Haskell, you can use the bind operator ( >>= ) like this: repli :: [a] -> [a] repli xs = xs >>= \x -> [x,x] *Main> repli [1,2,3] [1,1,2,2,3,3] I've read that flatMap is Scala's bind operator: def repli [A](xs: List[A]): List[A] = xs.flatMap { x => List(x,x) } scala> repli (List(1,2,3)) res0: List[Int] = List(1, 1, 2, 2, 3, 3) As a pedagogic exercise, I'm trying to add support for >>= to Scala: class MyList[T](list: List[T]) { def >>= [U](f: T => List[U]): List[U] = list.flatMap(f) }

Understanding filterM

荒凉一梦 提交于 2020-02-01 11:41:56
问题 Consider filterM (\x -> [True, False]) [1, 2, 3] I just cannot understand the magic that Haskell does with this filterM use case. The source code for this function is listed below: filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a] filterM _ [] = return [] filterM p (x:xs) = do flg <- p x ys <- filterM p xs return (if flg then x:ys else ys) With this use case, p should be the lambda function (\x -> [True, False]) , and the first x should be 1 . So what does flg <- p x return? What exactly

Understanding filterM

霸气de小男生 提交于 2020-02-01 11:40:15
问题 Consider filterM (\x -> [True, False]) [1, 2, 3] I just cannot understand the magic that Haskell does with this filterM use case. The source code for this function is listed below: filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a] filterM _ [] = return [] filterM p (x:xs) = do flg <- p x ys <- filterM p xs return (if flg then x:ys else ys) With this use case, p should be the lambda function (\x -> [True, False]) , and the first x should be 1 . So what does flg <- p x return? What exactly

Understanding filterM

点点圈 提交于 2020-02-01 11:39:28
问题 Consider filterM (\x -> [True, False]) [1, 2, 3] I just cannot understand the magic that Haskell does with this filterM use case. The source code for this function is listed below: filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a] filterM _ [] = return [] filterM p (x:xs) = do flg <- p x ys <- filterM p xs return (if flg then x:ys else ys) With this use case, p should be the lambda function (\x -> [True, False]) , and the first x should be 1 . So what does flg <- p x return? What exactly

Understanding filterM

岁酱吖の 提交于 2020-02-01 11:39:06
问题 Consider filterM (\x -> [True, False]) [1, 2, 3] I just cannot understand the magic that Haskell does with this filterM use case. The source code for this function is listed below: filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a] filterM _ [] = return [] filterM p (x:xs) = do flg <- p x ys <- filterM p xs return (if flg then x:ys else ys) With this use case, p should be the lambda function (\x -> [True, False]) , and the first x should be 1 . So what does flg <- p x return? What exactly

`fail “zero”` as a way to generate Nothing in simple `a -> Maybe a` example

…衆ロ難τιáo~ 提交于 2020-01-25 09:16:20
问题 I'm reading a revealing example of using a bind operator: Just 5 >>= (\ x -> if (x == 0) then fail "zero" else Just (x + 1) ) returns Just 6 . I'm confused by the behaviour of fail and its usefulness in the example. When looking at the code I thought fail "zero" may have a meaning: program never gets to that point laziness something else. Then I realised that after a type cohesion, an exception becomes Nothing (documented here). Still confusing for me that without type enforcement fail is

Filter for first matching monadic action (without evaluating all actions)?

久未见 提交于 2020-01-25 06:40:28
问题 Is there a standard / optimized implementation of the following function that I'm writing (probably unnecessarily): filterFirstM :: (Monad m, Foldable t) => (a -> Bool) -> t m a -> m a filterFirstM predicate actions = foldlM fn Nothing actions where fn memo action = case memo of Just _ -> pure memo Nothing -> do x <- action pure $ if predicate x then (Just x) else Nothing Sample usage: filterFirstM (== 1) [pure 0 :: IO Int, pure 1, error "not evaluated"] == (pure 1) 来源: https://stackoverflow

Filter for first matching monadic action (without evaluating all actions)?

≡放荡痞女 提交于 2020-01-25 06:40:08
问题 Is there a standard / optimized implementation of the following function that I'm writing (probably unnecessarily): filterFirstM :: (Monad m, Foldable t) => (a -> Bool) -> t m a -> m a filterFirstM predicate actions = foldlM fn Nothing actions where fn memo action = case memo of Just _ -> pure memo Nothing -> do x <- action pure $ if predicate x then (Just x) else Nothing Sample usage: filterFirstM (== 1) [pure 0 :: IO Int, pure 1, error "not evaluated"] == (pure 1) 来源: https://stackoverflow

Carry STRef implicitly in an environment during computation

这一生的挚爱 提交于 2020-01-24 09:20:06
问题 I am working on some bigger computation that requires use of mutable data in some critical moments. I want to avoid IO as much as I can. My model used to constist of ExceptT over ReaderT over State datatype, and now I want to replace State with mentioned ST . To simplify, let's assume I would like to keep single STRef with an Int during the whole computation, and let's skip the ExceptT outer layer. My initial idea was to put STRef s Int into ReaderT 's environment: {-#LANGUAGE Rank2Types#-} {