typelevel / typelevel/cats

Positive integers and non-empty collections

Open
#3,529 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
5.5k
Forks
1.2k
Avg merge
2d 10h
Merged PRs (30d)
5

Description

Positive integers and non-empty collections

Hi all. cats implements non-empty collections such as NonEmptyList. I think we might squeeze a little more out of the concept. Wouldn't it be nice to have a type PosInt (for positive integers) and define

def size[A](nel: NonEmptyList[A]): PosInt

Moreover, this would allow us to add safer alternatives to functions which accept Int and produce a List. E. g. replicateA

def replicateA[A](n: PosInt, fa: F[A]): F[NonEmptyList[A]]

The use case I had in mind

Imagine a user who wants to generate random values and estimate their mean. To that end the user defines

type Random[T] = State[Gen, T]
val random: Random[T] = ...
val gen: Gen = ...

def average[T](nel: NonEmptyList[T]) : T = nel.reduce / size(nel)

Here we implicitly assume T is a semigroup and also defines division operator def /(n: PosInt): T.

Now it would make sense to code

average(replicateA(PosInt(100) , random).runA(gen))

I'd appreciate hearing your thoughts.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue proposes a PosInt type and safer alternatives involving NonEmptyList, size, and replicateA, but names no files or tests. Start by reviewing those existing APIs and the surrounding Cats design; the work is done only after the proposed types and signatures have an agreed design and corresponding implementation and tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.