Questions tagged [traversable]

In Haskell, class of data structures that can be traversed from left to right, performing an action on each element. Defined in Data.Traversable.

In Haskell, class of data structures that can be traversed from left to right, performing an action on each element.

Defined in Data.Traversable.

76 questions
7
votes
2 answers

Why can't a Traversable visit its elements more than once?

I remember reading somewhere that a type like this one can't be Traversable: data Bar a = Bar a deriving(Show) instance Functor Bar where fmap f (Bar x) = Bar (f x) instance Foldable Bar where foldMap f (Bar x) = f x <> f x The bit of the…
7
votes
1 answer

What does the Naturality law for Traversables mean?

The Naturality law states that: t . traverse f == traverse (t . f) -- for every applicative transformer t Now for the RHS of the law, if f has the type Applicative a => x -> a y, then t has to be of the type (Applicative a, Applicative b) => a y ->…
user4132
  • 397
  • 1
  • 7
7
votes
2 answers

What does Traversable is to Applicative contexts mean?

I am trying to understand Traversable with the help of https://namc.in/2018-02-05-foldables-traversals. Somewhere the author mentioned the following sentence: Traversable is to Applicative contexts what Foldable is to Monoid values. What did…
softshipper
  • 32,463
  • 51
  • 192
  • 400
7
votes
3 answers

How to convert Option[Try[_]] to Try[Option[_]]?

I quite often use the function below to convert Option[Try[_]] to Try[Option[_]] but it feels wrong. Can be such a functionality expressed in more idiomatic way? def swap[T](optTry: Option[Try[T]]): Try[Option[T]] = { optTry match { case…
kopiczko
  • 3,018
  • 1
  • 17
  • 24
6
votes
0 answers

What subclasses of `Profunctor` does `FunList` support?

A FunList is a datatype invented by Twan van Laarhoven in this blog post. A minor variation given by Bartosz Milewski looks like this: data FunList a b t = Done t | More a (FunList a b (b -> t)) An interesting fact about this…
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
6
votes
1 answer

What should a "higher order Traversable" class look like?

In this answer I made up on the spot something which looks a bit like a "higher order Traversable": like Traversable but for functors from the category of endofunctors on Hask to Hask. {-# LANGUAGE RankNTypes #-} import Data.Functor.Compose import…
6
votes
4 answers

Mapping while showing intermediate states

I need a function that does this: >>> func (+1) [1,2,3] [[2,2,3],[2,3,3],[2,3,4]] My real case is more complex, but this example shows the gist of the problem. The main difference is that in reality using indexes would be infeasible. The List…
Danny Navarro
  • 2,733
  • 1
  • 18
  • 22
6
votes
3 answers

Testing Haskell traversable with a simple example

I am trying to traverse all members of a data structure in haskell using Data.Traversable, which is documented at the following…
toeplitz
  • 777
  • 1
  • 8
  • 27
5
votes
1 answer

Can ZipList be Distributive?

Base provides ZipList, which is just a wrapper for [] where <*> is based on zip instead of cartesian-product. This isn't the default because it's not consistent with the Monad [] instance, but some people find it more intuitive, and both behaviors…
ShapeOfMatter
  • 991
  • 6
  • 25
5
votes
2 answers

Is Traversable different for breadth-first and depth-first trees?

I have a Rose Tree structure and I wanted to write a Traversable instance for it. So I started with the following: data Tree a = Tree a [Tree a] deriving (Show) instance Functor Tree where fmap f (Tree x subs) = Tree (f x) (fmap (fmap f) subs) I…
Listerone
  • 1,381
  • 1
  • 11
  • 25
5
votes
1 answer

"Empty" traversable - does it make sense, is it provided in any library?

I'm writing a piece of Haskell code that uses the abstraction of Traversable. Behind this abstraction I want to be able to conceal all regular traversable structures like lists, trees, maps etc, with special case of Data.Functor.Identity.Identity as…
5
votes
4 answers

Is there an equivalent to head/tail for Foldable functors in general?

I would like to express the following Haskell code, using only functor algebra (i.e. - not relying on any specific container type, such as List): ys = zipWith (+) (head xs : repeat 0) (tail xs ++ [y]) It seems to me that there…
dbanas
  • 1,707
  • 14
  • 24
5
votes
1 answer

Can we use IterateAggregate or Iterator in foreach loop in php?

I am beginner to php and learning it from php.net. A note on that page(http://php.net/manual/en/class.traversable.php) says that: Internal (built-in) classes that implement this interface can be used in a foreach construct and do not need to…
user8693138
5
votes
1 answer

What's the most standard/generic way to zip a traversable with a list?

Traversable is in a sense the class of containers whose structure has a “path” (that can correspond to a list), the elements on which can be modified without dissolving the structure. Hence zipTrav :: Traversable t => t a -> [b] -> Maybe (t…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
5
votes
0 answers

Understanding confusing

I've been thinking about whether there's some method that could be added to Traversable to make it compose more cheaply in the presence of expensive functors. The inspiration was Control.Lens.Traversal.confusing, which uses a special Applicative to…
dfeuer
  • 48,079
  • 5
  • 63
  • 167