Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

nonEmptyTraverse for NEM with key mapping - #4759

#4759
Open
dpevunov-cp wants to merge 3 commits into
typelevel:maintypelevel/cats:mainfrom
dpevunov-cp:maindpevunov-cp/cats:mainCopy head branch name to clipboard
Open

nonEmptyTraverse for NEM with key mapping#4759
dpevunov-cp wants to merge 3 commits into
typelevel:maintypelevel/cats:mainfrom
dpevunov-cp:maindpevunov-cp/cats:mainCopy head branch name to clipboard

Conversation

@dpevunov-cp

Copy link
Copy Markdown

No description provided.

@satorg

satorg commented Jul 15, 2025

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

fixed

loop(head, tail).value
}

def nonEmptyTraverse[G[_], L, B](

Copy link
Copy Markdown
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
Copy Markdown
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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

fixed

@dpevunov-cp
dpevunov-cp requested review from johnynek and satorg March 17, 2026 19:48
@satorg

satorg commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

@dpevunov-cp , thank you for you commitment to the task!

I'd really appreciate if you could add one or more tests to NonEmptyMapSuite for that new method.

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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.