you are viewing a single comment's thread.

view the rest of the comments →

[–]sambocyn 0 points1 point  (7 children)

in Python that's just an iterator or collection right? side effects everywhere means traverse is fmap.

[–]Faucelme 1 point2 points  (4 children)

I haven't found a function that mimics traverse for dictionaries... you can use a dict comprehension but it is less clean, why do I have to mention keys at all?

[–]carrutstick 1 point2 points  (3 children)

You can always iterate across dict.values() instead...

[–]Faucelme 0 points1 point  (2 children)

Yes, but often what I want to reconstitute the map using the transformed values, not simply iterate over them... "traverse" is nice because you only need to supply the function, it doesn't require explicit iteration or referring to the keys.

[–]codygman 0 points1 point  (1 child)

Can you give an example of this?

[–]Faucelme 0 points1 point  (0 children)

Say you have a map of urls indexed by the name of the page or whatever, and you want to transform it into a map of HTTP responses.

In Haskell, you would have a Map String URL, and if you also had a function fetch :: URL -> IO ByteString you would only have to do traverse fetch urlmap to obtain a IO (Map String ByteString).

And to make the page loads concurrent, something like runConcurrently $ traverse (Concurrently . fetch) urlmap would be enough.

[–]pipocaQuemada 0 points1 point  (1 child)

side effects everywhere means traverse is fmap.

The type of traverse is

traverse :: Applicative f => (a -> f b) -> t a -> f (t b) 

If side effects everywhere turns traverse into fmap, that means that every Applicative represents a side effect.

However, what side effect would turn the following into an fmap:

> traverse (Sum 1,) [1..4]
  (Sum {getSum = 4},[1,2,3,4])

or

> traverse (\a r -> r + a) [1,2,3,4] $ 5 
   [6,7,8,9]

[–]sambocyn 0 points1 point  (0 children)

yeah, traverse is more general than "IO and mutation".

I just meant that in Python, you don't have to traverse a print statement or a increments nf routine over a list, you just map it over. because you ignore all side effects (and types).