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
3
votes
2 answers

How to write reverseT without using List?

I need an alternative to reverseT that doesn't use toList. Obviously, this code is incorrect, but demonstrates the idea I was pursuing: reverseF :: (Foldable f, Representable f, Num (Rep f)) => f a -> f a reverseF f = tabulate $ \ix -> index f $…
dbanas
  • 1,707
  • 14
  • 24
3
votes
2 answers

How do I implement a foldable instance for Constant a b = Constant a?

I want to implement foldable for data Constant a b = Constant a This is my straightforward attempt: instance Foldable (Constant a) where foldr f b (Constant a) = f a b The part of the compilation error I want to understand is: Couldn't match…
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
3
votes
2 answers

How to create a Foldable instance of a type with multiple parameters in Haskell?

I have a data type: data Box a b = Box a b I want to create a Foldable instance of Box and since the Foldable instance has to be given something of kind * -> *, I'll declare the instance as: instance Foldable (Box a) where foldr f x (Box r s) =…
Muhammad Ali
  • 599
  • 1
  • 7
  • 15
2
votes
1 answer

Adequate instantiation of foldMap

data MyData a b = MyData a b b Why is the first instantiation good and the second not ? instance Foldable (MyData a) where foldMap f (MyData x y z) = f y <> f z instance Foldable (MyData a) where foldMap f (MyData x y z) = f z f maps…
Johnny
  • 447
  • 2
  • 8
2
votes
2 answers

Cannot implement Foldable instance due to wrong kind

I am learning haskell and trying to make a pretty print program. At some point I want to get the length of a row (i.e. number of columns in that row). To be able to do that on my datatype I understand I have to implement Foldable, which relies on…
2
votes
2 answers

Convert Tuple into Foldable

Is there a way to derive Foldable from Tuple? At least when the tuple is homogeneous? For example let's say I have (1,2,3) and I want to reverse it or to transform it into [1,2,3] and similar things. I've tried to do something like over each (\x ->…
user11323942
2
votes
1 answer

(New?) Modal Operators for Foldable

This post follows in a sense my previous one. HTNW, in their answer there, defined the data type Same and the function allEq. So I thought that by defining the data type AllDifferent, the function allDiff and the derived ones someEq and someDiff, I…
Alberto Capitani
  • 1,039
  • 13
  • 30
2
votes
1 answer

Foldable and Monoid types

I'm trying to write functions that add and multiply all elements in a list using monoids and Foldable. I've set up some code that I think works: data Rose a = a :> [Rose a] deriving (Eq, Show) instance Functor Rose where fmap f rose@(a:>b)…
The Coding Wombat
  • 805
  • 1
  • 10
  • 29
2
votes
1 answer

Haskell creating map and foldr function for new datatype

I have written a data structure, data Bit a = Add a (Bit a) | End deriving (Show,Eq) data Bits a = Bits (Bit a) (Bit a) deriving (Show,Eq) but am struggling to create a map and foldr function for them. So far I have this: instance Functor Bit …
Alutri
  • 33
  • 4
2
votes
2 answers

Changing indices within a Haskell tree

(Sorry for the long context description, but I couldn't find a simpler way to explain my problem) Consider the following types: import Data.Array data UnitDir = Xp | Xm | Yp | Ym | Zp | Zm deriving (Show, Eq, Ord, Enum, Bounded, Ix) type…
ablondin
  • 427
  • 1
  • 4
  • 7
2
votes
1 answer

Universal container conversion? if from Foldable to Alternative?

For instance Alternative [], (<|>) = (++). So I regarded (<|>) as some kind of splicer, resulting in seemingly almost-universal container converter: -- (<|>) = generalization of (++) (<|) :: Alternative f => x -> f x -> f x x <| xs = pure x <|>…
2
votes
1 answer

Foldable "foldMap" that take a partial function: foldCollect?

Say, I have the following object: case class MyFancyObject(a: String, b: Int, c : Vector[String]) And what I needed is to get a single Vector[String] containing all 'c's that match a given partial function. E.g.: val xs = Vector( …
mdm
  • 3,928
  • 3
  • 27
  • 43
2
votes
3 answers

traverse_ equivalent in Python?

In Haskell, we have traverse_, a function that works on Foldable types to fold a structure with an applicative function, discarding any result: traverse_ :: (Applicative f,Foldable t) => (a -> f b) -> t a -> f () So we can for instance do…
phaazon
  • 1,972
  • 15
  • 21
2
votes
0 answers

Emulate "burning bridges" on GHC 7.8

Is there a simple way to emulate the burning bridges proposal (also called foldable/traversable proposal, part of GHC 7.10) on GHC 7.8 or possibly older? Some aspects are genuinely hard to emulate. This includes the changing in class hierarchies.…
Helmut Grohne
  • 6,578
  • 2
  • 31
  • 67
2
votes
6 answers

How can I fold with state in Haskell?

I have a simple function (used for some problems of project Euler, in fact). It turns a list of digits into a decimal number. fromDigits :: [Int] -> Integer fromDigits [x] = toInteger x fromDigits (x:xs) = (toInteger x) * 10 ^ length xs + fromDigits…
ruben.moor
  • 1,876
  • 15
  • 27