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
5
votes
1 answer

Why is this instance of Traversable for list not correct?

The below code fails the checkers test for traversable. I'd appreciate an explanation of why it fails, not just how to fix it. import Test.QuickCheck import Test.QuickCheck.Checkers import Test.QuickCheck.Classes data List a = Nil | Cons a…
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
4
votes
1 answer

Is explicit type passing not equivalent to type inference (in terms of expressiveness)?

I try to translate traverse/sequenceA to Javascript. Now the following behavior of the Haskell implementation gives me trouble: traverse (\x -> x) Nothing -- yields Nothing sequenceA Nothing -- yields Nothing traverse (\x -> x) (Just [7]) -- yields…
user6445533
4
votes
3 answers

Order of execution with Haskell's `mapM`

Consider the following Haskell statement: mapM print ["1", "2", "3"] Indeed, this prints "1", "2", and "3" in order. Question: How do you know that mapM will first print "1", and then print "2", and finally print "3". Is there any guarantee that it…
George
  • 6,927
  • 4
  • 34
  • 67
4
votes
1 answer

what does the A stand for in sequenceA?

What does sequenceA from Traversable stand for? Why is there capital A at the end? I've been learning Haskell for a few months now and this is one of those things that's been bugging me for a while.
Matt Hamrick
  • 759
  • 5
  • 10
3
votes
1 answer

Is it impossible to get the depth of elements inside a Traversable?

Suppose I have this Tree type: {-# LANGUAGE DeriveFoldable, DeriveFunctor #-} data Tree a = Leaf | Branch (Tree a) a (Tree a) deriving(Functor,Foldable) instance Traversable Tree where -- equivalent to the one I could derive, but written out for…
3
votes
2 answers

How to convert function returning [] to Traversable?

I have the following module which implements a directory walk: module Walk ( walk ) where import Control.Monad import Control.Monad.IO.Class import Data.List import System.Directory import …
Listerone
  • 1,381
  • 1
  • 11
  • 25
3
votes
1 answer

Is there a corresponding optic for higher-order traversable functors?

Hedgehog has an HTraversable class defined like this: -- | Higher-order traversable functors. -- class HTraversable t where htraverse :: Applicative f => (forall a. g a -> f (h a)) -> t g -> f (t h) Which is used with their Var type for…
3
votes
2 answers

Why does 'for' from Data.Traversable accept monadic actions?

I was working on the following small fragment of code: import Control.Monad import Data.Aeson import qualified Data.HashMap.Strict as HashMap import Data.Map (Map) import qualified Data.Map as Map import …
ppb
  • 905
  • 6
  • 9
3
votes
1 answer

Is Traversable a trait or a class

Question 1 - In the Scala documentation, I found that Traversable is a trait with an abstract method foreach: http://www.scala-lang.org/docu/files/collections-api/collections.html Then, why could I instantiate an object of type Traversable? val t =…
Manu Chadha
  • 15,555
  • 19
  • 91
  • 184
3
votes
1 answer

Traversable for data Constant a b = Constant a passes Quickchecks, but acts funny

The haskell book wants me to implement the traversable instance for newtype Constant a b = Constant { getConstant :: a } including all necessary superclasses. The code below passes Quickcheck/Checkers, but acts funny import Test.QuickCheck import…
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
2
votes
1 answer

Is there a name for this higher-level "bi" version of distribute in Haskell?

I have a Bitraversable called t that supports this operation: someName :: Monad m => (t (m a) (m b) -> c) -> m (t a b) -> c In other words, it's possible to take a function that accepts two monads packaged into the bitraversable and turn it into a…
jacobsa
  • 5,719
  • 1
  • 28
  • 60
2
votes
2 answers

in Haskell, how to replace elements of a pair in the presence of Maybe values?

Consider these two functions in Haskell: replace_snd :: b -> Maybe (a, b) -> Maybe (a, b) replace_snd y' (Just (x, y)) = Just (x, y') replace_snd _ Nothing = Nothing inject_snd :: Maybe b -> (a, b) -> Maybe (a, b) inject_snd (Just b') (a, b) = Just…
Adam Dingle
  • 3,114
  • 1
  • 16
  • 14
2
votes
3 answers

Building a list from a non-traversable without recursion

I can build a data structure that is a member of the Traversable typeclass (e.g. List or Map), by mapping (map, mapM) or folding (foldl, foldM) another traversable data structure. However, I often encounter situations where I need to build a…
category
  • 2,113
  • 2
  • 22
  • 46
2
votes
1 answer

How to model this recurisve structure in Haskell?

I am trying to model the kdb/q "atoms and lists" through Haskell type system. In kdb/q all data is built from atoms. An atom is an irreducible value of a specific data type. Int, boolean and char are examples of atoms. Lists are ordered collections…
dhu
  • 718
  • 6
  • 19
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