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 FoldableWithKey and TraversableWithKey #614

Open
masaeedu opened this issue Mar 20, 2019 · 3 comments
Open

Add FoldableWithKey and TraversableWithKey #614

masaeedu opened this issue Mar 20, 2019 · 3 comments

Comments

@masaeedu
Copy link
Member

masaeedu commented Mar 20, 2019

It'd be handy to have something like foldMapWithKey so you can do:

const replaceAll = foldMapWithKey(Endo)(k => v => s => s.replace(k, v))

const f = replaceAll({
  "Earth": "Jupiter",
  "Mars": "Earth"
})

f("Come in Earth, this is Mars speaking")
// => "Come in Jupiter, this is Earth speaking"

Similarly traverseWithKey.

@davidchambers
Copy link
Member

That looks neat, @masaeedu! What are the types of the two functions?

@masaeedu
Copy link
Member Author

@davidchambers Weird, looks like I edited away the types I just edited in. Here they are:

type FoldableWithKey k f = {
  foldMapWithKey : Monoid m -> (k -> v -> m) -> f v -> m
}

and:

type TraversableWithKey k f = {
  traverseWithKey : Applicative f -> (k -> a -> f b) -> t a -> f (t b)
}

@masaeedu
Copy link
Member Author

masaeedu commented Mar 20, 2019

I'm actually still a little bit torn about whether it's a better idea to have foldMapWithKey and traverseWithKey or just: type WithKey k f = { withKey : f a -> f (k, a) }. If you do it the second way you can implement traverseWithKey just once for anything that happens to be both Traversable and WithKey (although of course you have to implement withKey for everything).

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

No branches or pull requests

2 participants