foldable

Is there such a thing as maximumWith?

故事扮演 提交于 2021-02-11 12:50:32
问题 Specifically I'm searching for a function 'maximumWith', maximumWith :: (Foldable f, Ord b) => (a -> b) -> f a -> a Which behaves in the following way: maximumWith length [[1, 2], [0, 1, 3]] == [0, 1, 3] maximumWith null [[(+), (*)], []] == [] maximumWith (const True) x == head x My use case is picking the longest word in a list. For this I'd like something akin to maximumWith length . I'd thought such a thing existed, since sortWith etc. exist. 回答1: Let me collect all the notes in the

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

How can I express foldr in terms of foldMap for type-aligned sequences?

空扰寡人 提交于 2020-01-02 01:18:25
问题 I'm playing around with type-aligned sequences, and in particular I'm messing around with the idea of folding them. A foldable type-aligned sequence looks something like this: class FoldableTA fm where foldMapTA :: Category h => (forall b c . a b c -> h b c) -> fm a b d -> h b d foldrTA :: (forall b c d . a c d -> h b c -> h b d) -> h p q -> fm a q r -> h p r foldlTA :: ... It's pretty easy to implement foldrTA in terms of foldMapTA by first using foldMapTA to convert the sequence to a type

Foldr/Foldl for free when Tree is implementing Foldable foldMap?

家住魔仙堡 提交于 2019-12-28 02:12:10
问题 I am a beginner at Haskell and learning from "Learn You a Haskell". There's something I don't understand about the Tree implementation of Foldable . instance F.Foldable Tree where foldMap f Empty = mempty foldMap f (Node x l r) = F.foldMap f l `mappend` f x `mappend` F.foldMap f r Quote from LYAH: "So if we just implement foldMap for some type, we get foldr and foldl on that type for free !" . Can someone explain this? I don't understand how and why do I get foldr and foldl for free now...

Foldable IntSet

拥有回忆 提交于 2019-12-10 16:11:48
问题 For me, an integer set seems to be a foldable data structure. Why is Data.IntSet not an instance of Foldable ? My actual intention is to use find on an IntSet . How can I implement find for Data.IntSet ? 回答1: IntSet can't be Foldable from base package because it doesn't have kind * -> * . ghci> :t foldr foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b ghci> :k Foldable Foldable :: (* -> *) -> Constraint ghci> import Data.IntSet (IntSet) ghci> :k IntSet IntSet :: * In simple words, to be

Can `foldr` and `foldl` be defined in terms of each other?

对着背影说爱祢 提交于 2019-12-08 14:24:20
问题 Can foldr and foldl be defined in terms of each other? Programming in Haskell by Hutton says What do we need to define manually? The minimal complete definition for an instance of the Foldable class is to define either foldMap or foldr , as all other functions in the class can be derived from either of these two using the default definitions and the instance for lists. So how can foldl be defined in terms of foldr ? Can foldr be defined in terms of foldl , so that we can define a Foldable

Is there anything we lose with MonoFoldable?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 22:15:20
问题 MonoFoldable in the mono-traversable package seems to be able to implement all of the usual Foldable containers and more, for example, things like Bytestring and homogeneous tuples can be made MonoFoldable but not Foldable . My question is, do we lose anything from MonoFoldable that we don't have in Foldable , aside from requiring some advanced GHC features, making it slightly more tricky for instance writers and perhaps getting uglier error messages? For example, is there some code which

Foldable, Monoid and Monad

心不动则不痛 提交于 2019-11-29 23:01:03
Consider the following signature of foldMap foldMap :: (Foldable t, Monoid m) => (a -> m) -> t a -> m This is very similar to "bind", just with the arguments swapped: (>>=) :: Monad m => m a -> (a -> m b) -> m b It seems to me that there therefore must be some sort of relationship between Foldable , Monoid and Monad , but I can't find it in the superclasses. Presumably I can transform one or two of these into the other but I'm not sure how. Could that relationship be detailed? ThreeFx Monoid and Monad Wow, this is actually one of the rare times we can use the quote: A monad is just a monoid in

Foldable, Monoid and Monad

此生再无相见时 提交于 2019-11-28 20:05:06
问题 Consider the following signature of foldMap foldMap :: (Foldable t, Monoid m) => (a -> m) -> t a -> m This is very similar to "bind", just with the arguments swapped: (>>=) :: Monad m => m a -> (a -> m b) -> m b It seems to me that there therefore must be some sort of relationship between Foldable , Monoid and Monad , but I can't find it in the superclasses. Presumably I can transform one or two of these into the other but I'm not sure how. Could that relationship be detailed? 回答1: Monoid and