witch
witch copied to clipboard
`Foldable f => From f ...` instances
Hi! Would generalizations of instances of the form From [...] be a welcome contribution? Are there reasons to not generalize those?
My example use case:
instance (Foldable f, Ord k) => From (f (k, v)) (Map k v) where
from = M.fromList . toList
-- usage:
header :: Vector Name = ...
headerLengths = into @(Map Name Int) $ (id &&& BS.length) <$> header
This saves a toList near <$> header, and allows the whole pipeline to work in the original functor (here: Vector) until we actually need the list. WDYT?
Thanks for opening this suggestion! I generally support adding new instances to Witch.
That being said, won't this instance overlap with the list instance?
https://github.com/tfausak/witch/blob/461bf44506ea822100dc2277d43331fd6cee9786/source/library/Witch/Instances.hs#L976
Are you suggesting to replace that instance with this one, or something else?
Good point - the overlap didn't surface for me b/c I did not have the usage of above instance in my code - which your tests helpfully cover :)
My initial thought would be to replace the instance. What's your stance on that?
I’m not necessarily opposed to replacing the instance with a more polymorphic one. So far I’ve tried to avoid polymorphic instances where possible. For example there’s no instance for (Integral a, Integral b) => From a b, but there is an instance for From Int8 Int16. I have found this to be useful for documentation for two reasons: one, it’s immediately clear which types you can convert; and two, if there’s something tricky about a conversion I can document that directly on the instance.
Sorry, clearly I haven't made any progress here. I'm going to close this issue, but I'd be open to a PR introducing this instance.