Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyMapImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.

f: (K, A) => G[(L, B)]
)(implicit G: Apply[G], ordL: Order[L]): G[NonEmptyMap[L, B]] = {
def loop(h: (K, A), t: SortedMap[K, A]): Eval[G[NonEmptyMap[L, B]]] =
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?


loop(head, tail).value
}

/**
* Typesafe stringification method.
*
Expand Down