optics-dev / optics-dev/Monocle

`filter` and `filterIndex` names are confusing

Open
#1,023 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.x
Dominant language
Scala
Stars
1.7k
Forks
207
Avg merge
7h 59m
Merged PRs (30d)
5

Description

filter and filterIndex are two optic combinators with similar name and behaviour but different types, which may be confusing.

val siblings: Lens[User, List[String]] = ...

siblings.get(user) 
// res: List[String] = List( "Zoe", "Jo", "Eda")

// select list with more than two elements
siblings.filter(_.length > 2).getOption(user)
// res: Option[List[String]] = Some(List( "Zoe", "Jo", "Eda"))

// select names with more than two characters
siblings.each.filter(_.length > 2).getAll(user)
// res: List[String] = List( "Zoe", "Eda")

// select all names except the first (index == 0 means head)
siblings.filterIndex(_ >= 1).getAll(user)
// res: List[String] = List("Jo", "Eda")

It may be easier to see the confusion when we check the type of filter and filterIndex for List

filter     (predicate): Optional [List[A], List[A]]
filterIndex(predicate): Traversal[List[A], A]

Contributor guide

No contributing guide indexed for this repository

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 locating the definitions and usages of the Scala optic combinators filter and filterIndex, then review the existing comment discussion before proposing a direction. Done requires an agreed naming change or documented decision that resolves the confusion between their types and behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
backend-api-design
Issue type
Refactor
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.