Skip to content

nonEmptyTraverse for NEM with key mapping #4759

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dpevunov-cp
Copy link

No description provided.

@satorg
Copy link
Contributor

satorg commented Jul 15, 2025

Hi @dpevunov-cp , thank you for the PR!

I think I understand what you're trying to achieve, but I've got a couple of suggestions about the proposed method:

  1. Cats generally avoids overloading method names that only differ in type parameters or type classes. I'd suggesst name nonEmptyTraverseBoth (which would be similar to map/mapBoth)
  2. Tests are highly appreciated. For example, it may make sense to check that nem.nonEmptyTravese(f) <=> nem.toNel.traverse(f).map(_.toNem) or whatever works best for this case.

if (t.isEmpty)
Eval.now(G.map(f.tupled(h))(b => NonEmptyMap(b, SortedMap.empty[L, B](ordL.toOrdering))))
else
G.map2Eval(f.tupled(h), Eval.defer(loop(t.head, t.tail)))((lb, acc) => NonEmptyMap(lb, acc.toSortedMap))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wrap and unwrap with NonEmptyMap on every step? Why not do it once at the end after calling .value?

@@ -261,6 +261,18 @@ sealed class NonEmptyMapOps[K, A](private[data] val value: NonEmptyMap[K, A]) {
loop(head, tail).value
}

def nonEmptyTraverse[G[_], L, B](
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this name isn't right. Traverse on maps only uses the values, not the keys. So if you are going to use the keys I think we need a new name. Something like nonEmptyTraversePairs or traverseWithKey or something.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NonEmptyMap already has map (values only) and mapBoth (keys with values). So I guess this should be nonEmptyTraverseBoth then.

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

Successfully merging this pull request may close these issues.

3 participants