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 'ToPairs' type family with the ability to have list of pairs #97

Closed
chshersh opened this issue Dec 17, 2017 · 2 comments
Closed

Add 'ToPairs' type family with the ability to have list of pairs #97

chshersh opened this issue Dec 17, 2017 · 2 comments

Comments

@chshersh
Copy link
Contributor

Currently toList for key-value containers returns list of values only:

ghci> :t toList @(HashMap _ _)
toList @(HashMap _ _) :: HashMap t1 t -> [t]

I think it might be more useful to return list of (key, value). This can be implemented as default here:

-- | Default implementation of 'Element' associated type family.
type family ElementDefault (t :: *) :: * where
ElementDefault (f a) = a

Like this for example:

type family ElementDefault (t :: *) :: * where
    ElementDefault (m k v) = (k, v)
    ElementDefault (f a)   = a

But this immediately will break behavior of all Foldable-like functions. Maybe this change is too radical...

@chshersh chshersh changed the title Make 'Element' type family for containers return return pair of '(key, value)' instead of just 'value' Make 'Element' type family for containers return pair of '(key, value)' instead of just 'value' Dec 17, 2017
@neongreen
Copy link
Contributor

This is very bad because it would break any code which uses toList as a synonym for elems, and in many cases it will do so silently. It will also break elem :: Element a -> a -> Bool.

Either create toPairs or just deprecate toList and instead add keys, elems, pairs.

@chshersh
Copy link
Contributor Author

@neongreen Yeah, one more thing is breaking something like:

maximum :: Ord (Element t) => t -> Element t

which doesn't make much sense... Solution with toPairs sounds better to me as well. Also, in that case we will be closer to Prelude interface thus less surprises to users.

So do you propose the following interface?

class ToPairs t where
    type Key t :: *
    type Val t :: *

    toPairs :: t -> [(Key t, Val t)]
    keys    :: t -> [Key t]
    elems   :: t -> [Val t]  -- maybe `vals` or `values`?

@chshersh chshersh changed the title Make 'Element' type family for containers return pair of '(key, value)' instead of just 'value' Add 'ToPairs' type family with the ability to have list of pairs Dec 23, 2017
vrom911 added a commit that referenced this issue Dec 27, 2017
vrom911 added a commit that referenced this issue Dec 28, 2017
vrom911 added a commit that referenced this issue Dec 28, 2017
vrom911 added a commit that referenced this issue Dec 29, 2017
chshersh added a commit that referenced this issue Dec 29, 2017
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

3 participants