arrow-kt / arrow

Λrrow - Functional companion to Kotlin's Standard Library
http://arrow-kt.io
Other
6.13k stars 442 forks source link

Delicate optics #3454

Closed serras closed 2 months ago

serras commented 2 months ago

(Raised by this Slack conversation)

This PR brings a couple of unlawful optics, namely filtering values and aggregating. These are potentially dangerous in different ways, so they need to be opted-in using DelicateOptic.

@nomisRev I've also renamed the filter functions in the dsl package to filterIndex, as that's the place they make the filtering. That way the new one can get the filter name, which coincides which what you expect from a collection / traversal. AFAIK that filter in the DSL hadn't been released yet, so should be OK changing it.

github-actions[bot] commented 2 months ago

Kover Report

File Coverage [33.33%]
arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Delicate.kt 42.86%
arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/dsl/delicate.kt 100.00%
arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/dsl/filterIndex.kt 0.00%
Total Project Coverage 60.66%
kyay10 commented 2 months ago

Can we have a test showcasing what happens when the optics are used incorrectly? Not super knowledgable about the theory, so I'd like to have examples of incorrect usage and to codify the current behaviour so that it doesn't change unexpectedly

serras commented 2 months ago

Can we have a test showcasing what happens when the optics are used incorrectly? Not super knowledgable about the theory, so I'd like to have examples of incorrect usage and to codify the current behaviour so that it doesn't change unexpectedly

I've added the information to the documentation. Furthermore, I've realized that it should not be a prism, but simply an optional. The example was taken from Haskell's optics package.

nomisRev commented 2 months ago

Furthermore, I've realized that it should not be a prism, but simply an optional.

Cool, I was wondering about that given there was no "constructing" going on.