typelevel / typelevel/cats

Named version of `mapN` and friends

Open
#4,792 0 comments 1 reaction 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

I wonder if we could have a Scala 3 only version of mapN and friends that work on NamedTuples instead of plain ones.

The motivation would be to fix the major usability issue with applicative composition, where operations and the names of their results are separated.

For example, imagine a very simple for comprehension like this:

final case class Config(foo: Foo, bar: Bar, baz: Baz, quax: Quax)
object Config {
  def load: IO[Config] =
    for {
      foo <- env(name = "FOO").as[Foo]
      bar <- env(name = "BAR").as[Bar]
      baz <- env(name = "BAZ").as[Baz]
      quax <- env(name = "QUAX").as[Quax]
    } yield Config(foo, bar, baz, quax)
}

One may realize that there is no dependency between each operation, and thus that instead of failing fast, we could try to read all env vars "concurrently" and accumulate all the errors instead:

def load: IO[Config] =
  (
    env(name = "FOO").as[Foo],
    env(name = "BAR").as[Bar],
    env(name = "BAZ").as[Baz],
    env(name = "QUAX").as[Quax]
  ).parMapN { case (foo, bar, baz, quax) =>
    Config(foo, bar, baz, quax)
  }

But, this disconnects the names (foo, bar, baz, quax) from the operations that produced them; and yes, in this case, you could just do parMapN(Config.apply) but that is besides the point.

Thus, it would be great if we could use NamedTuples and maybe Structural Types to get an API like this:

def load: IO[Config] =
  (
    foo = env(name = "FOO").as[Foo],
    bar = env(name = "BAR").as[Bar],
    baz = env(name = "BAZ").as[Baz],
    quax = env(name = "QUAX").as[Quax]
  ).namedParMapN { result =>
    Config(result.foo, result.bar, result.baz, result.quax)
  }

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

Start by reading the Scala 3 Named Tuples and Structural Types documentation linked in the issue, then locate Cats' existing mapN and parMapN APIs. The issue names no files or tests; done would require a decided Scala 3 API for named applicative composition and corresponding validation.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
api, backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.