https://kotlinlang.org logo
#arrow
Title
# arrow
g

Gemy

09/30/2023, 6:39 PM
I have a question about Optics How to get a focus on an item in a list, that's a property of an optional (sealed subtype) In this code
Copy code
private val lWeatherListState = WeatherScreenModel.weatherList.state
private val lEveryWeatherItem = lWeatherListState compose Prism<Lce<String, List<WeatherItemModel>>, Lce.Content<List<WeatherItemModel>>>(
        getOption = { (it as? Lce.Content<List<WeatherItemModel>>).toOption() },
        reverseGet = { it }
    ).value().every(Every.list())
Here I want to further focus.. on items after `every`` But all I found was an index function.. is there a way? Or is there something missing? I've tried the docs but it didn't cover that part.
a

Alejandro Serrano.Mena

10/01/2023, 6:37 AM
You can use
index
(in arrow.optics.dsl). You can find examples in the arrow-kt.io website, under Immutable Data > Optionals > Indexed collections
g

Gemy

10/01/2023, 7:33 AM
Thanks! @Alejandro Serrano.Mena what if I want to focus on an element matching a certain criteria, perhaps a certain ID? Can index do that? For lists I can only focus on index using
index
@simon.vergauwen What do you think, sir.
a

Alejandro Serrano.Mena

10/02/2023, 11:09 AM
one could get a
Fold
in that way (getting them is easy, not sure what "setting" means in this case)
g

Gemy

10/02/2023, 11:17 AM
Is there an example somewhere for fold, sir?
a

Alejandro Serrano.Mena

10/02/2023, 11:37 AM
even better, it turns out we already have it in the library! https://apidocs.arrow-kt.io/arrow-optics/arrow.optics/-fold/-companion/select.html
g

Gemy

10/02/2023, 11:59 AM
That's amazing! Thank you! What happens if the predicate returns true for two or more elements in
Every
?
a

Alejandro Serrano.Mena

10/02/2023, 12:02 PM
that's ok, because a
Fold
may focus on more than one element
g

Gemy

10/02/2023, 12:04 PM
That's very convenient 🙏 Is there a good resource for finding examples covering the multiple use cases of Arrow-Optics? Because i found no hint of Fold in the documentation at https://arrow-kt.io/learn
and the API docs got no examples for each type
a

Alejandro Serrano.Mena

10/02/2023, 12:05 PM
Traversals are the "get + set" version of Fold https://arrow-kt.io/learn/immutable-data/traversal/
as described at the end of https://arrow-kt.io/learn/immutable-data/intro/#many-optics-to-rule-them-all, we plan to remove half of the optics in 2.0, that's why some are not so well documented
g

Gemy

10/02/2023, 12:09 PM
Oh, okay, nice read That means when using arrow v2 i'll have to refactor my code that's using lenses and prisms?
Is there a way to try arrow v2 right now?
a

Alejandro Serrano.Mena

10/02/2023, 12:11 PM
you'll only need to refactor if you use fold, getter, or setter. The rest (lens, optional, prism, traversal) stay Everything we'll remove is marked as Deprecated in version 1.2.x. So if you use that version and your code compiles without warnings, you're good to go
g

Gemy

10/02/2023, 12:13 PM
I'm on v1.2.1 And fold usage has no warnings
a

Alejandro Serrano.Mena

10/02/2023, 12:14 PM
mm 🤔 I'll try to have a look at the reason during the week
g

Gemy

10/02/2023, 12:14 PM
Thanks a lot for your time 🙏 btw can you tell me if i'm using the prism correctly here? Or there is an easier way to access sealed class nested properties?
a

Alejandro Serrano.Mena

10/02/2023, 12:19 PM
you are, but I recommend you to define the prism
lceContent
separately,
Copy code
val lceContent: Prism<...> = Prism(
  getOption = ...
  reverseGet = ...
)
and then define
Copy code
val <A> Optional<A, Lce<String, List<WeatherItemModel>>.lceContent: Optional<A, Lce.Content<List<WeatherItemModel>>> = this compose lceContent
so you can write
Copy code
lWeatherListState.lceContent.value().every(Every.list())...
g

Gemy

10/02/2023, 12:20 PM
That's much better. Thanks!
4 Views