I have a question about Optics How to get a focus...
# arrow
g
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
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
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
one could get a
Fold
in that way (getting them is easy, not sure what "setting" means in this case)
g
Is there an example somewhere for fold, sir?
a
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
That's amazing! Thank you! What happens if the predicate returns true for two or more elements in
Every
?
a
that's ok, because a
Fold
may focus on more than one element
g
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
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
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
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
I'm on v1.2.1 And fold usage has no warnings
a
mm 🤔 I'll try to have a look at the reason during the week
g
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
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
That's much better. Thanks!