Questions tagged [foldable]

Foldable is a class of data structures that can be folded to a summary value.

Many of these functions generalize Prelude, Control.Monad and Data.List functions of the same names from lists to any Foldable functor. To avoid ambiguity, either import those modules hiding these names or qualify uses of these function names with an alias for this module.

class Foldable t where

Data structures that can be folded.

Minimal complete definition

[foldMap][5] | [foldr][6]

Full documentation: http://hackage.haskell.org/package/base-4.7.0.1/docs/Data-Foldable.html

69 questions
2
votes
2 answers

Is the equivalent of Haskell's Foldable and Traversable simply a sequence in Clojure?

In Haskell we see Foldable and Traversable landing in Haskell prelude. These both do operations on sequences. Prelude Data.Sequence> map (\n -> replicate n 'a') [1,3,5] ["a","aaa","aaaaa"] Prelude Data.Sequence> fmap (\n -> replicate n 'a') (1 <|…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
1
vote
1 answer

How can I avoid explicit recursion in this case?

I wound up with this skeleton: f :: (Monad m) => b -> m () f x = traverse_ (f . g x) =<< h x -- how avoid explicit recursion? g :: b -> a -> b -- h :: (Foldable t) => b -> m (t a) -- why "Could not deduce (Foldable t0) arising from a use of…
1
vote
1 answer

How to create toList function using a foldable container and a functor in haskell

Was looking at some problems to improve my knwoledge on foldable and functors but cannot quite seem to get my head around how to create the toList function using foldable and a functor This is what i have so far was unsure if i would need to create…
1
vote
1 answer

Haskell Tabulate function using Foldmap "Out of Scope"

I am trying to write a definition for "tabulate", a function that produces the monoidal summary of the values in the given Map that correspond to the keys in the given Foldable collection. Here is my code: module Foldables where import Prelude…
steve
  • 119
  • 7
1
vote
0 answers

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

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 …
Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60
1
vote
1 answer

What are some Foldable instances that are _not_ "general Foldable structures" in the sense of Data.Foldable?

The documentation for Foldable lists several properties that are demanded of "general Foldable structures": For foldr: For a general Foldable structure this should be semantically identical to, foldr f z = foldr f z . toList For foldl: For a…
sjakobi
  • 3,546
  • 1
  • 25
  • 43
1
vote
1 answer

Understanding FoldLeft in ScalaZ

I'm reading some articles about ScalaZ and have a question about understanding it. In this article, we generalize the sum function, to abstract away the type to be summed. def sum[T](xs: List[T])(implicit m: Monoid[T]) = //... Where trait…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
1
vote
1 answer

Using foldl in a function

I have a question about the type definition for using foldl in Haskell, I have the types: data Client = GovOrg String | Company String Integer Person String | Individual Person Bool deriving Show data Person =…
vamsiampolu
  • 6,328
  • 19
  • 82
  • 183
1
vote
1 answer

Ambiguous occurrence ‘foldMap’

I am implementing Foldable for the following data structure: data Tree a = Leaf a | Node (Tree a) (Tree a) deriving Show When I implement fold and foldMap: instance Foldable Tree where --fold :: Monoid a => Tree a -> a fold (Leaf x) = x fold…
Laurence
  • 1,815
  • 4
  • 22
  • 35
1
vote
1 answer

Different types in sum function

I'm new to Haskell so this could be a stupid question. I'm reading a book where it says :type sum is supposed to show sum :: (Num a) => [a] -> a. Instead of that the message is sum :: (Num a, Foldable t) => t a -> a. As I've seen in…
HastatusXXI
  • 101
  • 9
1
vote
2 answers

Cannot understand why MonoFoldable for my type doesn't compile, or the error message

I have the following code: {-# LANGUAGE NoImplicitPrelude, OverloadedStrings, TypeFamilies #-} module AI.Analysis.Rules where import ClassyPrelude -- Our set of rules data RuleSet a = RuleSet [Rule a] [Rule a] deriving (Eq) mkRuleSet :: (Ord…
Koz Ross
  • 3,040
  • 2
  • 24
  • 44
1
vote
1 answer

Should a Foldable always return all its results exactly once?

Here's a type for cyclic, directed graphs with labelled nodes and edges. import qualified Data.Map as M import Data.Foldable import Data.Monoid data Node n e = N n [(e, Node n e)] -- the node's label and its list of neighbors newtype Graph n e = G…
Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
1
vote
1 answer

How does Foldable knows the implementation of mappend

I am learning Haskell with the help of "http://learnyouahaskell.com". I am following the example of a BST (Binary Search Tree) that is an instance of Foldable: data Tree a = Nil | Node a (Tree a) (Tree a) deriving (Show, Read, Eq) instance…
OneEyeQuestion
  • 742
  • 7
  • 22
0
votes
1 answer

Haskell Foldable tree mempty implementation

Hi I need help in understanding this Foldable tree implementation from from here Consider the following import qualified Data.Foldable as F data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show, Read, Eq) instance F.Foldable Tree where…
Boris
  • 1,311
  • 13
  • 39
0
votes
1 answer

How can I write the fsum foldable function in haskell?

fsum :: (Foldable f, Num a) ⇒ f a → a It computes the sum of all numbers in a container-like data structure. My problem is that I cannot define f a as a list or Maybe or any other foldable types to have access. So I am assuming I should write fsum…