POptional
Optional is an optic that allows to focus into a structure and querying or copy'ing an optional focus.
import arrow.core.None
import arrow.core.Option
import arrow.core.Some
import arrow.optics.Optional
data class User(val username: String, val email: Option<String>) {
companion object {
// can be out generated by @optics
val email: Optional<User, String> = Optional(User::email) { user, email ->
user.copy(email = Some(email))
}
}
}
fun main() {
val original = User("arrow-user", None)
val set = User.email.set(original, "arRoW-UsEr@arrow-Kt.IO")
val modified = User.email.modify(set, String::lowercase)
println("original: $original, set: $set, modified: $modified")
}
A (polymorphic) POptional is useful when setting or modifying a value for a type with a optional polymorphic focus i.e. POptional
A POptional can be seen as a weaker Lens and Prism and combines their weakest functions:
set: (S, B) -> T
meaning we can focus into anS
and set a valueB
for a targetA
and obtain a modified sourceT
getOrModify: (S) -> Either<T, A>
meaning it returns the focus of a POptional OR the original value
Parameters
the source of a POptional
the modified source of a POptional
the focus of a POptional
the modified focus of a POptional
Inheritors
Properties
DSL to compose a Optional with focus Either with a Prism with a focus of Either.Right<R>
DSL to compose a Traversal with focus Either with a Prism with a focus of Either.Right<R>
Functions
DSL to compose FilterIndex with an Traversal for a structure S to focus in on A at given index I
Find the first element matching the predicate, if one exists.
Get the first target or null
Check if there is at least one target
Get the last target or null
Set the focus of a POptional with a value.