Skip to content

Commit

Permalink
add Order instance for AtLeast2
Browse files Browse the repository at this point in the history
  • Loading branch information
vreuter committed Nov 13, 2024
1 parent c6d608e commit 0e6efd5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions modules/pan/src/main/scala/collections.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ object collections:
/** Define equality the same way as for the underlying, unrefined value. */
given eqForAtLeast2[C[*], E](using Eq[C[E]]): Eq[AtLeast2[C, E]] = Eq.by(es => es: C[E])

/** Define order the same way as for the underlying, unrefined value. */
given orderForAtLeast2[C[*], A](using Order[C[A]]): Order[AtLeast2[C, A]] =
Order.by: xs =>
xs: C[A]

/** Simply concatenate the elements from the left list to the head of the right list. */
given semigroupKForAtLeast2List: SemigroupK[AtLeast2List] = new:
override def combineK[A](xs: AtLeast2List[A], ys: AtLeast2List[A]): AtLeast2List[A] =
Expand Down
9 changes: 9 additions & 0 deletions modules/pan/src/test/scala/TestAtLeast2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,13 @@ class TestAtLeast2
forAll: (xs: AtLeast2[Set, Int]) =>
xs.toNes shouldEqual NonEmptySet.fromSetUnsafe(xs.toSortedSet)

test("When syntax is imported, AtLeast2[Set, A] may be represented as simply Set[A]."):
assertCompiles("AtLeast2.unsafe(Set(1, 2))") // Precondition: we can build the collection.
assertTypeError(
"AtLeast2.unsafe(Set(1, 2)).toSet"
) // Test: without syntax import, .toNes is unavailable.
assertCompiles(
"import AtLeast2.syntax.toSet; AtLeast2.unsafe(Set(1, 2)).toSet"
) // Test: with syntax import, .toNes becomes available.

end TestAtLeast2

0 comments on commit 0e6efd5

Please sign in to comment.