In Control.Lens.Traversal
the beside
function traverses both parts of a Bitraversable
. The example given is
>>> ("hello",["world","!!!"])^..beside id traverse
["hello","world","!!!"]
Can I write a more explicit version of beside
(let's call it bothParts
) that instead of a Bitraversable
constraint takes two Traversal
s? I imagine it to be used like so:
>>> ("hello",["world","!!!"])^..bothParts _1 _2 id traverse
["hello","world","!!!"]
Does this already exist? Is this too unsafe to be sanely used? Thank you!
Edit:
Or perhaps something like:
>>> ("hello",["world","!!!"])^..bothParts _1 (_2.traverse)
["hello","world","!!!"]
The combinator you want is supposed to use 2 Traversal
s simultaneously. But that kind of combinator breaks Traversal
laws in general, in particular the "no duplication" law: a Traversal
should traverse each element only once.
Here's an example of what you probably don't want:
>>> (1, 2) ^.. bothParts _1 _1
[1, 1]
To be more precise, I'd like to cite Traversal
documentation from lens
package:
The laws for a
Traversal t
follow from the laws forTraversable
as stated in "The Essence of the Iterator Pattern".
t pure ≡ pure
fmap (t f) . t g ≡ getCompose . t (Compose . fmap f . g)
One consequence of this requirement is that a
Traversal
needs to leave the same number of elements as a candidate for subsequentTraversal
that it started with. Another testament to the strength of these laws is that the caveat expressed in section 5.5 of the "Essence of the Iterator Pattern" about exoticTraversable
instances that traverse the same entry multiple times was actually already ruled out by the second law in that same paper!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With