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
20
votes
4 answers

Is it possible to get all contexts of a Traversable lazily?

lens offers holesOf, which is a somewhat more general and powerful version of this hypothetical function: holesList :: Traversable t => t a -> [(a, a -> t a)] Given a container, holesList produces a list of elements of the container along…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
14
votes
1 answer

Vector creation safety

This question is actually a small lattice of very closely related questions; I don't think it makes much sense to break it up as yet. One of the fundamental ways to create a Vector is using unsafeFreeze. As its name suggests, unsafeFreeze is truly…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
12
votes
1 answer

Foldable vs Traversable

While studying Applicative deeper, I came to Traversable. Although I already knew Foldable from LYHGG, I haven't seen the former yet, so I started reading the Haskell wiki about Traversable. While reading it, I understood why Foldable.fold is…
FtheBuilder
  • 1,410
  • 12
  • 19
12
votes
3 answers

Haskell lenses: how to make view play nicely with traverse?

I am trying to learn about lenses by implementing it in Haskell. I have implemented the view combinator as follows: {-# LANGUAGE RankNTypes #-} import Control.Applicative import Data.Traversable type Lens s a = Functor f => (a -> f a) -> s -> f…
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
11
votes
2 answers

Why are traversals defined over Applicatives, fundamentally?

I've been on a bit of a "distilling everything to its fundamentals" kick lately, and I've been unable to find clear theoretical reasons for how the Traversable typeclass is defined, only practical ones of "it's useful to be able to traverse over…
janet
  • 221
  • 1
  • 9
11
votes
1 answer

Infinite loop in bubble sort over Traversable in Haskell

I am trying to implement a bubble sort over any traversable container using the Tardis monad. {-# LANGUAGE TupleSections #-} module Main where import Control.DeepSeq import Control.Monad.Tardis import Data.Bifunctor import Data.Traversable import…
11
votes
2 answers

How to detect if object is Traversable in PHP?

How can I detect the variable is a Traversable object to use in foreach loops? if(is_traversable($variable)) { return (array) $variable; }
Milad Rahimi
  • 3,464
  • 3
  • 27
  • 39
10
votes
2 answers

What should a Traversable instance look like for a Tree datatype with a nested Maybe value?

I have a Haskell exam in three days, so I thought I should practice a little and pulled up past exams, one of which features the following Tree datatype: data Tree a = Leaf1 a | Leaf2 a a | Node (Tree a) (Maybe (Tree a)) deriving (Eq, Ord, Show) It…
kleinerbur
  • 103
  • 5
10
votes
1 answer

How to convert a Haskell Traversable into a Vector?

If I have a Traversable instance, xs, how do I convert it into a Vector?
user1002430
9
votes
3 answers

How does Traversable use the fact that it subclasses both Foldable and Functor?

class (Functor t, Foldable t) => Traversable t where traverse :: Applicative f => (a -> f b) -> t a -> f (t b) traverse g = sequenceA . fmap g sequenceA :: Applicative f => t (f a) -> f (t a) sequenceA = traverse id How does…
Tim
  • 1
  • 141
  • 372
  • 590
8
votes
1 answer

Stream to be an instance of traversable

The vector-0.1 package has a quite efficient Stream implementation (Data.Vector.Stream): data Step s a = Yield a s | Skip s | Done -- | The type of fusible streams data Stream a = forall s. Stream (s -> Step s a) s…
mcmayer
  • 1,931
  • 12
  • 22
8
votes
4 answers

Why isn't the state monad traversable?

Intuitively, it seems to me that one could use traverse with a state monad, for example: traverse (\a -> [a, a+1]) (state (\s -> (1, s + 1))) = [state (\s -> (1, s + 1), state (\s -> (2, s + 1)] But the state monad doesn't implement the…
Guillaume Chérel
  • 1,478
  • 8
  • 17
8
votes
1 answer

What is the point of 'fmapDefault' in 'Data.Traversable'?

I'm looking at the documentation for Data.Traversable and came across fmapDefault - https://downloads.haskell.org/~ghc/latest/docs/html/libraries/base/Data-Traversable.html#g:3 fmapDefault :: Traversable t => (a -> b) -> t a -> t b The…
Anupam Jain
  • 7,851
  • 2
  • 39
  • 74
8
votes
2 answers

What would be the "distinct method" that Traversable has in addition to Foldable?

Foldable is a superclass of Traversable, similarly to how Functor is a superclass of Applicative and Monad. Similar to the case of Monad, where it is possible to basically implement fmap as liftM :: Monad m => (a->b) -> m a -> m b liftM f q = return…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
7
votes
0 answers

Does a joined Bitraversable require Monad?

Despite the jargon filled title I don't think this question is very complex. Introducing the characters There are two important Functor combinators at play here. Flip equivalent to the haskell functiong flip but operating on types newtype Flip p a…
Wheat Wizard
  • 3,982
  • 14
  • 34
1
2 3 4 5 6