Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add intersections, intersectionsWith #359

Open
Icelandjack opened this issue Nov 23, 2016 · 5 comments
Open

Add intersections, intersectionsWith #359

Icelandjack opened this issue Nov 23, 2016 · 5 comments

Comments

@Icelandjack
Copy link

Data.Map and Data.IntMap have the functions

unions     :: Ord k =>                  [Map k a] -> Map k a
unionsWith :: Ord k => (a -> a -> a) -> [Map k a] -> Map k a 

unions     ::                  [IntMap a] -> IntMap a 
unionsWith :: (a -> a -> a) -> [IntMap a] -> IntMap a 

should there be corresponding functions for intersection?

intersections     :: Ord k =>                  [Map k a] -> Map k a
intersectionsWith :: Ord k => (a -> a -> a) -> [Map k a] -> Map k a 

intersections     ::                  [IntMap a] -> IntMap a 
intersectionsWith :: (a -> a -> a) -> [IntMap a] -> IntMap a 

They could all be given Foldable constraints.

There are some similar functions, from data-interval

intersections :: (Ord r, Num r) => [Interval r] -> Interval r 

and fixplate

intersectionsWith :: (Ord hash, Ord k) => (v -> v -> v) -> [HashTable hash k v] -> HashTable hash k v 
@treeowl
Copy link
Contributor

treeowl commented Nov 23, 2016

I don't really see any need for unions; what's wrong with foldl' union mempty? intersections as you propose it is much worse, because it's
partial. Specifically, intersections [] = _|_. You could certainly work
around this in various ways, but I don't, as yet, see the point. Am I
missing something?

On Nov 23, 2016 3:05 AM, "Icelandjack" [email protected] wrote:

Data.Map and Data.IntMap have the functions

unions :: Ord k => [Map k a] -> Map k a
unionsWith :: Ord k => (a -> a -> a) -> [Map k a] -> Map k a
unions :: [IntMap a] -> IntMap a unionsWith :: (a -> a -> a) -> [IntMap a] -> IntMap a

should there be corresponding functions for intersection?

intersections :: Ord k => [Map k a] -> Map k a
intersectionsWith :: Ord k => (a -> a -> a) -> [Map k a] -> Map k a
intersections :: [IntMap a] -> IntMap a intersectionsWith :: (a -> a -> a) -> [IntMap a] -> IntMap a

They could all be given Foldable constraints.

There are some similar functions, from data-interval
https://hackage.haskell.org/package/data-interval-0.6.0/candidate/docs/Data-Interval.html#v:intersections

intersections :: (Ord r, Num r) => [Interval r] -> Interval r

and fixplate
https://hackage.haskell.org/package/fixplate-0.1.6/docs/Data-Generics-Fixplate-Util-Hash-Table.html#v:intersectionsWith

intersectionsWith :: (Ord hash, Ord k) => (v -> v -> v) -> [HashTable hash k v] -> HashTable hash k v


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#359, or mute the thread
https://github.com/notifications/unsubscribe-auth/ABzi_U97Wj9D7g-d_KizoOIY2-POLY2Iks5rA_PUgaJpZM4K6Tm3
.

@Icelandjack
Copy link
Author

unions, unionsWith are simple folds that are in containers already. I didn't think deeply about this but we can get around the empty case, as you said

intersections     ::                  NonEmpty (IntMap a) -> IntMap a 
intersectionsWith :: (a -> a -> a) -> NonEmpty (IntMap a) -> IntMap a

Is this more/less useful than unions, unionsWith? Who knows

@phadej
Copy link
Contributor

phadej commented Dec 13, 2016

NonEmpty is only in base-4.9, otherwise intersectionsWith would be nice indeed, for completeness. Though bike-shedding: unions is []-specialised fold, and I use UnionsWith, I can imagine newtype for intersection as well.

@fumieval
Copy link
Contributor

fumieval commented Jun 6, 2020

Somewhat related: #699

@sjakobi
Copy link
Member

sjakobi commented Jul 15, 2020

Also related: #137

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants