Nikita Volkov
Nikita Volkov

Reputation: 43310

Optic from map to its association pairs

I need a Fold (Map k v) (k, v) optic. Is there a way to obtain it with the "optics" library? If not, is there one with "lens"?

Upvotes: 2

Views: 69

Answers (1)

K. A. Buhr
K. A. Buhr

Reputation: 50929

In either optics or lens, you should be able to write:

pairs :: Fold (Map k v) (k, v)
pairs = folding Map.toList

Generally speaking folding takes any function that produces a foldable result (in this case, a list of key-value pairs) and creates a fold over those elements.

Upvotes: 3

Related Questions