Skip to content

Commit

Permalink
add .head as syntax extension to AtLeast2[List, *]
Browse files Browse the repository at this point in the history
  • Loading branch information
vreuter committed Oct 26, 2024
1 parent 0a59b37 commit bf6a262
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions modules/pan/src/main/scala/collections.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ object collections:
*/
def contains(x: X): Boolean = (xs: List[X]).contains(x)

/** Access the first element of the collection. */
def head: X = (xs: List[X]).head

/** We can safely convert to [[cats.data.NonEmptyList]]. */
def toNel: NonEmptyList[X] = NonEmptyList(xs.head, xs.tail)

Expand Down
5 changes: 5 additions & 0 deletions modules/pan/src/test/scala/TestAtLeast2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ class TestAtLeast2 extends AnyFunSuite, ScalaCheckPropertyChecks, should.Matcher
"import AtLeast2.syntax.map; AtLeast2.unsafe(List(0, 1, 2)).map(_ + 1): AtLeast2[List, Int]"
) // With the proper syntax import, the .map operation works.

test("Wrapping a list in the AtLeast2 refinement doesn't change the .head element."):
import AtLeast2.syntax.head
forAll: (x: Int, xs: NonEmptyList[Int]) =>
AtLeast2(x, xs).head shouldEqual x

end TestAtLeast2

0 comments on commit bf6a262

Please sign in to comment.