Is there something like an `At` that returns an `O...
# arrow
s
Is there something like an
At
that returns an
Optional
instead of a
Lens
? I'm basically looking to achieve something like this:
Copy code
@optics
data class Comments(val data: List<Comment>)

@optics
data class Comment(val id: String, val message: String)
Copy code
val comments = Comments(listOf(Comment("first", "First comment")))
Copy code
Comments.data.at(someAt, "first").message.getOrNull(comments).let { println(it) } // print "First comment"
Comments.data.at(someAt, "non-existent-id").message.getOrNull(comments).let { println(it) } // print null
Copy code
// This doesn't work, as `At` wants a `Lens`, not an `Optional`
val someAt = At<List<Comment>, String, Comment> { id ->
    Optional(
        getOption = { it.firstOrNone { it.id == id } },
        set = { comments, comment -> /* TODO */ },
    )
}
EDIT: Figured it out! What I wanted was an
Index
!
Copy code
val index = Index<List<Comment>, String, Comment> { id ->
    Optional(
        getOption = { it.firstOrNone { it.id == id } },
        set = { comments, comment -> /* TODO */ },
    )
}
Copy code
Comments.data.index(idx, "first").message.getOrNull(comments).let { println(it) } // print "First comment"
Comments.data.index(idx, "non-existent-id").message.getOrNull(comments).let { println(it) } // print null
solved 1
🙌 2